python module not found in sudo mode (ubuntu 20.04)

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:

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.