How to append multiple Paths to PYTHONPATH programmatically

path, python, pythonpath

Solution

Try this:

import sys
sys.path.append('/home/user/')
from test1.common.api import GenericAPI

It is not recommended, but will maybe do what you meant to do? Because I guess your files are not in the folder `/home/user/test1/test1/common/api/` ...

Given a python path of `["a", "b", "c"]`, trying to `import a.b.c` will look in `a/a/b/c`, then `b/a/b/c` and `c/a/b/c`. However, NOT in `a/b/c`. There is no matching of the module name starting with `a` and the python path ending with `a` and then leaving out one of the `a`s. It strictly is path + module, not part-of-path + part-of-module.

Since your question is about "multiple paths", does a single path work for you yet? Doesn't seem so...

Problem

I have 4 directories: ``` /home/user/test1 /home/user/test2 /home/user/test3 /home/user/test4 ``` I have another directory with tests ``` /home/user/testing ``` having the file testall.py ow, how can I append PATHS, for test1 thru test4 to PYTHONPATH so that I can access the files under test1 thru 4. btw, test1 thru 4 have multiple directories under them where the python files are located. I tried: ``` import sys import os PROJECT_ROOT = os.path.dirname(__file__) sys.path.insert(0,os.path.join(PROJECT_ROOT,"test1")) sys.path.insert(1,os.path.join(PROJECT_ROOT,"test2")) sys.path.insert(2,os.path.join(PROJECT_ROOT,"test3")) sys.path.insert(3,os.path.join(PROJECT_ROOT,"test4")) ``` did not seem to work also: ``` import sys sys.path.append('/home/user/test1','/home/user/test2','/home/user/test3','/home/kahmed/test4') from test1.common.api import GenericAPI ``` did not work. basically: from test1.common.api import GenericAPI should work

Original source