简体   繁体   中英

Import Error when using Anaconda with Python launcher

I installed Anaconda (Anaconda3-2020.07-Windows-x86_64, python 3.8). Following the installation guide, I didn't add Anaconda to the PATH environment variable. After installation, it works well when using Anaconda Prompt to start python.

Problem occurs when I use Python Launcher to start python from CMD:

In CMD, type "py" to launch python(The Python Launcher is able to find Anaconda python). Then when I attempt to import numpy , I get the following error:

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:

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 . I checked sys.path under these two situations and they are exactly the same.

Why? What's the difference between the two situations?

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". The error log shows that it is caused by the failure of importing numpy and sqlite3, just like the above errors.

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM