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

前端 未结 2 1042
花落未央
花落未央 2020-12-22 12:54

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 m

相关标签:
2条回答
  • 2020-12-22 13:22

    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)

    0 讨论(0)
  • 2020-12-22 13:31

    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:\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 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)
    0 讨论(0)
提交回复
热议问题