What does ImportError mean in Python? - python

I try to import a module:
import cv
And I get the following error message:
ImportError: DLL load failed: The specified module could not be found.
But if I try to import a library that definitely does not exist, for example:
import blabla
I get:
ImportError: No module named blabla
So, I conclude the the cv library is not totally hidden. Python is able to see something. Does anybody know what Python is able to see and what is missing?
ADDED
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ADDED 2
In the directory that contains the cv library there is sub-directory (C:\OpenCV2.2\bin) with many *.dll files. So, I tried:
import sys
sys.path.append("C:\OpenCV2.2\bin")
and I still get the "dll load failed". Is there a way to find out which exactly "dll" file is missing. I mean, Python tries to find a specific dll file (let say cv.dll) and cannot find it?

In this particular case, "DLL load failed" is caused by using Python 2.6 with OpenCV 2.2. You should use Python 2.7, because cv.pyd is linked with python27.dll.

ImportError can be confusing because it can be thrown when the module you are trying to import tries to import something else and because all of the import code is written in C you don't always get a useful backtrace.
In this case it looks as though either cv itself is a DLL, or some module that it tries to import is a DLL. The DLL won't load because it depends on some other DLL that isn't present on your system.
If you can't easily see what dependency is missing you can try using Microsoft's 'depends' tool to find out.

Most probably Python finds the pure-python module cv, which cannot find a DLL it needs.

Related

DLL load failed, module not found?

I have a folder with a file "main.py" and a file "_test.pyd" (note .pyd). The file "main.py" looks like this:
import _test
I get the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import _test
ImportError: DLL load failed while importing _test: The specified module could not be found.
Why is this error coming up? Cheers.
Note: I was given this code by others, and it works for the original authors, so I'm not sure what's wrong with me/my machine.
Update: Running os.path.isfile('_test.pyd') returns True, so I don't think it's a problem with the path
You should append the path of the folder which contains the imported module before import.
Code:
import os
import sys
sys.path.append(os.path.realpath(os.path.dirname(__file__)))
import _test # noqa: E402
EDIT:
Other ideas:
Adding __init__.py file to the related director.
Checking the PyInit_foo() function in .pyd file.
If the Python finds the .pyd file, it will attempt to call PyInit_foo() to initialize it
Update Following posts from people experiencing similar issues, I tried downgrading my Python version (from 3.8.4rc1 to 3.5.4) and the import now works correctly. No clue why. I guess the .pyd file was written in that version of Python (I'm not the author of the file), but still I'm clueless as to what the exact origin of the problem is.
I've been through this error and what I found after a lot of investigation:-
issue was in Opencv==4.5.1 build from source with cuda and flag cuda_with_fast_math=on
I just rebuild OpenCV and disable
cuda_with_fast_math
and it works for me.

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

ImportError: No module named anorm

I got the below shown error. Can anyone please help me with a solution?
from common import anorm, getsize
Exception:
Traceback (most recent call last): File "<pyshell#10>", line 1, in <module>
from common import anorm, getsize
ImportError: cannot import name anorm
Sorry for necroposting, but for anyone who's stuck on this:
apparently, you're trying to run the opencv example somewhat like this one. You need to copy the common.py from the same directory as well and everything would be fine (no need to install common package which has nothing to do with this partucular problem).
The common module does not contain an importable item named anorm. Simple as that.
Some possible reasons why:
The common module was not installed correctly.
Misspelling of the name of the item to be imported.
A file named common.py exists in the current directory, which takes precedence over the real common module (wherever it is).
Circular imports (which don't appear to be the case here, given the error traceback).

Upgrading to Python 3.6.1 seems to have corrupted/broken modules

Good day,
I recently tried to upgrade to Python 3.6.1 by building from source, and I've had a few issues.The first of which had to do with the $PYTHONHOME environment variable, which I have now (seemingly) solved, whereas the second - and most persistent - has to do with me not being able to access many modules.
For instance, when I launch the python3 interpreter, I immediately encounter an error that states:
Traceback (most recent call last): File "/etc/pythonstart", line 7,
in
import readline ModuleNotFoundError: No module named 'readline'
Similar errors occur with other modules that are imported by other scripts, such as 'encodings'
ImportError: No module named 'encodings'
and '_socket'
ModuleNotFoundError: No module named '_socket'
I'm not used to building from source, so I just used the instructions in the readme file that comes with the Python 3.6.1 tar ball, without much understanding of what's going on. I performed this procedure over and over again, (hopefully that didn't add to the problems), and here's the latest error output from the make test command:
Traceback (most recent call last): File
"./Tools/scripts/run_tests.py", line 12, in
import test.support
File "/usr/local/lib/python3.6/test/support/init.py", line 15, in
import logging.handlers
File "/usr/local/lib/python3.6/logging/handlers.py", line 26, in
import logging, socket, os, pickle, struct, time, re
File "/usr/local/lib/python3.6/socket.py", line 49, in
import _socket ModuleNotFoundError: No module named '_socket'
Makefile:1018: recipe for target 'test' failed make: *** [test] Error 1
I've searched all over the web frantically and I can't seem to find anything that really captures my problem, as the many other problems I've seen often involve people not being able to access only one module, or the solutions provided are extremely specific and seem difficult to generalise from (such as solutions that make use of lengthy Debian-based commands). I sincerely apologise if there are general solutions to this problem that I just haven't found.
Additionally, when I try to launch the Python 2 interpreter, I just get this error (and it fails completely):
ImportError: No module named site
My system information:
OS: Opensuse Leap 42.2
Previous Python3 version: 3.4.5 Current
Python3 version: 3.6.1
Your help with this matter would be greatly, greatly appreciated!

Importing pyFileMaker complains about EXPAT library

I've just downloaded pyFileMaker. I copied the directory PyFileMaker into the dir Lib under Python31 directory, but when I simply include the module FMServer with this line:
from PyFileMaker import FMServer
I get this error
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from PyFileMaker import FMServer
File "C:\Python31\lib\PyFileMaker\__init__.py", line 17
print "Unable to load the EXPAT library. You need to have it installed"
I checked whether the module expat exists and, infact, it exists. How can I fix this?
Well, as far as I can see the error happened not in the import, but in the print statement. (I.e. there probably was an error in import too, but it was intercepted.) The problem is that PyFileMaker is written for Python 2.x (their site says 2.4 is the minimal version), while you're using Python 3, and v3 is not compatible with v2.x.
I'd suggest switching to Python v2.7 (which is the primary version at the moment anyway). If you really want to use v3, you might try to convert the file with the 2to3 converter, but there's no guarantee it's going to work.

Categories

Resources