Python produces: OSError: [WinError 193] %1 is not a valid Win32 application, but only with activate_this.py

Question:

This is presumably the same as Python produces: OSError: [WinError 193] %1 is not a valid Win32 application However, that has no answers, and I have additional details for my situation.

Background:

I’m using a venv, it gets activated internally with activate_this.py via:

exec(compile(open(venv_script, "rb").read(), venv_script, 'exec'), dict(__file__=venv_script))

This worked on python2 at least…

when I import numpy I get:

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Dropbox (CEP)venvswinCYANLibsite-packagesnumpy__init__.py", line 142, in <module>
    from . import core
  File "C:Dropbox (CEP)venvswinCYANLibsite-packagesnumpycore__init__.py", line 23, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:Python37libctypes__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

If I activate the venv normally, I can import numpy fine, so I’m guessing the problem is how I’m using activate_this.py

Minimal case:

C:Dropbox (CEP)venvs>virtualenv testEnv
Using base prefix 'c:\users\brianp\appdata\local\programs\python\python37-32'
New python executable in C:DROPBO~1venvstestEnvScriptspython.exe
Installing setuptools, pip, wheel...
done.

C:Dropbox (CEP)venvs>testEnvScriptsactivate

(testEnv) C:Dropbox (CEP)venvs>pip install numpy
Collecting numpy
  Using cached https://files.pythonhosted.org/packages/61/be/b4d697563d4a211596a350414a87612204a8bb987c4c1b34598cd4904f55/numpy-1.16.2-cp37-cp37m-win32.whl
Installing collected packages: numpy
Successfully installed numpy-1.16.2

(testEnv) C:Dropbox (CEP)venvs>deactivate
C:Dropbox (CEP)venvs>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> this_file = 'testenv/Scripts/activate_this.py'
>>> exec(open(this_file).read(), {'__file__': this_file})
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Dropbox (CEP)venvstestenvLibsite-packagesnumpy__init__.py", line 142, in <module>
    from . import core
  File "C:Dropbox (CEP)venvstestenvLibsite-packagesnumpycore__init__.py", line 23, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:Python37libctypes__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
>>> exit()

C:Dropbox (CEP)venvs>testEnvScriptsactivate

(testEnv) C:Dropbox (CEP)venvs>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
Asked By: Brian Postow

||

Answers:

This is a well known error: it’s an architecture mismatch 032bit / 064bit (pc032 / pc064), in your case trying to load a 032bit .dll in a 064bit process. To make things clear, NumPy contains a bunch of .dlls that get loaded in the current process when importing it.
It’s actually a duplicate of [SO]: Python Ctypes – loading dll throws OSError: [WinError 193] %1 is not a valid Win32 application (@CristiFati’s answer), but I’m going to detail.

The examples in the question are twisted and hard to read: some example that works, then some that doesn’t then some that works again and so on (for example I don’t even know what’s the 2nd snippet purpose), instead of clearly separating scenarios that do work from those that don’t.

Anyway in spite of the above, I was able to identify the problem.

  • The testEnv environment that you created (and installed NumPy in) is pc032:

    • 3rd snippet (begin):

      Using base prefix
      'c:\users\brianp\appdata\local\programs\python\python37-32'
      
    • 3rd snippet (end):

      Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
      
    • In this case, import numpy works (and NumPy (and the .dlls that it contains) is pc032)

  • The Python interpreter launched from outside testEnv is pc064:

    • 3rd snippet (mid):

      Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
      
    • When running testEnv‘s activate_this.py in the current process, it adds testEnv paths to %PYTHONPATH% (sys.path), and import numpy picks the pc032 version from testEnv, which obviously fails

To get rid of this error you can either (listing some of the possible options):

  1. Use a pc032 Python from outside VEnv (C:Dropbox (CEP)venvspython)

  2. The other way around: create a testEnv64 VEnv, and use its activate_this.py

  3. Don’t use activate_this.py at all, unless you know what you’re doing (I’d recommend this one)

Might also want to check [SO]: PyCharm doesn’t recognize installed module (@CristiFati’s answer).

Answered By: CristiFati

Another thing might have happened. VS code automatically searches for the numpy and other packages from predefined OS locations. It might have found out 32 bit version of numpy instead of a 64 bit version.
Fix:
Uninstall numpy from all OS locations
* In VS code terminal. Type pip uninstall numpy or conda uninstall numpy (If you use Anaconda)
* Restart VS code
* Voila! (Reinstall numpy if the problem persists)

Answered By: Saad Ibn Rahman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.