[英]Import Error when using Anaconda with Python launcher
I installed Anaconda (Anaconda3-2020.07-Windows-x86_64, python 3.8).我安装了 Anaconda(Anaconda3-2020.07-Windows-x86_64,python 3.8)。 Following the installation guide, I didn't add Anaconda to the PATH environment variable.
按照安装指南,我没有将 Anaconda 添加到 PATH 环境变量中。 After installation, it works well when using Anaconda Prompt to start python.
安装后,使用Anaconda Prompt启动python时效果很好。
Problem occurs when I use Python Launcher to start python from CMD:当我使用 Python Launcher 从 CMD 启动 python 时出现问题:
In CMD, type "py" to launch python(The Python Launcher is able to find Anaconda python).在 CMD 中,输入“py”启动 python(Python Launcher 能够找到 Anaconda python)。 Then when I attempt to import
numpy
, I get the following error:然后,当我尝试导入
numpy
,出现以下错误:
Original error was: DLL load failed: The specified module could not be found.
And when I attempt to import sqlite3
, I get the following error:当我尝试导入
sqlite3
,出现以下错误:
ImportError: DLL load failed while importing _sqlite3: %1 is not a valid Win32 application
If I use Anaconda Prompt to start python, I can suscessfully import numpy
and sqlite3
.如果我使用 Anaconda Prompt 启动 python,我可以成功导入
numpy
和sqlite3
。 I checked sys.path
under these two situations and they are exactly the same.我在这两种情况下检查了
sys.path
,它们完全相同。
Why?为什么? What's the difference between the two situations?
这两种情况有什么区别?
ps ps
I'm concerned with this problem because I built a website using "django + mod_wsgi + Apache", and when visiting the website from brwoser, the website got an "Internal Server Error".我很担心这个问题,因为我使用“django + mod_wsgi + Apache”构建了一个网站,当从 brwoser 访问该网站时,该网站出现“内部服务器错误”。 The error log shows that it is caused by the failure of importing numpy and sqlite3, just like the above errors.
错误日志显示是导入numpy和sqlite3失败导致的,和上面的错误一样。
What's the difference between the two situations?
这两种情况有什么区别?
The anaconda command prompt has added a bunch of paths to the PATH environment variable, eg for me it has added all of these locations: anaconda 命令提示符为 PATH 环境变量添加了一堆路径,例如对我来说,它添加了所有这些位置:
C:\ProgramData\Miniconda3
C:\ProgramData\Miniconda3\Library\mingw-w64\bin
C:\ProgramData\Miniconda3\Library\usr\bin
C:\ProgramData\Miniconda3\Library\bin
C:\ProgramData\Miniconda3\Scripts
C:\ProgramData\Miniconda3\bin
C:\ProgramData\Miniconda3\condabin
In some of these locations there are executables like python.exe
so that calling python
will launch the anaconda installed version.在其中一些位置有像
python.exe
这样的可执行文件,因此调用python
将启动 anaconda 安装版本。 Other folders in this list contain .dll files, ie libraries that your modules might depend on (like numpy or sqlite that are interfaces to functions written in c/c++).此列表中的其他文件夹包含 .dll 文件,即您的模块可能依赖的库(如 numpy 或 sqlite,它们是用 c/c++ 编写的函数的接口)。 So even if you are calling the right python.exe, if the PATH is not set to include the locations of the neccessary dlls, then importing such packages will fail, as you can see in your error messages:
因此,即使您正在调用正确的 python.exe,如果 PATH 未设置为包含必要 dll 的位置,则导入此类包将失败,如您在错误消息中所见:
Original error was: DLL load failed: The specified module could not be found.
What you can do:你可以做什么:
I am not an expert on configuring a django server, but by manually adding these locations to your PATH, you might be able to solve the issue.我不是配置 django 服务器的专家,但通过手动将这些位置添加到您的 PATH,您可能能够解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.