How can you use a Python script to programmatically append the current path to the current Virtualenv's PYTHONPATH, using either .pth or another method?
This needs to work on both Windows and Linux.
I'm trying to have a Python script setup.py
in the project root path which when executed, adds the project root path to PYTHONPATH so other scripts in nested directories can import modules by relative import from the project root.
MyProjectRoot
+ setup.py
+ data
+ source
+ foo
+ lib
+ qux
+ A.py
+ bar
+ assets
+ B.py
+ baz
+ C.py
For example, in C.py
, we can use
import source.foo.lib.qux.A
import source.bar.assets.B
to avoid having to add a variant of the following to every file that wishes to use import relative to the project root:
basePath = os.path.abspath("../..")
sys.path.append(basePath)