Import Error when using Anaconda with Python launcher - python

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?
p.s.
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, e.g. 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, i.e. 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.

Related

ImportError "no pq wrapper available" when importing psycopg3

I installed pyscopg3 on my venv using pip install psycopg[binary] as per the documentation but I still get an import error:
Exception has occurred: ImportError
no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: DLL load failed while importing pq: The specified module could not be found.
- couldn't import psycopg 'python' implementation: libpq library not found
I'm running a Windows 10 machine. How can I solve this error?
You need to install the command line tools on PostgreSQL on your Windows machine.
Download the full server installer here: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
You don't need to install the full server package, only command line tools in the installer options will be necessary:
After the installation, you need to add the PostgreSQL bin folder in your PATH environnement variable:
hit +R at the same time to get command prompt. Then type sysdm.cpl, go to advanced and select "Environment Variables", in PATH add the path to :
C:\Program Files\PostgreSQL\13\bin\ folder (or whatever folder you choose to install the PostreSQL commande line tools).
IMPORTANT: do not forget to close and restart your dev environnement (ie: VSCode, PyCharm, ...) to take the new environnement variable into account.
Note : This answer is related to Windows machine. For Linux the installation of a at least one postgresql-client-<version> package will be enough.
related to: https://stackoverflow.com/a/60369228/5341247

Pycharm Error: DLL load failed while importing qhull: The specified module could not be found

I've been trying to run my code and it throws an Import Error that says DLL load failed while importing qhull: The specified module could not be found.
What could be wrong, and what can I do to fix this
Your question is too generic: there can be many reasons. My easiest and quick way to solve such class of problems (assuming you already checked the environment, and that you have relevant packages):
remove all python cache (.pyc files)
and if this doesn't solve the problem do:
copy the setting of your virtual environment (or conda environment)
delete the virtual environment (and then move or remove the remaining files)
create again the environment, with the packages you got from first point
Very often, with such procedures, you get again a working environment. It seems that on some updates, some files remain in the wrong place, and so the wrong version of a DLL is used, and it confuse python and windows, about inconsistent status.
Sometime you may have segmentation fault in Python program, alternate to failed to load DLL error.

ImportError: DLL load failed: The application has failed to start because its side-by-side configuration is incorrect

I downloaded a Python wrapper called rawpy recently using easy_install (pip install did not work). When I imported it and attempted to run code, this error appeared: "ImportError: DLL load failed: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail."
Could anyone offer a fix for this?
I looked at similar questions on SO and found that a common theme was that they had incorrect versions of whatever they were working with. I decided to switch to Python 3.5.3 over 2.7; this fixed the problem, but a new error "ImportError: No module named 'rawpy'" appeared. This is because the module is now in the incorrect directory. I was able to fix this by uninstalling and reinstalling the module using pip.

ImportError: No module named qgis.core ubuntu 16.04 python 2.7 qgis 2.16.2

I keep getting this error in python from trying to call qgis from a script.
The code is:
from qgis.core import *
from qgis.analysis import *
I have read every posting on SO about this; wiped QGIS and reinstalled. Reset my PYTHON_PATH and QGIS_PREFIX variables to the correct directory. I've also checked the dependencies via dpkg -l | grep qgisand all of my dependencies are the xenial version.
Any other suggestions?
I had the same problem but it was with Windows 7. Following the last point called Running Custom Applications in http://docs.qgis.org/2.8/en/docs/pyqgis_developer_cookbook/intro.html I solved it.
You will need to tell your system where to search for QGIS libraries and appropriate Python modules if they are not in a well-known location — otherwise Python will complain:
>>> import qgis.core
ImportError: No module named qgis.core
This can be fixed by setting the PYTHONPATH environment variable. In the following commands, qgispath should be replaced with your actual QGIS installation path:
on Linux: export PYTHONPATH=/qgispath/share/qgis/python
on Windows: set PYTHONPATH=c:\qgispath\python
The path to the PyQGIS modules is now known, however they depend on qgis_core and qgis_gui libraries (the Python modules serve only as wrappers). Path to these libraries is typically unknown for the operating system, so you get an import error again (the message might vary depending on the system):
>>> import qgis.core
ImportError: libqgis_core.so.1.5.0: cannot open shared object file: No such file or directory
Fix this by adding the directories where the QGIS libraries reside to search path of the dynamic linker:
on Linux: export LD_LIBRARY_PATH=/qgispath/lib
on Windows: set PATH=C:\qgispath;%PATH%
These commands can be put into a bootstrap script that will take care of the startup. When deploying custom applications using PyQGIS, there are usually two possibilities:
require user to install QGIS on his platform prior to installing your application. The application installer should look for default locations of QGIS libraries and allow user to set the path if not found. This approach has the advantage of being simpler, however it requires user to do more steps.
package QGIS together with your application. Releasing the application may be more challenging and the package will be larger, but the user will be saved from the burden of downloading and installing additional pieces of software.
The two deployment models can be mixed - deploy standalone application on Windows and Mac OS X, for Linux leave the installation of QGIS up to user and his package manager.
Finally got it working. Had to completely wipe and reinstall QGIS twice and separately remove python-qgis. Also had to uninstall anaconda. After the second fresh install of QGIS I've gotten it working.
No other changes to my configuration.

Python: Failed to import extension - Errno 2

I am having weird behaviors in my Python environment on Mac OS X Lion.
Apps like Sublime Text (based on Python) don't work (I initially thought it was an app bug),
and now, after I installed hg-git, I get the following error every time I lauch HG in term:
*** failed to import extension hggit from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-package/hggit/: [Errno 2] No such file
or directory
So it probably is a Python environment set up error. Libraries and packages are there in place.
Any idea how to fix it?
Notes:
I installed hg-git following hg-git web site directions.
I also added the exact path to the extension in my .hgrc file as: hggit = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-package/hggit/
Python was installed using official package on Python web site.
Echoing $PYTHONPATH in term print anything
"site-package"? Did you mean "site-packages"?

Categories