Making Python Use Code in My Directory (not that in /usr/...)
I am trying to work on a Python library that is already installed on my (Ubuntu) system. I checked out that library, edited some files, and wrote a small script to test my changes. Even though I put my script in the same folder as that of the library, it seems Python is us开发者_高级运维ing the installed version instead (the one in /usr/share/pyshared/...
).
This is my directory structure:
src
+ my_package
- my_script.py
+ library_package
- lots_of_code
How can I tell Python to use the code in my directory, not the installed one?
You can dictate where python
searches for modules using the PYTHONPATH
environment variable:
When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path; on Unix, this is usually .:/usr/local/lib/python.
from http://docs.python.org/tutorial/modules.html#the-module-search-path
Lets consider a more general issue. In the /usr/share/pyshared/ there are lots of modules. You wish to override just one of the modules. Say the module name is xyz.py. And it happens to use other modules in /usr/shared/pyshared also.
Say we create $HOME/mylibs and add $HOME to Python's sys.path.
Now wherever we have to use xyz, we do something like
from mylibs import xyz
If we wish to revert back to the original xyz, we try:
import xyz # picks up from /usr/shared/pyshared
I wonder if this kind of approach would be more general. You mask only those modules which you are overriding and keep others in use as usual.
import sys
from os.path import join, dirname, pardir
sys.path.insert(0, join(dirname(__file__), pardir))
This will check the src
directory for any python modules, and will look there first.
So even if you have a module with the same name installed elsewhere, this will cause python to load the local one.
sys.path documentation
.
Check the complete path that Python uses through sys.path
. You should be able to add to the path (in front for precedence).
Of course, you can also use PYTHONPATH environment variable or work-around using .pth
files.
It might work if you set the PYTHONPATH
environment variable to include that directory, e.g.:
$ cd src
$ export PYTHONPATH=./library_package
$ python my_package/my_script.py
精彩评论