简体   繁体   English

Python产生:OSError:[WinError 193]%1不是有效的Win32应用程序,仅带有activate_this.py

[英]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. 大概与Python产生的结果相同:OSError:[WinError 193]%1不是有效的Win32应用程序但是,这没有答案,我还针对我的情况提供了其他详细信息。

Background: 背景:

I'm using a venv, it gets activated internally with activate_this.py via: 我正在使用venv,它通过以下方式在内部使用activate_this.py

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

This worked on python2 at least... 这至少在python2上起作用...

when I import numpy I get: 当我导入numpy时,我得到:

>>> 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 ... 如果我正常激活venv,我可以导入numpy,所以我猜问题是我如何使用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
>>>

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. 这是一个众所周知的错误:这是体系结构不匹配( 32bit / 64bit ),在您的情况下,您试图在64bit进程中加载32bit .dll To make things clear, numpy contains a bunch of .dll s that get loaded in the current process when importing it. 为了清楚起见numpy包含一堆.dll ,它们在导入时会在当前进程中加载​​。

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 2 nd 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 : 您创建(并在其中安装了numpy )的testEnv环境是32bit

    • 3 rd snippet (begin): 第3个摘要 (开始):

      Using base prefix 'c:\\users\\brianp\\appdata\\local\\programs\\python\\python37- 32 '

    • 3 rd 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 .dll s that it contains) is 32bit ) 在这种情况下, import numpy可以工作(并且numpy (及其包含的.dll )是32bit
  • The Python interpreter launched from outside testEnv is 64bit 外部 testEnv启动的Python解释器是64位的

    • 3 rd 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 在当前进程中运行testEnvactivate_this.py时,它将testEnv路径添加到%PYTHONPATH%sys.path ),然后import numpytestEnv中选择32位版本,这显然会失败

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 ) VEnvC:\\Dropbox (CEP)\\venvs>python )外部使用32位 Python
  • The other way around: create a testEnv64 VEnv , and use its activate_this.py 另一种方法是:创建一个testEnv64 VEnv ,并使用其activate_this.py
  • Don't use activate_this.py at all, unless you know what you're doing (I'd recommend this one) 除非您知道自己在做什么,否则不要使用activate_this.py (我建议您这样做)

Another thing might have happened. 可能发生了另一件事。 VS code automatically searches for the numpy and other packages from predefined OS locations. VS code从预定义的OS位置自动搜索numpy和其他软件包。 It might have found out 32 bit version of numpy instead of a 64 bit version. 它可能发现了numpy 32 bit版本,而不是64 bit版本。 Fix: Uninstall numpy from all OS locations * In VS code terminal . 修复:从所有OS位置卸载numpy *在VS code terminal Type pip uninstall numpy or conda uninstall numpy (If you use Anaconda ) * Restart VS code * Voila! 输入pip uninstall numpyconda uninstall numpy (如果使用Anaconda )*重新启动VS代码*瞧! (Reinstall numpy if the problem persists) (如果问题仍然存在,请重新安装numpy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSError: [WinError 193] %1 不是 Python 中的有效 Win32 应用程序 - OSError: [WinError 193] %1 is not a valid Win32 application in python OSError: [WinError 193] %1 不是有效的 Win32 应用程序 - python - OSError: [WinError 193] %1 is not a valid Win32 application - python OSError: [WinError 193] %1 不是有效的 Win32 应用程序 Python - OSError: [WinError 193] %1 is not a valid Win32 application Python Python 错误:OSError: [WinError 193] %1 不是有效的 Win32 应用程序 - Python error: OSError: [WinError 193] %1 is not a valid Win32 application OSError: [WinError 193] %1 不是有效的 Win32 应用程序 (Pandas) - OSError: [WinError 193] %1 is not a valid Win32 application (Pandas) OSError: [WinError 193] %1 不是有效的 Win32 应用程序 - OSError: [WinError 193] %1 is not a valid Win32 application OSError: [WinError 193] %1 不是 CTypes 中的有效 Win32 应用程序 - OSError: [WinError 193] %1 is not a valid Win32 application in CTypes OSError: [WinError 193] %1 不是有效的 Win32 应用程序 - nltk - OSError: [WinError 193] %1 is not a valid Win32 application - nltk OSError: [WinError 193] %1 不是有效的 Win32 应用程序 (LibTiff) - OSError: [WinError 193] %1 is not a valid Win32 application (LibTiff) 如何修复“OSError: [WinError 193] %1 不是有效的 Win32 应用程序” - How to fix"OSError: [WinError 193] %1 is not a valid Win32 application"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM