3

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?!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
morph3us
  • 220
  • 1
  • 3
  • 13
  • Duplicate: [Python found No module named package that is installed with conda install](https://stackoverflow.com/q/53197674/570918) – merv Jun 05 '19 at 03:00
  • @merv thank you for the link, it seems that guy is having the same issue, as "pip list" does not show pyqt (when "conda list" does). however, the solution is to not use anaconda, which I feel is not a "solution"... – morph3us Jun 05 '19 at 19:42
  • @eyllanesc I am sorry for my bad phrasing, I am actually not a native english speaker, please forgive me. :) I edited my answer accordingly! – morph3us Jun 05 '19 at 19:47
  • @eyllanesc I do still get the same error message. I changed the script I am starting to a simple "print("Hello World!")" one-liner, and that works started through anaconda. So I believe thats not the problem? – morph3us Jun 05 '19 at 19:56
  • @eyllanesc yet again, please excuse my bad phrasing, I scrambled a few things together. I believe that older pyqt versions where imported with lower case, but PyQt5 isn't anymore(?). As it was irrelevant to my question, as we rooted that out to not be the problem, I removed it from my original question. Thank you for your patience! :) – morph3us Jun 05 '19 at 20:32

1 Answers1

4

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 (i.e. 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!
wovano
  • 4,543
  • 5
  • 22
  • 49
  • So I did "conda remove pyqt5" and then did "pip install pyqt5". I still get a Traceback (most recent call last): File "C:\shelf\shelf.py", line 2, in from PyQt5.QtWidgets import QApplication, QWidget ModuleNotFoundError: No module named 'PyQt5' – morph3us Jun 05 '19 at 19:48
  • @morph3us, maybe [this answer](https://stackoverflow.com/a/56451612/10669875) can help you? – wovano Jun 05 '19 at 20:34
  • Thank you for the link, I only have one Python version installed, though, which is confirmed by "$ py -0p". And my Python as a whole works, so I believe it hasn't got to do with the PATH variable? Maybe anaconda gets confused internally, because this is my second environment? However, switching back to my old environment and using packages only installed in there (e.g. tensorflow), works. So I assumed its a pyqt specific problem... – morph3us Jun 05 '19 at 20:53
  • @morph3us, using virtual environments complicates the story a bit more (although using virtual environments is a good idea!). I've updated this answer with some additional information. I really hope this helps, because I don't have any other thoughts at this moment. I'll be happy to hear the results. – wovano Jun 05 '19 at 22:43
  • thank you very much for your help! the corresponding command is "conda activate env". =>python -V: Python 3.5.6 :: Anaconda, Inc. =>-m pip -V: pip 10.0.1 from C:\Users\morph3us\Miniconda3\envs\myenvname\lib\site-packages\pip (python 3.5) => - pip list: PyQt5 5.12.2 => -m pip show pyqt5: Location: c:\users\morph3us\miniconda3\envs\myenvname\lib\site-packages =>-c "import PyQt5": throws no error, so everything seems in order. Still, a script run directly afterwards containing one line ("import PyQt5") throws "ModuleNotFoundError: No module named 'PyQt5'" – morph3us Jun 06 '19 at 20:38
  • @morph3us, great that you didn't get an error in the last step. That's hopeful. So now the question remains: how do you run the script containing the one line? If you're doing `python that_script.py` from the same virtual environment, that should work as expected. If you directly run `that_script.py` (as a command on its own), then the Windows registry determines which Python to run (which is definitely not the virtual environment, so that would explain it). – wovano Jun 06 '19 at 20:44
  • yes, that's it! thank you so much! I am so emberrassed right now, as this is a very dumb error on my side! Please take my apologies for taking up your time with this silly nonsense... If you would edit this into your answer, I would approve it. @everybody else: please do consider taking away the "on hold status", as this is a piece of information I did not find in any other thread (and I would like to not have negative rep :) ) Again, thanks so much to everyone participating, this gave me headaches... – morph3us Jun 06 '19 at 20:57
  • @morph3us, I'm glad I could help :-) I've updated my [other answer](https://stackoverflow.com/a/56451612/10669875) with this information as well. As you can see, it's quite a complex situation in Windows, so I can understand that it wasn't immediately clear for you. Good thought to update your question with this information, it might certainly make a change (you got my upvote in advance ;p). – wovano Jun 06 '19 at 21:07