简体   繁体   English

同一台计算机上的单独版本的Python不会导入相同的模块

[英]Separate versions of Python on same computer won't import same modules

I've been coding in Python 3.7.2 as my usual, but an API that I really want for my code only supports up to 3.6 (and does not support 2.7). 我像往常一样使用Python 3.7.2进行编码,但是我真正想要用于我的代码的API仅支持多达3.6(并且不支持2.7)。 I downloaded Python 3.6.4 to my computer, which also downloads a separate instance of the IDLE (not a problem). 我将Python 3.6.4下载到了我的计算机上,该计算机还下载了IDLE的单独实例(这不是问题)。 If I try to import something like numpy to my code in 3.7 (ex. import numpy as np) then it works as expected. 如果我尝试将类似numpy的内容导入到我的3.7中的代码中(例如,将numpy导入为np),则它将按预期工作。 However, if I do the same in the 3.6 IDLE I get: 但是,如果在3.6 IDLE中执行相同的操作,则会得到:

Traceback (most recent call last): File "", line 1, in import numpy as np ModuleNotFoundError: No module named 'numpy' 追溯(最近一次调用):文件“”,行1,在导入numpy中作为np出现ModuleNotFoundError:没有名为“ numpy”的模块

I think that it's a path problem but I'm unsure on how to fix it, and I can't find a solution to this problem elsewhere. 我认为这是一个路径问题,但是我不确定如何解决它,而且在其他地方也找不到解决此问题的方法。 Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

尝试专门为python3.6安装numpy:

python3.6 -m pip install numpy

Step 1: Get the location of the python executable from IDLE 步骤1:从IDLE获取python可执行文件的位置

import sys

print(sys.executable) # e.g. /Users/jk/.../bin/python

Step 2: Run the pip in the same folder as the one returned above. 步骤2:在与上面返回的文件夹相同的文件夹中运行pip

/Users/jk/.../bin/pip install numpy

PS It's better to maintain libraries independently for each distribution, or even better use virtualenv or conda to create environments. PS最好为每个发行版独立维护库,甚至更好地使用virtualenvconda创建环境。

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

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