6

I can install numpy or pandas, but I can't import them in cmd, jupyter notebook or sublime text. (However, I can install and import them in Pycharm).

I've already searched different forums/stackoverflow, seeking for an answer, but the most common causes like architecture mismatch (I use Python 32-bit) or spaces in paths seem not to be the issue here. I've tried reinstall Python, but it didn't solve the problem.

The error always points out at "ctypes" lib and it seems to be a problem with numpy module specifically. When I try to import pandas/matplotlib it fails with importing numpy.

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jadwi\AppData\Roaming\Python\Python37\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Users\jadwi\AppData\Roaming\Python\Python37\site-packages\numpy\core\__init__.py", line 23, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Users\jadwi\AppData\Local\Programs\Python\Python37-32\lib\ctypes\__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

>>> help('numpy')
problem in numpy - OSError: [WinError 193] %1 is not a valid Win32 application 

>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jadwi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\__init__.py", line 13, in <module>
    __import__(dependency)
  File "C:\Users\jadwi\AppData\Roaming\Python\Python37\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Users\jadwi\AppData\Roaming\Python\Python37\site-packages\numpy\core\__init__.py", line 23, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Users\jadwi\AppData\Local\Programs\Python\Python37-32\lib\ctypes\__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

I know people had this error before, but, as I mentioned, it seems not to be an architecture mismatch problem, there are no spaces in the paths and I'm really stuck with it. Any suggestions on how to solve it will be very appreciated.

KamilaW
  • 199
  • 1
  • 1
  • 11
  • 1
    With 90% certainty or better: It *is* an architecture mismatch problem. – BoarGules Jun 23 '19 at 12:53
  • You're probably right. In most of the posts I saw about this problem it was pretty visible that someone used e.g. Python 64-bit and tried to use a module for 32-bit In my case I can't detect where lies the problem. I have two laptops, which are both running on 64-bit Win 10, they both use the exact same Python version and yet, on one numpy works without a problem and on the other one I get this OSError. – KamilaW Jun 23 '19 at 14:09
  • You wrong-footed yourself by assuming that your Python installations were automatically both 64-bit because of the platform and so identical. But 32-bit Python will run on a 64-bit system. And then the extensions in that installation all have to be 32-bit also. – BoarGules Jun 23 '19 at 15:13
  • Actually, it's not the case. I know that I can run both Python 32-bit and 64-bit on my laptops and I actually installed 32-bit on both of them, not 64-bit. – KamilaW Jun 23 '19 at 16:38
  • I never said you couldn't. I did say that if you run 32-bit Python, that Python must have 32-bit `numpy` installed. If you run 64-bit Python, that Python must have 64-bit `numpy` installed. (And the same applies to any CPython extension `.pyd`.) You cannot take shortcuts with this by just copying the files. The reason is that a 32-bit Windows process cannot load a 64-bit DLL, and a 64-bit Windows process will not load a 32-bit DLL. – BoarGules Jun 23 '19 at 16:51

4 Answers4

9

Okay, so this eventually helped me:

  1. I uninstalled only a numpy module with "pip uninstall numpy",

  2. I reinstalled it once again with "pip install numpy",

KamilaW
  • 199
  • 1
  • 1
  • 11
2

You can try this, I solved my problem after update package.

pip install --upgrade numpy

if nothing changed, you can upgrade pip first, than upgrade numpy.

python -m pip install --upgrade pip 

(if you want to update all package, please refer to this page:How to upgrade all Python packages with pip?)

Or check the python version is fit your environment or not. Please make sure you remove old version totally after you reinstall new version.

Mina chen
  • 1,604
  • 12
  • 13
1

I'm using PyCharm, determined to learn the PipEnv virtualization tool(it utilizes SHA256 hashing against downloaded packages), and was experiencing this same exact error message (error 193, %1 not a valid win32 or whatever). I realize this isn't exactly your scenario however, you're using python and PyCharm so I figure it might and might help others who arrive here. Anyways...

The way I resolved it was by: 1.) in the 'Add Python Interpreter' window (first setting up virtualization interpreter), I had to put "C:\Users\ericm\AppData\Roaming\Python\Python38\Scripts\pipenv.exe" as my "Pipenv executable'. (After doing this, I began getting a permission error when trying to utilize it) 2.) I had to add my project's path into the "Working directory" (e.x. C:\Users\ericm\OneDrive\Documents\Programming\Python\NameOfProject)

No more errors of any kind and my app is fully working again.

Eric Milliot-Martinez
  • 4,076
  • 3
  • 23
  • 28
0

Reinstalling numpy did not work for me.

I had the same issue and noticed multiple paths pointing to numpy in the error message. E.g.

C:\Users\USERNAME\AppData\Roaming\Python\Python37\*
C:\Users\USERNAME\AppData\Local\Programs\Python\Python37-32\*

I deleted the C:\Users\USERNAME\AppData\Roaming\Python\Python37\* folder because that was left behind by a previous Python which I had uninstalled. I would not recommend deleting anything until you are certain you won't break other things, so maybe send it to the recycle bin and restore if it does not fix the issue for you.

kym
  • 818
  • 7
  • 12