How to import / open numpy module for IDLE

I want to use numpy for a program that I have to execute, and I want to do this in the IDLE IDE. I installed the numpy binary from the internet, but when I try to run "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 tried using pip to install numpy, but when I run pip install numpy in a bash shell, it says

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

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

What to do to make numpy work in IDLE? Should I save it somewhere?

ps I am running OsX 10.10.5 Yosemite

+5
source share
2 answers

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

 import sys; print(sys.executable) 

By running this in IDLE on my windows machine, I get

 C:\Programs\Python36\pythonw.exe 

(The suffix w is a Windows-specific binary option for running graphical programs without a blank console window. It should be omitted in the future.)

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

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

On * nix, you may first need to run python -m ensurepip to set the pip itself for this python.

About import pip; pip.main import pip; pip.main : pip is designed as a command-line utility that initializes, performs one function, and exits. main () is the intentionally undocumented internal implementation detail. The author of pip does not recommend using it, since it is for one call, followed by the exit of the program. Several calls will not work correctly if the internal data is not synchronized with the installed files.

+10
source

To install packages without affecting the configuration of anaconda, you can use pip from IDLE :

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

Although IDLE can be a bit slow at refresh rates (at least this is on my Mac), it can be a big boost to hide the output to 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 

Please note that this means that after the error text appears, all standard output will be displayed, which can be confusing if something goes wrong, so first try it normally and do it only if it is far behind.

On the other hand, it seems that anaconda has launched many of the python functions installed from python.org, to reduce the impact on your machine, you should take a look at Use Python by default instead of installing Anaconda when called from the terminal , although this can lead to anaconda functionality , which in turn can make the transition difficult if you want to do it.

+3
source

Source: https://habr.com/ru/post/1246196/


All Articles