How do I use Numpy in Python IDLE?

ぃ、小莉子 提交于 2019-12-06 02:01:10

Firstly, you might already know that Anaconda comes with its own free IDE, very similar to IDLE in many respects. It's known as Spyder, and should be accessible in any terminal as: 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. It's bundled with each distribution of Python that you have installed on your system. For example, I have a version of IDLE installed at the following location:

/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:

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).

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

The workaround I can do to get NumPy in the console is this:

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. Then the IDLE console pops up and does allow me to import and use 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:

Unknown object id: 'console'

So just be aware that you could run into issues like that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!