3

I have updated my operating system to Fedora 27, and I experienced a problem when running Python by root. I cannot import modules I installed with pip3, like psutils

I have installed psutil by pip3

[user@localhost ~]$ sudo pip3 install psutil
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Requirement already satisfied: psutil in /usr/local/lib64/python3.6/site-packages

I can import psutil by a normal user

[user@localhost ~]$ python3
Python 3.6.3 (default, Oct  9 2017, 12:07:10) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages', '/usr/lib/python3.6/site-packages/pykakasi-0.24-py3.6.egg']
>>> import psutil
>>> 

But I cannot import psutil when running root.

[user@localhost ~]$ sudo python3
Python 3.6.3 (default, Oct  9 2017, 12:07:10) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages', '/usr/lib/python3.6/site-packages/pykakasi-0.24-py3.6.egg']
>>> import psutil
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'psutil'
>>> 
[user@localhost ~]$ 

It seems that when running a normal user, '/usr/local/lib64/python3.6/site-packages' and '/usr/local/lib/python3.6/site-packages' are added to sys.path, but how can I add these to python3 when running with root?

Eric Stdlib
  • 1,292
  • 1
  • 18
  • 32
  • Try using the full path to your python installation (ie, run `which python` as your normal user, then use the return from that in your sudo command) – Shadow Dec 11 '17 at 22:15
  • 1
    Possible duplicate of [Expand Python Search Path to Other Source](https://stackoverflow.com/questions/3144089/expand-python-search-path-to-other-source) – smac89 Dec 11 '17 at 22:16
  • 1
    Here's a detailed explanation what Python does https://stackoverflow.com/questions/897792/where-is-pythons-sys-path-initialized-from – Omni Dec 11 '17 at 22:20

0 Answers0