Python: Submodules Not Found - python

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.

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.

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.

How to import my python file in another file in visual studio code

I am using python/selenium in visual studio code. I am trying to import my another python class driverScript which resides in executionEngine module and in the file DriverScript. I have imported as below:
import driverScript from executionEngine.DriverScript
It is producing error:
Traceback (most recent call last):
File "c:/Selenium/Selenium-Python Framework/RK_Practice/Tests/mainTest.py", line 5, in <module>
from executionEngine.DriverScript import driverScript
ModuleNotFoundError: No module named 'executionEngine'
How can I import correctly? Your help is very much appreciated.
If the script you are wishing to import is not in the current directory, you may want to take this approach:
import sys
sys.path.insert(1, '/path/to/script/folder')
import driverScript from executionEngine.DriverScript
If your python file is on the same level in dir, then you can import just by calling:
import filename
If your python file is inside another folder, then you need to create a blank __init__.py file that will help python to understand it's a package. Then you can import that as follows:
from folderName import filename
Depends on where executionEngine is supposed to come from. If it's from a package installable via a package manager such as pip or Anaconda, looks like it's not properly installed. If you installed it yourself, you probably need to add the directory containing executionEngine to your PYTHONPATH, so the Python interpreter can find it. This can be done in the VSCode environment files. See the PYTHONPATH section in https://code.visualstudio.com/docs/python/environments

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.

Importing in Python failing

I am following the instructions on this blog post, to convert from Mercurial to Git
When I run the script as such:
hg-fast-export.sh -r c:\projects\demoapp
Then it fails with the following error:
Traceback (most recent call last):
File "./hg-fast-export.py", line 11, in <module>
from mercurial import node
ImportError: cannot import name node
And the begining of my hg-fast-export.py looks like this
#!/usr/bin/env python
# Copyright (c) 2007, 2008 Rocco Rutte <pdmef#gmx.net> and others.
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
import sys
# import mercurial libraries from zip:
sys.path.append(r'C:\Program Files (x86)\Mercurial\library.zip')
from mercurial import node
from hg2git import setup_repo,fixup_user,get_branch,get_changeset
from hg2git import load_cache,save_cache,get_git_sha1,set_default_branch,set_origin_name
from optparse import OptionParser
import re
import os
I checked the library.zip file (which is located in C:\Program Files (x86)\Mercurial\, and it contains the following folder structure (in addition to many other files/folders insize library.zip
library.zip
|
---------mercurial
|
----------node.pyc
I am really stumped. I do not know what to do. I have been stuck on this for two days. It maybe something very simple that I am overlooking, but I have not idea what it is. Is it a caching issue? Is it a setup issue? Is it an environment issue?
Please helpl, and thanks :)
You almost certainly have another mercurial package or module in your path somewhere. Since you use sys.path.append() the library.zip file is searched last for the module.
Your best bet is to add library zipfile to the Python module search path at the start:
sys.path.insert(0, r'C:\Program Files (x86)\Mercurial\library.zip')
If this is a package, have you tried placing an __init__.py file? This would make sure that files from your subdirectories can be found. While you would have to change a little bit of code (especially in your import statements), this seems like it should be the way to go.

Categories

Resources