pip
is recommended to be run under local user, not root. This means if you do sudo python ...
, you will not have access to your Python libs installed via pip.
How can I run Python (or a pip installed bin/
command) under root / sudo (when needed) while having access to my pip libraries?
So the issue you're likely having is that you have pip
installed the packages you want to a user specific site-packages directory, which then isn't being included in the sys.path
module search list generated by sites.py
when Python starts up for the root user.
There are a couple of workaround for this to add the additional paths to be searched for modules.
PYTHONPATH
environment variable. This should be set to semi-colon delimited and terminated list of paths.sys.path
at the start of your script before running any other imports. I often see this proposed on SO as a workaround around by people who haven't really grasped absolute vs relative imports.I have an example here using both methods. I'm on Windows in a virtual environment, but the principles would be the same in Linux.
Setup:
(venv)
~/PythonVenv/playground311 (main)
$ export PYTHONPATH="C:\Path\From\PythonPath\;E:\Second\PythonPath\Path\;"
Start of script:
import sys
# Insert at position 1 as position 0 is this script's directory.
sys.path.insert(1, r"C:\Path\Inserted\By\Script")
for path in sys.path:
print(path)
Output
c:\Users\nighanxiety\PythonVenv\playground311
C:\Path\Inserted\By\Script
C:\Path\From\PythonPath
E:\Second\PythonPath\Path
C:\Users\nighanxiety\PythonVenv\playground311
C:\python311\python311.zip
C:\python311\DLLs
C:\python311\Lib
C:\python311
C:\Users\nighanxiety\PythonVenv\playground311\venv
C:\Users\nighanxiety\PythonVenv\playground311\venv\Lib\site-packages
If I weren't in a virtual environment, the last few entries are different:
C:\Users\nighanxiety\PythonVenv\playground311
C:\Path\Inserted\By\Script
C:\Path\From\PythonPath
E:\Second\PythonPath\Path
C:\Users\nighanxiety\PythonVenv\playground311
C:\Python311\python311.zip
C:\Python311\Lib
C:\Python311\DLLs
C:\Python311
C:\Python311\Lib\site-packages
I strongly recommend trying a virtual environment and using pip to install the desired packages there first, as Sauron suggests in his answer. The root user should still correctly use the virtual environment paths as long as the environment is activated. See How does activating a python virtual environment modify sys.path for more info.
If that doesn't work, then configuring PYTHONPATH
with the correct absolute path to your site-packages should be a better option than hacking sys.path. Modifying sys.path
makes sense if a specific override is needed for that specific script, but is otherwise a bit of a hack.