[英]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
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位的
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit ( AMD64 )] on win32
sys.path
), and import numpy
picks the 32bit version from testEnv , which obviously fails sys.path
),然后import numpy
从testEnv中选择32位版本,这显然会失败 To get rid of this error you can either (listing some of the possible options): 要消除此错误,您可以(列出一些可能的选项):
C:\\Dropbox (CEP)\\venvs>python
) C:\\Dropbox (CEP)\\venvs>python
)外部使用32位 Python 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 numpy
或conda uninstall numpy
(如果使用Anaconda
)*重新启动VS代码*瞧! (Reinstall numpy if the problem persists) (如果问题仍然存在,请重新安装numpy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.