【问题标题】:Why don't my modules install in my current python environment?为什么我的模块没有安装在我当前的 python 环境中?
【发布时间】:2020-05-02 16:35:54
【问题描述】:

我遇到了一个反复出现的问题,但一直找不到答案。
每当我通过终端在 PyCharm 中安装包时,每当我尝试导入模块时,包似乎都没有安装。或者,就像刚才一样,可能已经安装了包,但是在导入模块时出现错误 "No module named x":

C:\Users\TimStack\PycharmProjects\API>pip install requests
Requirement already satisfied: requests in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (2.23.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (2019.11.28)
Requirement already satisfied: idna<3,>=2.5 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (2.9)
Requirement already satisfied: chardet<4,>=3.0.2 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (1.25.8)

查看这些目录,似乎是指旧的 Python 3.7 安装。但是,我的环境使用 3.8

这里有什么问题,我该如何解决?

【问题讨论】:

  • 检查 PyCharm 中的解释器,它可能与安装了模块的解释器不同
  • @sK500 当前解释器是 3.8。我打算在 3.8 中安装,但从这些目录来看并没有发生这种情况
  • 转到preferences &gt; project &gt; python interpreter 应该有可用库的列表... numpy 列出了吗?如果不点击那里的+ 注册并安装 numpy 或任何你想要的包。
  • @sK500 这就是我一直在做的,但我更喜欢通过终端来做,因为它更快
  • @sK500 这不仅仅是 p3,OP 在他的系统上有多个版本的 python3。他想强制 pip 针对特定的 python 版本进行安装。

标签: python pip pycharm virtual-environment


【解决方案1】:

您需要卸载旧的 3.7 版本,因为您有 2 个相互冲突的 Python 版本。

在安装包时指定你的 Python 版本:

pip3.8 install [package name]

(Python 3.8 为 3.8)

【讨论】:

  • pip-3.8 install requests 给出错误'pip-3.8' is not recognized as an internal or external command, operable program or batch file.
  • 对不起,把-去掉,改成pip3.8
  • 谢谢,成功了。还有更多“永久”解决方案吗?不会强迫我指定 python 版本并且我不必卸载 3.7 的东西?
  • @TimStack 不抱歉,据我所知,除了卸载没有其他选择
【解决方案2】:

pip install--python-version &lt;python_version&gt; 作为参数。 您可以将 3.8 作为参数传递,它会为您选择正确的参数。

来自文档 -

用于 wheel 和 "Requires- Python”兼容性检查。默认为派生自的版本 正在运行的解释器。可以使用 up 指定版本 到三个点分隔的整数(例如,“3”代表 3.0.0,“3.7”代表 3.7.0 或“3.7.3”)。主要-次要版本也可以作为 不带点的字符串(例如 3.7.0 的“37”)。

【讨论】:

  • pip install requests --python-version 3.8 返回错误ERROR: When restricting platform and interpreter constraints using --python-version, --platform, --abi, or --implementation, either --no-deps must be set, or --only-binary=:all: must be set and --no-binary must not be set (or must be set to :none:).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多