Wednesday, May 2, 2012

Installing NumPy for Python 3 in Windows 7

It's time to do some scientific computing, which, in the Python world, means using NumPy. I'm in a Windows 7 (64-bit) environment running Python 3.2.3 (64-bit).

Getting NumPy installed for Python 2 or Python 3 in Ubuntu was easy. Getting it to work in Windows turned out to be more tricky.

The Short Takeaway

  • In a Windows 7 environment (even a 64-bit Windows 7 environment), you must install the 32-bit version of Python 3. The 64-bit version will not work with NumPy 1.6. 
  • Furthermore, the 32-bit version of Python 3 must be installed 'just for me', and not 'for everyone on this computer'. 
  • Finally, make sure you select the proper NumPy version (for Python 3.2), not the default version from SourceForge (which is for Python 2.6).
In this post I'm assuming you have already installed Python 3 and that you're running Windows 7. Specifically, I'm running Windows 7 Professional, 64-bit, Service Pack 1. What follows is the whole story of the troubleshooting, in case it helps out anyone else having the same issues.

Step 1: Ensure that NumPy Isn't Already Installed

Open a Python prompt and ensure that you don't already have NumPy installed:

>>> from numpy import *
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    from numpy import *
ImportError: No module named numpy

Step 2: Download NumPy from SourceForge

The SourceForge homepage for NumPy can be found at http://sourceforge.net/projects/numpy/. From there find the latest version of NumPy for Windows, and download it to your default download location. For me as write this, that means downloading NumPy 1.6.1. Note that the default SourceForge download link pointed to a version of NumPy compiled for Python 2.6. I had to navigate a bit deeper to find a version for Python 3.

Step 3: Open the installation file

Double-click the file from wherever you downloaded it to to start the installation wizard. It's trustworthy open-source software, so it's safe to click through all the prompts and allow it to be installed.


This leads us to a crucial error:

It reads: Python version 3.2 required, which was not found in the registry. Yikes. I definitely have Python 3.2, but it's telling me it looked in the Windows registry and couldn't find it. I heard somewhere that installing Python for 'just this user (me)' instead of 'for all users of this computer' is one way to get around this.

Step 4: Reinstall Python 'just for me'

So to comply, I uninstall Python, and reinstall it, being careful to install it 'just for me' as shown below.


Step 5: Try installing NumPy again

With Python 3.2.3(64-bit) reinstalled properly, I try the NumPy installer again. This time it finds Python in the registry (presumably), and installs NumPy 1.6 without issue. Now test it out.

Step 6: Test out NumPy

To make sure it installed correctly, go into the Python interpreter and try importing NumPy:
from numpy import *
This returns the following mess:
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    from numpy import *
  File "C:\Python32\lib\site-packages\numpy\__init__.py", line 137, in <module>
    from . import add_newdocs
  File "C:\Python32\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>
    from numpy.lib import add_newdoc
  File "C:\Python32\lib\site-packages\numpy\lib\__init__.py", line 4, in <module>
    from .type_check import *
  File "C:\Python32\lib\site-packages\numpy\lib\type_check.py", line 8, in <module>
    import numpy.core.numeric as _nx
  File "C:\Python32\lib\site-packages\numpy\core\__init__.py", line 5, in <module>
    from . import multiarray
ImportError: DLL load failed: %1 is not a valid Win32 application

The last line is telling: "not a valid Win32 application." Some of the online forums seem to suggest that it could be a problem with 64-bit Python. Here's the exact version I'm running: "Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] on win32".

 Step 7: Uninstall 64-bit Python, install 32-bit Python

So I'm going to uninstall NumPy and uninstall this 64-bit version of Python 3.2.3, and in its place install a 32-bit version of Python 3.2.3. Again, be careful to install 'just for me.' When this is done, I have this version installed: "Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32". Now try NumPy again.

Step 8: Try installing NumPy again

Using the same NumPy binary as every time before, re-install it. To re-cap, I'm installing this in the context of a 32-bit Python 3.2.3 installation. I get no errors from the installation of NumPy, so it's time to test it.

