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:


Root mode:

  • 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

4 Answers 4


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.


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.


Just do sudo pip install <package-name>


The solution for me was instead of writing:

sudo python test.py


sudo /home/dietpi/project/env/bin/python test.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .