0

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)\venvs\win\CYAN\Lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Dropbox (CEP)\venvs\win\CYAN\Lib\site-packages\numpy\core\__init__.py", line 23, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Python37\lib\ctypes\__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~1\venvs\testEnv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.

C:\Dropbox (CEP)\venvs>testEnv\Scripts\activate

(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)\venvs\testenv\Lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import core
  File "C:\Dropbox (CEP)\venvs\testenv\Lib\site-packages\numpy\core\__init__.py", line 23, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Python37\lib\ctypes\__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>testEnv\Scripts\activate

(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
>>>
Brian Postow
  • 11,709
  • 17
  • 81
  • 125
  • It would help if you provided a [mcve]. – Ulrich Eckhardt Apr 17 '19 at 17:21
  • Add a `print(sys.executable)` before the error and paste the output – CristiFati Apr 17 '19 at 17:22
  • @UlrichEckhardt What would make it complete? It's pretty minimal... I open python, type the stuff in the big block, and get the error... – Brian Postow Apr 17 '19 at 17:31
  • "NameError: name 'venv_script' is not defined" -- Please read that link that was provided. People shouldn't have to guess what other parts look like or guess which parts are relevant. Preferably start from an empty directory where you set up your venv. Of course, provided setting up a venv is necessary at all, which is your job to find out before posting here. – Ulrich Eckhardt Apr 17 '19 at 17:34
  • I mean, it's a virtualenv with numpy. I included the entire script of creating the venv, and the thing happening. – Brian Postow Apr 17 '19 at 17:44
  • Added successful import after activating at the command line rather than inside python – Brian Postow Apr 17 '19 at 17:50

2 Answers2

1

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)\venvs\python)

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

CristiFati
  • 38,250
  • 9
  • 50
  • 87
0

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)

Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
  • "*Fix: Uninstall `numpy` from all OS locations*": that's **bad advice** (should be avoided) and may lead to messing up other environments, instead of focusing on solving the actual problem. – CristiFati Jan 29 '23 at 20:03