简体   繁体   中英

Python error: OSError: [WinError 193] %1 is not a valid Win32 application

After using Anaconda Navigator to update some packages (numpy, matplotlib, ...) and Spyder; my code using these packages no longer works and gives error on import.

Looking forward to a better understanding and the resolving suggestion...

While using the IPython console and just typing below:

In [1]: import numpy

This gives the following output:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-5-d4cdadb62aa7> in <module>
----> 1 import numpy

~\AppData\Roaming\Python\Python37\site-packages\numpy\__init__.py in <module>
    138
    139     # Allow distributors to run custom init code
--> 140     from . import _distributor_init
    141
    142     from . import core

~\AppData\Roaming\Python\Python37\site-packages\numpy\_distributor_init.py in <module>
     24                 # NOTE: would it change behavior to load ALL
     25                 # DLLs at this path vs. the name restriction?
---> 26                 WinDLL(os.path.abspath(filename))
     27                 DLL_filenames.append(filename)
     28     if len(DLL_filenames) > 1:

E:\Programs\Anaconda\lib\ctypes\__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error)
    362
    363         if handle is None:
--> 364             self._handle = _dlopen(self._name, mode)
    365         else:
    366             self._handle = handle

OSError: [WinError 193] %1 is not a valid Win32 application

After a couple of nights, the following worked for me (hopefully it may be useful to others having the same problem):

In an Ipython environment, typing:

In [1]: import numpy

gave the error as shown above. So I did the following:

In [2]: pip uninstall numpy

getting some lines of code and confirming that numpy is being removed. After that I tried:

In [3]: import numpy

No errors and everything seems to work, (Did not do any install in between.) As if during upgrading some packages before, a double version of numpy was being installed and causing the error log as shown above.

Exactly the same procedure worked for Matplotlib as well (uninstall and not reinstalling) made it work. Hope this is helpful.

It happened to me, but unfortunately all the solutions present out there couldn't be of much help. The possible outcome of this issue is that your python environment is super messed up. Although I couldn't resolve this error because possibly I will have to uninstall python from my system, clear caches etc., But to quickly work on the task in hand, I was able to find a workaround

The workaround is:

pip install conda

Create a conda environment with your required python version. eg:

$ conda create --name conda_virtualenv python=3.6

If you are working in Pycharm or VScode, then manually select this new python interpreter and start working(now you will be able to install the required libraries)

If you are working in terminal then activate this environment $ conda activate conda_virtualenv and do whatever you want. If anyone has better workaround, please suggest here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM