简体   繁体   中英

Python 3.9 import problem (modulenotfounderror)

I have python 3.9 installed in windows. I use pycharm. I downloaded the module qrcode using the code 'pip install qrcode' in the terminal. This has successfully installed as when I type 'pip show qrcode' in the terminal in pycharm, I get;

Name: qrcode
Version: 7.2
Summary: QR Code image generator
Home-page: https://github.com/lincolnloop/python-qrcode
Author: Lincoln Loop
Author-email: info@lincolnloop.com
License: BSD
Location: c:\users\user\appdata\local\programs\python\python39\lib\site-packages
Requires: colorama
Required-by:

The problem occurs when I try to import qrcode into my project (a qrcode generator), after typing import qrcode, I get the following message;

C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\python.exe "C:/Users/USER/PycharmProjects/MyProjects/QR Code Generator.py"
Traceback (most recent call last):
  File "C:\Users\USER\PycharmProjects\MyProjects\QR Code Generator.py", line 1, in <module>
    import qrcode
ModuleNotFoundError: No module named 'qrcode'

If i type from colorama import qrcode, it still doesn't work as it says colorama is not found even though I installed it.

I have tried finding the sys path and if I can't find the location of my site packages in it I will just use the append function to add them. However, it is still there as you can see below;

['C:\\Users\\USER\\PycharmProjects\\MyProjects', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\lib', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39', 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv', 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\lib\\site-packages']

Just a side note, when I click file, settings, project interpreter, this is what I see;

[Screenshot] https://i.stack.imgur.com/83omh.png

The location of python.exe on my laptop is C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39

The location of python 3.9 (64 bit) on my laptop is C:\\Users\\USER\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.9

I started python just a few weeks ago, so I don't know that much. Anyways, hope I can get an answer.

You are using a virtualenv inside of PyCharm and you are installing the qrcode package on the global python installation.

Take a look at the package install location and the location of the python executable getting called from within PyCharm

qrcode:
c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages
PyCharm python.exe
C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\python.exe

You need to source the virtualenv and then install qrcode in the virtualenv.

Start reading here ...


EDIT

  1. navigate to correct folder C:\\Users\\USER\\PycharmProjects\\MyProjects
  2. run this command: .\\env\\Scripts\\activate.bat (or .\\env\\Scripts\\activate.ps1 if using PowerShell)
  3. your commandline should now indicate you are using a virtualenv.
  4. run this command: pip install qrcode
  5. try running your code now...

Another option would be to create a requirements.txt file in the same directory as your project, and put qrcode as a single line in there. IIRC PyCharm will pop up a notification asking if you want to install qrcode . (I'm not 100% sure about this one as I switched to Visual Studio Code)

I think the python interpreter for your project is directed to an interpreter in the virtualenv of your project (created by pycharm when you created the project), but your package got installed on your native python installation, according to your installation response. From what i see, you can either:

a) change your project's python interpreter to your own python installation (just add C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\python.exe as an interpreter and set it as your project interpreter); or

EDIT: b) Install the package using the virtualenv's python interpreter. Activate your project's virtual environment with 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\activate' (your prompt should change showing you are now using the virtual environment). Then use pip install qrcode. If this doesn't work try 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\python.exe -m pip install qrcode' on your terminal. If this doesn't work maybe try 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\python.exe -m C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\pip.exe install qrcode'. END OF EDIT

This link also contains a bit about how to manage your interpreter and install/upgrade packages for your project Install, unninstall and upgrade packages-jetbrains

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