ImportError: No module named - python

i'm going to feel like an absolute idiot for this, and i don't know if it's because it's late Friday, but i'm having a hard time understanding why i'm having an issue with this incredibly simple code.
Directory structure:
~/testapp/
~/testapp/__init__.py
~/testapp/settings.py
~/testapp/workers/a.py
~/testapp/settings.py:
x = 1
~/testapp/workers/a.py:
from settings import x
when running ~/testapp/workers/a.py through PyCharm, it runs fine. however when running in a terminal, i get:
m#G750JW:~$ python testapp/workers/a.py
Traceback (most recent call last):
File "testapp/workers/a.py", line 1, in <module>
from settings import x
ImportError: No module named settings
i have also tried the following in ~/testapp/workers/a.py:
from testapp.settings import x
and got the same error. i've also tried:
from ..settings import x
but this will return:
m#G750JW:~/$ python testapp/workers/a.py
Traceback (most recent call last):
File "python testapp/workers/a.py", line 1, in <module>
from ..settings import testvar
ValueError: Attempted relative import in non-package
i've ran a lot of apps previously that used this same import method, and have never had a problem. i'm not sure why all of a sudden this is happening.
when looking at other issues like this on stackoverflow and google, everyone mentions setting and checking system paths, which i have done. as mentioned, running this through PyCharm works fine. if i change ~/testapp/workers/a.py to print sys.path, sys.executable, and os.getcwd() before the import, the results in PyCharm and the console are the same.

The issue is related, as you say, to paths... When you execute your code, the interpreter has a list of places to look for the things you are importing. In this case, it doesn't know about your project's root directory.
To solve the problem, set the environment variable PYTHONPATH to your project's root directory. Something like this:
export PYTHONPATH=~/testapp

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.

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.

I can't import a module in Python/Windows

I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.

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.

python "ImportError: cannot import name urandom"

Somehow my python is broken and emits the error:
jseidel#EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.
I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.
How do I go about finding the error and fixing python?
As suggested by #Armin Rigo, this worked for me:
1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.
2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.
3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.

Categories

Resources