简体   繁体   English

ModuleNotFound Python

[英]ModuleNotFound Python

In my Macs Terminal I'm not able to use Python packages as I get a ModuleNotFound error.在我的 Mac 终端中,由于收到 ModuleNotFound 错误,我无法使用 Python 包。 This is not the case in VS code, as I tried all kinds of things, including a venv based from a tutorial, and it seemed to work.在 VS 代码中情况并非如此,因为我尝试了各种各样的东西,包括基于教程的 venv,它似乎有效。

When I run pip install requests I get the following:当我运行pip install requests时,我得到以下信息:

  • Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (2.25.1)已满足要求:/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (2.25.1) 中的请求
  • Requirement already satisfied: chardet<5,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (4.0.0)已满足要求:/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages 中的 chardet<5,>=3.0.2(来自请求)(4.0.0)
  • Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (1.26.5)已满足要求:/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages 中的 urllib3<1.27,>=1.21.1(来自请求)(1.26.5)
  • Requirement already satisfied: idna<3,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (2.10)已满足要求:/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages 中的 idna<3,>=2.5(来自请求)(2.10)
  • Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from requests) (2020.12.5)已满足要求:/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages 中的 certifi>=2017.4.17(来自请求)(2020.12.5)

When I run python --version I get Python 3.9.5当我运行python --version我得到 Python 3.9.5

I've spend hours now trying to get this "environment" up and correctly running, but with not much success.我现在花了几个小时试图让这个“环境”正常运行,但没有多大成功。

The problem is that you have multiple Python versions installed.问题是您安装了多个 Python 版本。 One possibly from the official Python website, and one through Homebrew (I'm guessing a bit).一个可能来自官方 Python 网站,一个来自 Homebrew(我猜了一下)。 These interfere somewhat.这些都有些干扰。

Here's the thing though: the "official" one didn't not install the standard python alias.事情是这样的:“官方”没有安装标准的python别名。 It only installed the more explicit python3 command.它只安装了更明确的python3命令。 Oddly (and confusingly) enough, it did install pip , probably alongside pip3 .奇怪(而且令人困惑),它确实安装pip ,可能与pip3一起安装。

When you type python , the shell will find the one installed by Homebrew;当您键入python时, shell 会找到 Homebrew 安装的那个; the other one doesn't not exist.另一个不存在。 When you explicitly type python3 , you'll get the correct one.当您显式键入python3时,您将得到正确的。 It looks like your PATH environment variable is set up to have the "official" installation come first, and then the Homebrew one.看起来您的PATH环境变量已设置为首先进行“官方”安装,然后是 Homebrew 安装。

So, best and easiest is to simply use python3 explicitly.因此,最好和最简单的方法是简单地显式使用python3

You could attempt to uninstall the Homebrew one in /usr/local/ if you want to and don't think you need it.如果您愿意并且认为您不需要它,您可以尝试在/usr/local/中卸载 Homebrew。

You could also tinker inside the /Library/Frameworks/Python.framework/Versions/3.9/bin and make an alias for python , but I wouldn't recommend it, unless you become more familiar with the shell, Python installations and your system. You could also tinker inside the /Library/Frameworks/Python.framework/Versions/3.9/bin and make an alias for python , but I wouldn't recommend it, unless you become more familiar with the shell, Python installations and your system. And after all, it's only one character more to type currently.毕竟,目前只需要多输入一个字符。

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

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