简体   繁体   English

Python 3.9 导入问题(modulenotfounderror)

[英]Python 3.9 import problem (modulenotfounderror)

I have python 3.9 installed in windows.我在 Windows 中安装了 python 3.9。 I use pycharm.我使用 pycharm。 I downloaded the module qrcode using the code 'pip install qrcode' in the terminal.我使用终端中的代码“pip install qrcode”下载了模块 qrcode。 This has successfully installed as when I type 'pip show qrcode' in the terminal in pycharm, I get;这已成功安装,因为当我在 pycharm 的终端中键入“pip show qrcode”时,我得到了;

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;当我尝试将 qrcode 导入我的项目(一个 qrcode 生成器)时出现问题,在键入 import qrcode 后,我收到以下消息;

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.如果我输入 from colorama import qrcode,它仍然不起作用,因为它说即使我安装了它也没有找到 colorama。

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.我已经尝试找到 sys 路径,如果我在其中找不到我的站点包的位置,我将只使用 append 函数来添加它们。 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 [截图] https://i.stack.imgur.com/83omh.png

The location of python.exe on my laptop is C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39 python.exe 在我的笔记本电脑上的位置是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 python 3.9(64位)在我的笔记本电脑上的位置是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.几周前我开始使用python,所以我不太了解。 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.您正在 PyCharm 中使用 virtualenv,并且您正在全局python 安装上安装qrcode包。

Take a look at the package install location and the location of the python executable getting called from within PyCharm查看包安装位置和从 PyCharm 中调用的 python 可执行文件的位置

qrcode:二维码:
c:\\users\\user\\appdata\\local\\programs\\python\\python39\\lib\\site-packages
PyCharm python.exe 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.您需要获取 virtualenv,然后在 virtualenv 中安装qrcode

Start reading here ...这里开始阅读...


EDIT编辑

  1. navigate to correct folder C:\\Users\\USER\\PycharmProjects\\MyProjects导航到正确的文件夹C:\\Users\\USER\\PycharmProjects\\MyProjects
  2. run this command: .\\env\\Scripts\\activate.bat (or .\\env\\Scripts\\activate.ps1 if using PowerShell)运行此命令: .\\env\\Scripts\\activate.bat (如果使用 PowerShell,则为.\\env\\Scripts\\activate.ps1
  3. your commandline should now indicate you are using a virtualenv.您的命令行现在应该表明您正在使用 virtualenv。
  4. run this command: pip install qrcode运行这个命令: 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.另一种选择是在与项目相同的目录中创建一个requirements.txt文件,并将qrcode作为一行放在那里。 IIRC PyCharm will pop up a notification asking if you want to install qrcode . IIRC PyCharm 会弹出一个通知,询问您是否要安装qrcode (I'm not 100% sure about this one as I switched to Visual Studio Code) (当我切换到 Visual Studio Code 时,我对这个不是 100% 确定)

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.我认为您的项目的 python 解释器被定向到您项目的 virtualenv 中的一个解释器(在您创建项目时由 pycharm 创建),但是根据您的安装响应,您的包已安装在您的本机 python 安装上。 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); a) 将您项目的python解释器更改为您自己的python安装(只需添加 C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\python.exe 作为解释器并将其设置为您的项目解释器); or要么

EDIT: b) Install the package using the virtualenv's python interpreter.编辑:b) 使用 virtualenv 的 python 解释器安装包。 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).使用“C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\activate”激活您项目的虚拟环境(您的提示应该会改变,显示您现在正在使用虚拟环境)。 Then use pip install qrcode.然后使用 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.如果这不起作用,请在终端上尝试“C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\Scripts\\python.exe -m pip install qrcode”。 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'.如果这不起作用,请尝试“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此链接还包含一些关于如何管理解释器和安装/升级项目包的内容Install、unninstall 和 upgrade packages-jetbrains

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

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