DLL load failed, module not found? - python

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.

Related

python relative import from same package fails

I am working on a project involving the popular aiortc/aioquic github package. Located at src/aioquic/buffer.py there is an import statement:
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
This line is meant to import a C/stub module _buffer.c / _buffer.pyi . When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails. I have tried reading several other questions on python relative imports but none seem to make sense to me.
To reproduce the issue:
open python in the src directory
from aioquic import about (works)
from aioquic import buffer (fail)
from aioquic import buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'
I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails. I thought I understood roughly how to use python imports but apparently not. What would cause the same line to work when running from one directoy but fail when running from another directory? Is it something magical with the PYTHONPATH? I'm hoping someone smarter than I am can explain why this is failing.
[EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. I have tried several different combinations but none seem to work.]
Thanks in advance.
I figured out what was wrong.
The stub/C module had to be built into a .pyd file before it could imported. This was done by
pip install -e .
on the parent directory
Is it something magical with the PYTHONPATH?
Yes.
Typically . dot (current working directory) will appear
in that exported env var,
so where you started matters.
Take care to ensure you're using the same env var setting,
and the same pwd,
during both the interactive and the Docker runs.

Python module not found when using MSIPred

I am very new to python. I am trying to use a pre-existing algorithm on my data:
https://github.com/wangc29/MSIpred
I downloaded the code from GitHub and used my command line installer as suggested on the first instruction of the vignette in the above link.
I save a python file in the same directory as the downloaded code and attempt to execute the first line:
import MSIpred as mp
I receive this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import MSIpred as mp
ImportError: No module named MSIpred
This is my directory organization. I have tried with both MSI_Smoker and MSI_Smoker2 as seen in the image below.
Any suggestions?
Edit:
In response to some comment, I tried moving the python script into the parent folder but I get the same error.
The easiest way is to add the lib path to the environment parameter PYTHONPATH(https://www.tutorialspoint.com/What-is-PYTHONPATH-environment-variable-in-Python)
export PYTHONPATH=$PYTHONPATH:`pwd` (replace `pwd` to the lib path)
link Python error "ImportError: No module named"

ImportError: cannot import name 'mailparser' from 'lib'

I downloaded a repository which other people are using. This file works for everyone else except for me so I believe there is a problem with my local setup.
There is a line from lib import mailparser which is causing the error:
Traceback (most recent call last):
File "parse.py", line 3, in <module>
from lib import mailparser
ImportError: cannot import name 'mailparser' from 'lib' (/usr/local/lib/python3.7/site-packages/lib/__init__.py)
There is 100% a file called 'mailparser' in the 'lib' directory but it isn't recognizing it.
From the error it seems to be looking in the usr/local/lib/python3.7/site-packages/lib which has to be wrong as the correct path is /Users/myname/Documents/Company Name/parser/lib/mailparser.py.
If that's where you've downloaded the lib package to, then you'll need to do
sys.path.insert(0, "<PATH-TO-PACKAGE>")
at the top of your file to get Python to look in there for it.
The problem here is that you have a package called lib that already exists in your site-packages folder, which is where Python looks for files to import. In the case of your colleagues, they have nothing, so Python falls-back to looking in the current working directory for something called lib. In your case, it finds this random lib and uses that. To avoid that, you tell Python to look at your current working dircetory first, by inserting it into the first position of your sys.path.
If the repository you've downloaded has a setup.py file, you might be expected to go into the repo and run pip install . to install from source. This will install the code into your site-packages.

Pycharm "no module named REPORT.py" however it exist and works. But then

Pycharm says REPORT.py file doesn't exist. However the code works perfectly.
Here is the image of the issue:
Now that is not the strangest part. I know sometimes when you working within a package you have to reference the package name: import package_name.filename so when I tried that it appears to have fixed the incorrect reporting.
But then...
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/REDACTED/MAIN/MAIN.py", line 2, in <module>
import MAIN.REPORTS as PDD
File "C:\Users\REDACTED\PycharmProjects\REDACTED\MAIN\MAIN.py", line 2, in <module>
import MAIN.REPORTS as PDD
ModuleNotFoundError: No module named 'MAIN.REPORTS'; 'MAIN' is not a package
This seams like a mistake with how Pycharm is checking for my file.
For the sake of completeness I also tried to import from.
Pycharm is also not marking this as invalid but I get a new interesting error when running the code:
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/REDACTED/MAIN/MAIN.py", line 2, in <module>
from MAIN import REPORTS as PDD
File "C:\Users\REDACTED\PycharmProjects\REDACTED\MAIN\MAIN.py", line 2, in <module>
from MAIN import REPORTS as PDD
ImportError: cannot import name 'REPORTS'
As request in the comments here is my folder structure:
Per suggestion in the comments I did try to import * but I still get the same reporting issue from Pycharm.
Update:
I believe I found out why the issue existed when trying to import from my package.
Due to my package name being MAIN and my main py file being MAIN.py I believe my code was trying to import from the py file and not the package.
After renaming my package to MAIN_PACK and doing import MAIN_PACK.REPORT as PDD the code works fine without any reporting from Pycharm saying it is not valid.
That at the vary least fixes the Pycharm reporting.
However that still does not explain why Pycharm reports that import REPORTS is not a module but still the code would work.Does anyone know why this is occurring?
Set the main folder as the source root. You can do that by right clicking the MAIN folder and navigating to the bottom of the list. Mark the directory as source root. Go into Pycharm settings under console and check add source roots to python path.
Pycharm uses source root to resolve imports. More information can be found here
https://www.jetbrains.com/help/pycharm/content-root.html
Relevant text from link:
Source roots (or source folders; shown as the Source root icon ).
These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
The files under the source roots are interpreted according to their type. PyCharm can parse, inspect, index, and compile the
contents of these roots.

What does ImportError mean in 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.

Categories

Resources