简体   繁体   中英

Installed package via pip, but ModuleNotFoundError

OK so I'm on a windows machine, and wanted to use paho-mqtt package. Downloaded official Python 3 installer msi file, and installed python 3, I don't remember any installation of python before this.

>python --version
Python 3.8.1

Next, Installed paho-mqtt via pip

>pip install paho-mqtt
Collecting paho-mqtt
  Using cached https://files.pythonhosted.org/packages/59/11/.../paho-mqtt-1.5.0.tar.gz
Installing collected packages: paho-mqtt
    Running setup.py install for paho-mqtt ... done
Successfully installed paho-mqtt-1.5.0

Tried same command via pip3 and it says the package is already installed.

But when I import paho.mqtt.client as mqtt I get the following error

Traceback (most recent call last):
  File "C:\mqt\paho.py", line 2, in <module>
    import paho.mqtt.client as mqtt
  File "C:\mqt\paho.py", line 2, in <module>
    import paho.mqtt.client as mqtt
ModuleNotFoundError: No module named 'paho.mqtt'; 'paho' is not a package

I've done every solution provided online but no chance.

Also got the installation directory using https://stackoverflow.com/a/49028561/2543240 the paho directory is there and contains py files.

Is there any debugging command that can help such conditions, to see where is python actually looking for the files it wants to load and can't?

Any help would be greatly appreciated.

Edit

Simply import paho returns no error. but import paho.mqtt leads to

ModuleNotFoundError: No module named 'paho.mqtt'; 'paho' is not a package

So installing and uninstalling makes some difference, but just for import paho not import paho.mqtt or import paho.mqtt.client as mqtt . for the later imports the error message is there whether i install or uninstall paho-mqqt.

If you name your source file as paho.py, it could hide the paho package.

So renaming paho.py to paho2.py should fix the problem.

Your script C:\\mqt\\paho.py when being run is used by Python import system as a module paho so import paho works but import paho.mqtt doesn't because paho.py is considered by Python as a module, not a package.

Rename your script. And please remember the experience for the future: never give your scripts the same names as existing modules or packages, especially modules or packages from the standard library. For example, always avoid naming your scripts email.py or test.py .

You might not be calling the same python when you run pip .

You can guarantee that you're running pip inside of the correct Python 3.8.1 instance by running:

python -m pip install paho-mqtt

Does that help you at all?

You have to mention the version of Python, when normal pip is not working.

Install by using sudo pip3 install paho-mqtt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM