简体   繁体   中英

PyQt5 and Anaconda: ModuleNotFoundError: No module named 'PyQt5'

I made a totally new, blank environment in anaconda and activated it. I then did "conda install pyqt5", however when importing modules, like in

from PyQt5.QtWidgets import QApplication, QWidget

my script throws an error:

File "C:\xyz\xyz.py", line 2, in <module>
from PyQt5.QtWidgets import QApplication, QWidget
ModuleNotFoundError: No module named 'PyQt5'

As "conda list" did show pyqt5 installed, but "pip list" didn't, I did "conda remove pyqt5" and "pip install pyqt5", it shows up in both "list" commands now, but I still get the same error message...

Commenting out everything and adding just a one-line "print("Hello World!")" works, so I believe my problem is not rooted in the relationship of Anaconda with Python?!

The following worked for me:

  • Install pyqt5 using pip install pyqt5 .
  • Then use from PyQt5.QtWidgets import QApplication, QWidget in Python ( note the different case! )

UPDATE:

When using virtual environments you have to be sure you are doing all the stuff in the same virtual environment. To do this, first activate your environment, then just use the python command for everything and avoid using the py or pip commands directly.

The following are some steps to help you debugging your problem:

  • First activate your virtual environment. I don't have experience with anaconda, but I assume it's similar to venv or virtualenv (ie just calling the corresponding activate script) and you know how to do this.
  • Then:
    • Run python -V to check your Python version.
    • Run python -m pip -V to check the version of PIP. Note that this also prints the location of the pip module. This should be in your virtual environment!
    • Run python -m pip list to see which PIP packages are installed. PyQt5 should be included in this list. If not, run python -m pip install pyqt5 and try again.
    • Run python -m pip show pyqt5 to show information about the pyqt5 module. This also should include a location inside your virtual environment.
    • Run python -c "import PyQt5" to check if the PyQt5 module can be imported. This should print nothing (no ModuleNotFoundError ).
    • Run your script using python xyz.py . Don't use the command xyz.py , since in that case the Windows registry determines the "open action", which is likely to run the script using your most recently installed Python version instead of the version from your virtual environment!

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