How to import/open numpy module to IDLE

SUPhys picture SUPhys · Apr 1, 2016 · Viewed 50.3k times · Source

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

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 numpy in the bash shell, it says

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.

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

p.s. I am running OsX 10.10.5 Yosemite

Answer

Terry Jan Reedy picture Terry Jan Reedy · Nov 2, 2016

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:

import sys; print(sys.executable)

Running this in IDLE on my Windows machine, I get

C:\Programs\Python36\pythonw.exe

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

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:

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

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

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.