Saturday, November 20, 2010

How do I change the directory in which Python Interpreter looks for a module on the 'import ' command?

Ok, so I have several version of python from 2.5 to 3, and they all attempt to look for a module upon the import command at the same place, that is my home directory. If i move the modules to say each's site-packages direction, as this is where they should belong, it says that there is no such module when I use the 'import' command.



How do I redirect where Python look for a module?



ThanksHow do I change the directory in which Python Interpreter looks for a module on the 'import ' command?
When looking for a module, Python searches the list of directories contained in the sys.path variable.



Every build of Python has a built-in list of directories that it will search when looking for a module, and that built-in list provides the initial value of sys.path. You can specify additional directories in the PYTHONPATH environment variable, and you can use command-line options to disable some parts of the search path. You can also have your program modify sys.path before it tries to import your module.



The site-packages directory should be on the built-in search path, so if you're putting If your module there and Python doesn't find it then that's pretty strange. I'd check to see that the site-packages directory really is on the built-in path, by running a two-line program that does just:



聽聽import sys

聽聽print sys.path



If it is, then I'd check the permissions on my module in the site-packages directory. On a Unix system if you put them there while logged in as root, it's possible that they aren't readable when you're logged in as yourself.

No comments:

Post a Comment