Instead of setting PYTHONPATH in os.environ
, you need to to modify sys.path
, like:
import sys
sys.path.append(pythonpath)
And then go on with your import
s.
The reason setting PYTHONPATH in os.environ
does not work is that sys.path
is what the interpreter refers to at the point of executing an import
directive.
If you set a PYTHONPATH environment variable and then start the interpreter, it would see it, add it to sys.path
, and carry on.
If you start the interpreter with no PYTHONPATH env-var set, sys.path
would not be updated at startup, and the new value in os.environ['PYTHONPATH']
, which is set after the interpreter-startup, will be left unused.