简体   繁体   English

PyQt5和Anaconda:ModuleNotFoundError:没有名为“ PyQt5”的模块

[英]PyQt5 and Anaconda: ModuleNotFoundError: No module named 'PyQt5'

I made a totally new, blank environment in anaconda and activated it. 我在anaconda中创建了一个全新的空白环境,并将其激活。 I then did "conda install pyqt5", however when importing modules, like in 然后我做了“ conda install pyqt5”,但是在导入模块时,例如在

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... 由于“ conda list”确实显示了已安装的pyqt5,但没有“ pip list”,所以我进行了“ conda remove pyqt5”和“ pip install pyqt5”,它现在同时显示在两个“ list”命令中,但我仍然得到相同的结果错误信息...

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?! 注释掉所有内容并仅添加单行“ print(“ Hello World!”)“是可行的,所以我相信我的问题不根源于Anaconda与Python的关系吗?

The following worked for me: 以下为我工作:

  • Install pyqt5 using pip install pyqt5 . 使用pip install pyqt5
  • Then use from PyQt5.QtWidgets import QApplication, QWidget in Python ( note the different case! ) 然后使用from PyQt5.QtWidgets import QApplication, QWidget Python中的from PyQt5.QtWidgets import QApplication, QWidget请注意不同的情况!

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. 为此,请先激活您的环境,然后对所有内容使用python命令,并避免直接使用pypip命令。

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. 我没有使用anaconda的经验,但是我认为它类似于venvvirtualenv (即仅调用相应的activate脚本),并且您知道如何执行此操作。
  • Then: 然后:
    • Run python -V to check your Python version. 运行python -V检查您的Python版本。
    • Run python -m pip -V to check the version of PIP. 运行python -m pip -V检查PIP版本。 Note that this also prints the location of the pip module. 请注意,这还将打印pip模块的位置。 This should be in your virtual environment! 应该在您的虚拟环境中!
    • Run python -m pip list to see which PIP packages are installed. 运行python -m pip list以查看安装了哪些PIP软件包。 PyQt5 should be included in this list. PyQt5 应该包含在此列表中。 If not, run python -m pip install pyqt5 and try again. 如果没有,请运行python -m pip install pyqt5试。
    • Run python -m pip show pyqt5 to show information about the pyqt5 module. 运行python -m pip show pyqt5以显示有关pyqt5模块的信息。 This also should include a location inside your virtual environment. 这还应该包括虚拟环境中的位置。
    • Run python -c "import PyQt5" to check if the PyQt5 module can be imported. 运行python -c "import PyQt5"以检查是否可以导入PyQt5模块。 This should print nothing (no ModuleNotFoundError ). 应该什么都不打印(没有ModuleNotFoundError )。
    • Run your script using python xyz.py . 使用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! 不要使用命令xyz.py ,因为在这种情况下,Windows注册表会确定“打开操作”,这很可能会使用您最近安装的Python版本而不是虚拟环境中的版本来运行脚本!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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