Python 如何将 numpy 模块导入/打开到 IDLE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36346597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:46:07  来源:igfitidea点击:

How to import/open numpy module to IDLE

pythonnumpyanacondapython-idle

提问by SUPhys

I want to use numpy for a program I have to run and I want to do it in the IDLE IDE. I have installed the numpy binary from online, but when I try running "import numpy" and then some numpy commands in my script, but the python shell returns an error saying

我想将 numpy 用于我必须运行的程序,并且我想在 IDLE IDE 中执行它。我已经从网上安装了 numpy 二进制文件,但是当我尝试运行“import numpy”然后在我的脚本中运行一些 numpy 命令时,python shell 返回一个错误说

Traceback (most recent call last):
  File "/Users/Admin/Desktop/NumpyTest.py", line 1, in <module>
    import numpy as np
ImportError: No module named numpy

I have tried using pip to install numpy, but when I run pip install numpyin the bash shell, it says

我曾尝试使用 pip 安装 numpy,但是当我pip install numpy在 bash shell 中运行时,它说

Requirement already satisfied (use --upgrade to upgrade):
numpy in ./anaconda/lib/python2.7/site-packages

I have downloaded Anaconda, which I can use the numpy distribution in, but I would really like to do it in IDLE.

我已经下载了 Anaconda,我可以在其中使用 numpy 发行版,但我真的很想在 IDLE 中使用它。

What should I do to get numpy working in IDLE? Do I have to save it somewhere?

我应该怎么做才能让 numpy 在 IDLE 中工作?我必须把它保存在某个地方吗?

p.s. I am running OsX 10.10.5 Yosemite

ps 我正在运行 OsX 10.10.5 Yosemite

回答by Terry Jan Reedy

The title is misleading in the following sense. You do not want to import a module to IDLE. You want to import it to the python that is running your code. When running IDLE, this currently is the same python running IDLE. To find which python is running, the following should work anywhere on any recent python, either directly or in an IDE:

标题在以下意义上具有误导性。您不想将模块导入 IDLE。您想将其导入到运行您的代码的 python 中。运行 IDLE 时,这当前与运行 IDLE 的 python 相同。要查找哪个 python 正在运行,以下内容应该可以在任何最近的 python 上直接运行或在 IDE 中运行:

import sys; print(sys.executable)

Running this in IDLE on my Windows machine, I get

在我的 Windows 机器上的 IDLE 中运行它,我得到

C:\Programs\Python36\pythonw.exe

(The wsuffix is a Windows-specific variant binary for running GUI programs withoutan empty console window popping up. It should be omitted in what follows.)

w后缀是 Windows 特定的变体二进制文件,用于运行 GUI 程序,而不会弹出空的控制台窗口。以下内容应省略。)

To import a module to a particular python, it must be installed for that particular python. The easiest way to do that is to run pip with that particular python in a console. For instance, given the executable above:

要将模块导入特定的 python,必须为该特定的 python 安装它。最简单的方法是在控制台中使用特定的 python 运行 pip。例如,给定上面的可执行文件:

C:\Programs\Python36> python -m pip install numpy

On *nix, one may have to first run, I believe, python -m ensurepipto install pip itself for that python.

在 *nix 上,我相信可能必须先运行python -m ensurepip才能为该 python 安装 pip 本身。

About import pip; pip.main: pip is designed as a command line utility that initializes, performs one function, and exits. main() is an intentionally undocumented internal implementation detail. The author of pip discourages its use as it is designed for one call followed by program exit. Multiple calls will not work right when internal data get out of sync with installed files.

关于import pip; pip.main:pip 被设计为一个命令行实用程序,可以初始化、执行一个功能并退出。main() 是故意未记录的内部实现细节。pip 的作者不鼓励使用它,因为它是为一次调用而设计的,然后程序退出。当内部数据与安装的文件不同步时,多次调用将无法正常工作。

回答by Tadhg McDonald-Jensen

To install packages without affecting anaconda's configuration you can use pip from within IDLE:

要在不影响 anaconda 配置的情况下安装软件包,您可以在 IDLE 中使用pip

import pip
pip.main(["install","numpy"])

Although because IDLE can be a little slow with refresh rate (at least it is on my mac) it can be a great speed boost to hide the output until the end:

虽然因为 IDLE 的刷新率可能有点慢(至少在我的 mac 上是这样),但隐藏输出直到结束可能会大大提高速度:

import sys
import pip
import io

stdout_real = sys.stdout
sys.stdout = io.StringIO()
try:
    pip.main(["install","kfksnaf"])
finally:
    stdout_real.write(sys.stdout.getvalue())
    sys.stdout = stdout_real

note that this means that all standard output will be displayed after the error text which might be confusing if something goes wrong so do try it normally first and only do this if it lags badly.

请注意,这意味着所有标准输出都将显示在错误文本之后,如果出现问题,这可能会令人困惑,因此请先正常尝试,只有在严重滞后时才这样做。

On the other hand, it seems like anaconda has commandeered a lot of the functionalities of the python installed from python.org, to reduce it's impact on your machine you should take a look at Use Default Python Rather than Anaconda Installation When Called from the Terminalalthough this might then break functionalities of anaconda which may then in turn make it difficult to switch back if you want to do so.

另一方面,似乎 anaconda 已经征用了从 python.org 安装的 python 的许多功能,为了减少它对你的机器的影响,你应该看看使用默认的 Python 而不是 Anaconda Installation When Called from the Terminal尽管这可能会破坏 anaconda 的功能,如果您想这样做,这反过来又会使其难以切换回来。

回答by Divyanshu mehta

I was getting error

我收到错误

import numpy as npa

将 numpy 导入为 npa

Traceback (most recent call last): File "", line 1, in import numpy as np ModuleNotFoundError: No module named 'numpy'

回溯(最近一次调用):文件“”,第 1 行,在 import numpy as np ModuleNotFoundError 中:没有名为“numpy”的模块

I went to below path from cmd (admin) C:\Users\\AppData\Local\Programs\Python\Python38-32\Scripts

我从 cmd (admin) C:\Users\\AppData\Local\Programs\Python\Python38-32\Scripts 转到以下路径

And then ran command :

然后运行命令:

pip install numpy

pip 安装 numpy

this solves my problem. You can also run below command in order to upgrade pip python -m pip install --upgrade pip

这解决了我的问题。您也可以运行以下命令来升级 pip python -m pip install --upgrade pip

After installing i can see "f2py.exe" under C:\Users\\AppData\Local\Programs\Python\Python38-32\Scripts

安装后我可以在 C:\Users\\AppData\Local\Programs\Python\Python38-32\Scripts 下看到“f2py.exe”