10

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

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
SUPhys
  • 221
  • 1
  • 5
  • 8
  • are you sure you are using IDLE for version 2.7? – Tadhg McDonald-Jensen Apr 01 '16 at 01:35
  • look like you have more that one python instaled, you need run the one where you install numpy on, or maybe is some mix up with the environment variables – Copperfield Apr 01 '16 at 02:29
  • @TadhgMcDonald-Jensen an AssertionError is raised – SUPhys Apr 01 '16 at 03:03
  • 'which python' and see what python you are running. It is likely that you have more than two versions of python installed and the python version you are using is not the one with numpy installed. – Hun Apr 01 '16 at 04:40
  • @SUPhys, if you are using IDLE for a different version of python (like 3.5) you just need to install numpy for that version with `pip3 install numpy` but if you are using IDLE for 2.7 then maybe try `pip uninstall numpy` then install it again `pip install numpy`. – Tadhg McDonald-Jensen Apr 01 '16 at 12:03
  • @Sung I have python 2.7.10 on my cpu, but when I run which python in the bash shell, I get this: /Users/Admin/anaconda/bin/python – SUPhys Apr 01 '16 at 16:10

3 Answers3

17

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.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
6

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

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

In later versions this is no longer directly exposed, since doing this in production code is bad. but you can still import the internals to install a module.

from pip._internal.main import main as pip_main
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:

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 Terminal although this might then break functionalities of anaconda which may then in turn make it difficult to switch back if you want to do so.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
1

I was getting error

import numpy as npa

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

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

And then ran command :

pip install numpy

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

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

Divyanshu mehta
  • 319
  • 4
  • 8