In Python, I'm getting an error because it's loading a module from /usr/lib/python2.6/site-packages
but I'd like it to use my version in $HOME/python-modules/lib/python2.6/site-packages
, which I installed using pip-python --install-option="--prefix=$HOME/python-modules --ignore-installed
How can I tell Python to use my version of the library? Setting PYTHONPATH
to $HOME/python-modules/lib/python2.6/site-packages
doesn't help, since /usr/lib/...
apparently has precedence.
The easiest way to change it is to add a file /usr/local/lib/python2. 6/dist-packages/site-packages. pth containing ../site-packages . Alternatively, maybe you can teach the package to use site.
Place module.py in the folder where the program will execute. Include the folder that contains the module.py in the PYTHONPATH environment variable. Or you can place the module.py in one of the folders included in the PYTHONPATH variable. Place the module.py in one of the installation-dependent folders.
The Python Standard Library contains hundreds of modules for performing common tasks, like sending emails or reading JSON data. What's special about the Standard Library is that it comes bundled with your installation of Python, so you can use its modules without having to download them from anywhere.
It is a reusable chunk of code that we can use by importing it into our program, we can just use it by importing that library and calling the method of that library with a period(.). However, it is often assumed that while a package is a collection of modules, a library is a collection of packages.
Take a look at the site module for ways to customize your environment.
One way to accomplish this is to add a file to a location currently on sys.path
called usercustomize.py
, when Python is starting up it will automatically import this file, and you can use it to modify sys.path
.
First, set $PYTHONPATH
to $HOME
(or add $HOME
if $PYTHONPATH
has a value), then create the file $HOME/usercustomize.py
with the following contents:
import sys, os
my_site = os.path.join(os.environ['HOME'],
'python-modules/lib/python2.6/site-packages')
sys.path.insert(0, my_site)
Now when you start Python you should see your custom site-packages directory before the system default on sys.path
.
Newer Python versions now have built-in support to search the opendesktop location:
$HOME/.local/lib/pythonX.Y/site-packages
If you put your local modules there you don't have to any sys.path manipulations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With