Question

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

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
>>>

Solution #1:

This is a well known error: it’s an architecture mismatch (32bit / 64bit), in your case trying to load a 32bit .dll in a 64bit process. To make things clear, numpy contains a bunch of .dlls that get loaded in the current process when importing it.

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 32bit:

    • 3rd snippet (begin):

      Using base prefix 'c:usersbrianpappdatalocalprogramspythonpython37-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 32bit)
  • The Python interpreter launched from outside testEnv is 64bit

    • 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 32bit version from testEnv, which obviously fails

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

  • Use a 32bit Python from outside VEnv (C:Dropbox (CEP)venvs>python)
  • The other way around: create a testEnv64 VEnv, and use its activate_this.py
  • Don’t use activate_this.py at all, unless you know what you’re doing (I’d recommend this one)
Respondent: Brian Postow

Solution #2:

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)

Respondent: CristiFati

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy