Skip to main content
Avoid the KeyError issue
Source Link
Vitali
  • 3.8k
  • 2
  • 28
  • 29

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.seppathsep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os os.environ['PYTHONPATH']environ.get('PYTHONPATH', '').split(os.pathsep) 

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os os.environ['PYTHONPATH'].split(os.pathsep) 

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.pathsep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os os.environ.get('PYTHONPATH', '').split(os.pathsep) 
os.sep is for directory separator - needs to be os.pathsep for the path separator.
Source Link
Vitali
  • 3.8k
  • 2
  • 28
  • 29

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os import sys os.environ['PYTHONPATH'].split(os.seppathsep) 

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os import sys os.environ['PYTHONPATH'].split(os.sep) 

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os os.environ['PYTHONPATH'].split(os.pathsep) 
Source Link
Vitali
  • 3.8k
  • 2
  • 28
  • 29

Can't seem to edit the other answer. Has a minor error in that it is Windows-only. The more generic solution is to use os.sep as below:

sys.path might include items that aren't specifically in your PYTHONPATH environment variable. To query the variable directly, use:

import os import sys os.environ['PYTHONPATH'].split(os.sep)