Search code examples
pythonpipinstallationsudoimporterror

ImportError: No module named... After 'sudo pip install..'


Similar to this question, I have problems importing my own modules and get an ImportError: No module named .... For example, I have a module at myfolder/mymodule.py that I try to load with from myfolder import mymodule.

The difference to the other question is that it was working fine before and the problem only occurs on one machine (it works on two other machines). Also I think I know what might have caused the problem: I ran sudo pip install numpy to install NumPy. Now, I read that you should never use sudo with pip install, but it's too late.

Is it possible that sudo pip install broke something here? If so, how can I fix it?

So far, I didn't mess with PYTHONPATH as suggested by the top answer because I also read that you shouldn't have to touch it when using pip and I don't want to further break the system.

Just to clarify; numpy installed correctly, works and is listed when running pip freeze. The problem is really with importing my own module. I don't have a setup.py yet. And again, it worked before - so why would I suddenly need to set a variable or path?


Solution

  • I'm still not sure what suddenly caused the problem (if it was really caused by using sudo pip install).

    Anyway, I fixed it by properly packaging my code with a setup.py and then installing it with sudo python3 setup.py develop. Didn't work without sudo.