Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sys.path modification not working

Tags:

python

path

I'm trying to modify the sys.path in one of my Python files in order to have some specific libraries dirs in the modules search path (it might not be the best way but ...). If I insert several paths in the front of sys.path my script is not taking into account those paths for future imports. If i make a whole new list containing those libraries dirs i need and assign that list to sys.path then those imports are taken into account. Is this the correct behavior? I'm using python 2.5.4. Could it be something from my environment that could lead to such behavior?

Some code snippets: If I do

pathtoInsert1 = " .... "
pathtoInsert2 = " .... "
sys.path.insert(0, pathToInsert1)
sys.path.insert(0, pathToInsert2)

it does not work. It does not take into account the paths.

If I do

pathList = [pathToInsert1, pathToInsert2] 
sys.path = pathList

it works.

Thanks

like image 718
celavek Avatar asked Jun 19 '09 13:06

celavek


3 Answers

I just had a similar problem while working in iPython with modules that are distributed over several directories. In that case, to get import to work, one must make sure the module.__path__ of modules with distributed __init__.py includes all directories where one of the module's __init__.py are, as well as making sure the correct directory is in the sys.path list.

For example, I have a module called foo, which contains a module called bar which is spread over several directories:

aerith/foo/bar/__init__.py
aerith/foo/bar/baz/__init__.py
bob/foo/bar/__init__.py
bob/foo/bar/baf/__init__.py
carol/foo/bar/__init__.py
carol/foo/bar/quux/__init__.py

In iPython, I had already imported baz and baf, and wanted to import quux.

from foo.bar import quux

This gave an ImportError, because carol was not in sys.path, but sys.path.append('carol') did not fix the ImportError.

What was required was informing the bar module that one of its __init__.py could be found in 'carol/foo/bar'.

foo.bar.__path__.append('carol/foo/bar')
from foo.bar import quux
like image 166
pcurry Avatar answered Nov 07 '22 02:11

pcurry


You really need to post some code for us to be able to help you. However, I can make an educated guess. You say that if you make a whole new list and assign it to sys.path then it works. I assume you mean that you're doing something like this

sys.path = ["dir1", "dir2", ...]

But that if you insert the paths at the beginning then it doesn't work. My guess is that you're using the insert method, like so

sys.path.insert(0, ["dir1", "dir2"])

If so then this is incorrect. This would create a list that looks like

[["dir1", "dir2"], "dir3", ...]

You should instead say

sys.path[:0] = ["dir1", "dir2"]

which will give you

["dir1", "dir2", "dir3", ...]

But this is all guesswork until you post your code.

like image 19
Eli Courtwright Avatar answered Nov 10 '22 17:11

Eli Courtwright


Example of updating sys.path taken from here

import sys
sys.path.append("/home/me/mypy")

This worked for me.

like image 3
bentford Avatar answered Nov 10 '22 16:11

bentford