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.
Related
I am trying to import a python module from a certain folder. This is the code:
import sys
sys.path.append(r"C:\path\to\module_foobar")
import foobar
In a miniconda env with all prerequisites installed (python 3.10 and numpy), I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing foobar: The specified procedure could not be found.
But with the system python in cmd, the same code works.
Tried adding the path to system path variable as well as specifying it in a custom .pth file in the site-packages folder of the miniconda environment.
Tried running dependencywalker on the .pyd file of the package and it says that a bunch of MS C++ Redistributeable dll files seem to be missing. Tried repairing those to no avail.
What on earth could be wrong?
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.
I am writing my own package for a test with setup.py. I tried to install it on my own computer with python3 setup.py install, and there was no error. I checked with pip3 list and found it there,
steplib 0.1.0
However, when I tried importing it in a python interpreter, I could not,
>>> import steplib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'steplib'
Using help('modules') confirmed this as it was not shown in the list.
Am I doing anything wrong? Do I have to register it first before using it?
edit
My site packages contains steplib-0.1.0-py3.7.egg-info. It does not have any sub-packages (it is a test package):
steplib
__init__.py
steplib.py
setup.py
Ideally module should go into your site packages. Do you see it there?
If its there then check your package path. You package may be steplib, but have you checked if you are importing right package and module there. For example within your steplib folder you might have additional package and module within it. Say package is X and module is Y. Then you can import it as below.
from X import Y
Make sure you have init.py in your package to qualify that as a package.
Did you install the package in the python directory under Lib?
I'm new to python and trying to test a script from this github repo (https://github.com/mgp25/psn-api).
The root directory has an example.py and I'm trying to run it with
$ python example.py
which gives this error:
Traceback (most recent call last):
File "example.py", line 1, in <module>
from src.Auth import Auth
ImportError: No module named src.Auth
How can I get this to run?
There is a folder in the root directory named src but because I'm new to python I don't know how to connect things so that the src.Auth module gets imported (or if that's even the right terminology)
Python 3.3+ will happily interpret it as a package without an __init__.py, fwiw, and I believe that's what the author wrote in.
Also note from trying to run it just now, you'll need to install simplejson and requests. (Normally there'd be a requirements.txt or similar saying this.)
Being in the repository root directory, you do:
touch src/__init__.py
This will create an empty file but it is necessary for the Python module search system. Then you should be able to run it without problems, unless there is some dependency on external libraries.
My Python couldn't figure out the submodules when I was trying to import reportlab.graphics.shapes like this:
>>> from reportlab.graphics.shapes import Drawing
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
from reportlab.graphics.shapes import Drawing
ImportError: No module named shapes
I have copied the reportlab package to /site-packages and I can import module reportlab.graphics successfully.
My Python version is 2.7.3.
Could anyone help me to fix this problem?
As #dan-boa pointed out, you can add paths to the module search path, but since you can find the parent module, I doubt that this is your root problem.
Do you have some left-over installation of the module at another path? You can check the path where it is finding the parent package (reportlab) by executing:
import reportlab
print reportlab.__file__
If this is indeed the path you were expecting, then try this recursively with the the sub-modules, until you can see where the problem is. Perhaps, your package is corrupted? Try manually checking in the path returned if you can find the files/modules in question.
If this is not the path you were expecting, clean-up the installation from this 2nd path and try again.
Finally, in case you do find that it is a path problem, instead of adding the path each time using sys.path.append, you can add it to PYTHONPATH
Please check your sys path and if the directory of the module is not present then add it.
import sys
sys.path.append('PATH_OF_THE_MODULE')
As site-packages is already their in the sys.path, may be therefore the package was imported successfully.