2

I am using ubuntu 20.04 to write python programs. I usually install python packages without the sudo prefix. But I noticed that if I go into the sudo mode and then try to import a previously installed python package, it would raise ModuleNotFoundError: No module named 'xxx'. But if I exit the sudo mode everything will be alright again.

What could be the reason? Could it be because in sudo mode the import path is not added to the PATH variable? But that doesn't seem to be the reason if you check my PATH variables below:

Normal user mode:

/usr/local/cuda-10.2/bin:/home/myusername/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Root mode:

/usr/local/cuda-10.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:
2
  • I assume you installed the packages using pip (or pip3)?
    – danzel
    Aug 21, 2020 at 6:59
  • @danzel Yes I did.
    – trisct
    Aug 21, 2020 at 7:30

2 Answers 2

6

Python doesn't use the PATH environment variable. The process of determining where it looks for modules is rather complicated and I won't go into detail here, but in the end, all paths are contained in the sys.path variable. Use the following one-liner to print that list:

python3 -c "import sys; print(sys.path)"

(Note that it only contains existing directories.)

In the output, you'll see a folder inside your home folder. That's your user's site-packages directory, a feature described in PEP 370. And that is the directory where pip3 installs packages by default if you run it with your "normal" user.

If you now switch to the root user, python will look for modules in the root user's home folder, which is /root, so it won't find packages in your "normal" user's site-packages directory.

For development purposes, I would recommend using virtual environments such as venv.

0

My issue (on debian) was that python would run python3 and sudo python ran python2.

Fixed by running sudo python3 app.py

This assumes the modules are installed for root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.