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

Cœur
  • 37,241
  • 25
  • 195
  • 267
kamal
  • 9,637
  • 30
  • 101
  • 168
  • 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` – mgilson May 15 '12 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 – kamal May 15 '12 at 19:50
  • Why -2, even though i tried many solution ? – kamal May 15 '12 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 ...`. – mgilson May 15 '12 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... – Has QUIT--Anony-Mousse May 15 '12 at 20:14
  • Please **detail your question** by adding the **exact** location of your `GenericAPI` class (the correct path name it should be loaded from!) – Has QUIT--Anony-Mousse May 15 '12 at 20:23

3 Answers3

12

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']
An Se
  • 870
  • 11
  • 24
  • 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 Nov 11 '19 at 21:48
  • 1
    @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. – An Se Nov 12 '19 at 08:36
  • 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 Nov 13 '19 at 15:50
  • 1
    @user3342981 do `sys.path += ['/home/user/test1']` for single path. – An Se Nov 13 '19 at 16:19
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...

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • 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. – kamal May 16 '12 at 01:01
0

More like this:

sys.path.append \
("C:\\Program Files\\DIgSILENT\\...");("C:\\Programs\\eclipse...")
falsetru
  • 357,413
  • 63
  • 732
  • 636