9

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

6
  • 1
    Do you want to do this in a python script? (sys.path.append(path)), from you bash shell? export PYTHONPATH=${PYTHONPATH}:path1:path2:path3:path4, from csh like shells? setenv PYTHONPATH ${PYTHONPATH}:path1:path2:path3:path4 Commented May 15, 2012 at 19:43
  • i alos tried using bash and appending this line in /etc/profile export PATHONPATH=${PYTHONPATH}:/home/user/test1:/home/user/test2:/home/user/test3:/home/user/test4 but when i do echo $PYTHONPATH, i get nothing Commented May 15, 2012 at 19:50
  • Why -2, even though i tried many solution ? Commented May 15, 2012 at 19:52
  • do the directories have an __init__.py file in them (this makes a directory a module)? If not, put one in and then try sys.path.append('/home/user'); from test1.common.api import .... Commented May 15, 2012 at 20:01
  • After changing your /etc/profile did you log out and log in again? The system profile file is only re-read upon login. Alternatively, just execute that command directly in your current shell... Commented May 15, 2012 at 20:14

3 Answers 3

13

sys.path.append('/home/user/test1','/home/user/test2', ...) does not work because append() function can take only 1 argument.

What you could use instead is:

import sys
sys.path += ['/home/user/test1','/home/user/test2','/home/user/test3','/home/kahmed/test4']
Sign up to request clarification or add additional context in comments.

4 Comments

doesn't work on python3.7.4. I still have to do sys.path.append() on individual item to make python recognize those paths.
@user3342981 Just tested on 3.7.5 and it works. Check last elements of sys.path list, added path's will be there. += operator is an equivalent to .extend(), which works exactly like .append() but accepts multiple arguments.
Solution works for multiple paths only. For single path, it weirdly adds every character to path. Try sys.path += ('/home/user/test1'); print(sys.path). P.S. I know question is about multiple paths.
@user3342981 do sys.path += ['/home/user/test1'] for single path.
5

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 as. 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...

1 Comment

this works, but i will still search for a universal solution, where the directories dont have to be hardcoded, and use relative paths. No it did not work for a single path.
0

More like this:

sys.path.append \
("C:\\Program Files\\DIgSILENT\\...");("C:\\Programs\\eclipse...")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.