Step 9: Test NumPy

One more time, in the Python interpreter, try importing NumPy:
>>> from numpy import *
This time it returns nothing, meaning it worked! Try creating a NumPy array, and see if it returns the proper type:
type(array([1,6,3,7]))
<class 'numpy.ndarray'>
That worked too, which means our task of installing NumPy for Python 3 in Windows 7 has been completed.

Conclusion

Here are the condensed steps for getting NumPy to work with Python 3 in Windows 7
  1. Regardless of whether you have a 32-bit or a 64-bit operating system, install the 32-bit version of Python 3.2
  2. Make sure you have installed Python 3.2 'just for me', and not 'for all users of this computer'.
  3. Make sure you download the correct version of NumPy from SourceForge, not the default that it offers as the latest version (which is for Python 2.6 instead of Python 3)

32 comments:

  1. Thank you very much. This was an extremely useful post!

    ReplyDelete
  2. Also very grateful for your guidance!

    ReplyDelete
  3. I've spent a few days fighting this. Thanks for taking the time to help us all out!

    ReplyDelete
  4. thank so much
    this exactly helped me a lot

    ReplyDelete
    Replies
    1. this also works with Python 3.3 and numpy-1.7.0-win32-superpack-python3.3.exe

      Delete
  5. Very useful post!
    Thank you so much for the walkthrough =D

    ReplyDelete
  6. Thanks a lot, saved me a lot of time and effort!

    ReplyDelete
  7. Thanks for the post. However, you don't need to reinstall python. The problem is that Python registry entry is located in: HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.2, while installer looks for it in here: HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.2. What you can do is export HKEY_LOCAL_MACHINE-like registry entry into text file, replace HKEY_LOCAL_MACHINE with HKEY_CURRENT_USER and add this new entry to registry (regedit.exe text_file.reg). Cheers!

    ReplyDelete
  8. You can also install Numpy from here:

    http://www.lfd.uci.edu/~gohlke/pythonlibs/

    ReplyDelete
  9. I was able to install the 64bit version from Shaunak's link (numpy-MKL-1.7.1.win-amd64-py3.3.exe) onto python 3.3.2 [MSC v.1600 64 bit (AMD64)] with no issues.

    I also used Valentina Zadrija registry hack.

    Thanks All!

    ReplyDelete
  10. thank you men, I love you so much¡...

    ReplyDelete
  11. Thanks indeed. Saved me a lot of time :)

    ReplyDelete
  12. Thanks for the post. Saved a lot of time.

    ReplyDelete
  13. Thank you. This post is very useful for me.

    ReplyDelete
  14. Hi—

    I just downloaded numy1.8 and python 3.4.0. I tried to install numpy 1.8 but I kept getting an error message ‘you need python 2.7”. Since there seems to be weekly or monthly python updates and so is numyp. Which are the latest compatible versions of numpy and python?

    Thanks, EK Esawi

    ReplyDelete
    Replies
    1. Most likely you just DLed and installed default version that was proposed by SourceForge, which was for Python 2.7.

      Delete
  15. great work, saved me a lot of time.

    ReplyDelete
  16. Thank you, Andrew! You are a real life saver! It seems that there's a lot of space for improvement for Python...

    ReplyDelete
  17. the most useful and detailed post i ever read on web ;)
    thank you

    ReplyDelete
  18. I had this problem trying to install numpy on python 3.4. I tried downloading the 32 bit version, and it worked, without having to re-install python.

    ReplyDelete
  19. Could I this for other package as SciPy and MatPlot?
    Thank you!

    ReplyDelete
  20. I had this problem using easy_install and pip with Windows 7; 64 bit Python 3.4. Lots of errors during install. Instead I simply:
    1) Grabbed the numpy-1.8.2.zip
    2) extracted and put the resulting numpy-1.8.2 folder under python\Lib
    3) navigated into that folder and ran python setup.py install

    and it worked!

    ReplyDelete