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.
Related
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.
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.
I am pretty new to python, so I might be missing (and probably am) some critical part of the import system, but I can't for the life of me figure it out at this point. I have a directory structure as follows:
/
/always_run.py
/lib/__init__.py
/lib/data.py
/lib/config.py
File Internals (imports):
/always_run.py
from lib import data
/lib/data.py
from lib import config
yields
Traceback (most recent call last):
File ".\data.py", line 9, in <module>
from lib import config as configs
ModuleNotFoundError: No module named 'lib'
Note: I've also tried:
from . import config
yields:
Traceback (most recent call last):
File ".\data.py", line 9, in <module>
from . import config as configs
ImportError: cannot import name 'config'
Within data.py I also have:
if __name__ == "__main__":
print("loading myself")
Just to test, but it makes no difference, it never gets to that point
Normally I run my program as python always_run.py and the world is my oyster, but if I try to run data.py directly I always get import failures. (after first cding into the lib directory and running python .\data.py
Is what I am trying to do not possible without adding the local directory to the sys.path, like this(test, works):
import sys
local_path = os.path.dirname(os.path.realpath(__file__))
if local_path not in sys.path:
sys.path.append(local_path)
import config
In the process of writing this, I tested one further thing, cding to the parent directory and running python -m lib.data which works flawlessly. Now my question is one of curiosity, because I still can not find the answer otherwise. Is it not possible to run a local file that is part of a module from the local directory? Or must it be done from another directory?
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
I'm trying to import module from local path in Python2.7.10 Shell on Windows
I add local path to sys.path by:
import sys
sys.path.append('C:\download')
next I try to import by:
from download.program01 import *
but I've got this error:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
from download.program01 import *
ImportError: No module named download.program01
On Linux this code works fine.
Does someone know what is wrong?
If download is in your pythonpath, then you should import program01 directly.
Also, please don't import *; it makes things very hard to debug. Just do import program01.
put a file __init__.py in your download folder so that python knows it is a module and do sys.path.append('C:') instead.
If you want to keep just using path and not create a module file (the __init___.py) then just keep your code like that but import doing
import program01