简体   繁体   English

如何在Python IDLE中使用Numpy?

[英]How do I use Numpy in Python IDLE?

Variations of this question have been asked, but the answers always start from scratch (ie from installing numpy). 已经提出了这个问题的变化,但答案总是从头开始(即从安装numpy)。

I already have numpy installed on my computer from an earlier time when I downloaded Anaconda. 我从早些时候下载Anaconda时已经在我的电脑上安装了numpy。

If I try to install numpy again using pip install numpy , I get a long error like, the end of which looks like 如果我尝试使用pip install numpy install numpy再次pip install numpy ,我会得到一个很长的错误,其结尾看起来像

Command C:\Python27\python.exe -c "import setuptools, tokenize;__file__='c:\\users\\imray~1\\appdata\\local\\temp\\pip_build_Imray\\numpy\\setu
p.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\imray~1\appd
ata\local\temp\pip-smduhf-record\install-record.txt --single-version-externally-managed --compile failed with error code 1 in c:\users\imray~1\appdat
a\local\temp\pip_build_Imray\numpy
Storing debug log for failure in C:\Users\Imray\pip\pip.log

numpy works fine when I open cmd from inside the Anaconda folder and command import numpy . 当我从Anaconda文件夹中打开cmd并命令import numpy时,numpy工作正常。 But it won't work from IDLE even if I navigate to said folder (via os.chdir('C:\\Anaconda') ) and issue the same command. 但即使我导航到所述文件夹(通过os.chdir('C:\\Anaconda') )并发出相同的命令,它也无法从IDLE起作用。

How do I get numpy to work in IDLE? 我如何在IDLE中工作?

Firstly, you might already know that Anaconda comes with its own free IDE, very similar to IDLE in many respects. 首先,您可能已经知道Anaconda有自己的免费IDE,在许多方面与IDLE非常相似。 It's known as Spyder, and should be accessible in any terminal as: spyder . 它被称为Spyder,应该可以在任何终端访问: spyder You could stop reading at this point and use that. 你可以在这一点上停止阅读并使用它。

But if you really want to use IDLE, you'll need to track it down first. 但如果你真的想使用IDLE,你需要先跟踪它。 It's bundled with each distribution of Python that you have installed on your system. 它与您在系统上安装的每个Python发行版捆绑在一起。 For example, I have a version of IDLE installed at the following location: 例如,我在以下位置安装了IDLE版本:

/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/idlelib/idle.pyw

If I run the Python distribution that this copy of IDLE belongs to, I can't access NumPy, because I've never installed it in that distribution: 如果我运行这个IDLE副本所属的Python发行版,我无法访问NumPy,因为我从未在该发行版中安装它:

python3
...
>>> import numpy as np
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'

I do have NumPy installed in my Canopy version of Python though (Canopy is very similar to Anaconda). 我确实在我的Canopy版本的Python中安装了NumPy(Canopy与Anaconda非常相似)。

python
...
>>> import numpy as np
>>>

The workaround I can do to get NumPy in the console is this: 我可以在控制台中获取NumPy的解决方法是:

python /usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/idlelib/idle.pyw

I'm executing the Canopy distribution of Python, which does have NumPy installed, and calling the other Python distribution's IDLE as you would any script. 我正在执行安装了NumPy的Python的Canopy发行版,并像调用任何脚本一样调用其他Python发行版的IDLE。 Then the IDLE console pops up and does allow me to import and use NumPy. 然后IDLE控制台弹出, 允许我导入和使用NumPy的。

This is bit of a workaround and I have found it to be hit-and-miss. 这是一个解决方法,我发现它是命中注定。 When I use Canopy's Python to open the IDLE belonging to yet another distribution of Python (Python 2.7 installed through Homebrew), I sometimes get the following error when using the print statement: 当我使用Canopy的Python打开属于另一个Python发行版(通过Homebrew安装的Python 2.7)的IDLE时,我在使用print语句时有时会出现以下错误:

Unknown object id: 'console'

So just be aware that you could run into issues like that. 所以请注意,你可能遇到类似的问题。

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

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