Skip to content
Advertisement

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:

JavaScript

This worked on python2 at least…

when I import numpy I get:

JavaScript

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:

JavaScript

Advertisement

Answer

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

      JavaScript
    • 3rd snippet (end):

      JavaScript
    • 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):

      JavaScript
    • 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).

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement