This is a very typical issue, so I will leave this here for future reference.
If I have a project called: my_project
.
βββ my_project
βββ first_folder
βββ second_folder
βββ third_folder
What you want to do is one of these two things:
- PYTHONPATH (only Python)
cd ~/my_project && export PYTHONPATH=$(pwd)
change dir to root dir, and export that dir to PYTHONPATH, so when Python runs it looks in the PYTHONPATH.
- PATH (everything)
cd ~/my_project && export PATH=$PATH:$(pwd)
Same as above!
This needed to live somewhere since it took me a while to figure out. So for anyone in the future who needs help with this!
Answer from DUDANF on Stack OverflowThis is a very typical issue, so I will leave this here for future reference.
If I have a project called: my_project
.
βββ my_project
βββ first_folder
βββ second_folder
βββ third_folder
What you want to do is one of these two things:
- PYTHONPATH (only Python)
cd ~/my_project && export PYTHONPATH=$(pwd)
change dir to root dir, and export that dir to PYTHONPATH, so when Python runs it looks in the PYTHONPATH.
- PATH (everything)
cd ~/my_project && export PATH=$PATH:$(pwd)
Same as above!
This needed to live somewhere since it took me a while to figure out. So for anyone in the future who needs help with this!
Check the python version you are calling. Run "python --version". If it's not 3.7.1, that's the problem. If it returns a version beginning with "2", then try running the python3 binary instead.
Python doesn't find local module although it is installed and its location exists in sys.path
Module not found but it's already installed
ModuleNotFoundError: No module named 'Directory_one'
python - Module not found - "No module named" - Stack Overflow
All modules in Python have to have a certain directory structure. You can find details here.
Create an empty file called __init__.py under the model directory, such that your directory structure would look something like that:
.
βββ project
βββ src
βββ hello-world.py
βββ model
βββ __init__.py
βββ order.py
Also in your hello-world.py file change the import statement to the following:
from model.order import SellOrder
That should fix it
P.S.: If you are placing your model directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path.
If it's your root module, just add it to PYTHONPATH (PyCharm usually does that):
export PYTHONPATH=$PYTHONPATH:<root module path>
For Docker:
ENV PYTHONPATH="${PYTHONPATH}:<root module path in container>"
Three possibilities come to my mind:
- The module just works for certain python versions (e.g. 3.5 but not 3.6).
- The path where your module is currently located does not appear in sys.path.
- The class you want to use must be specified while importing the module because it is not specified in the __init__.py of the module itself.
As far as I can tell, only the first option might cause your problem.
Nevertheless I would make a quick check with
import sys
sys.path
and in case that the correct path is really missing..
sys.path.append('PathToModule')
You will need to do a relative import (PEP-328), but according to this guide, you can only go up until the level where you launched the script from, which is the tests directory in you case.
So you'll either have to add the model directory to the path, or add the source folder of your project to a .pth file in your site-packages as shown here
Change your import in test.py to:
from .hello import hello1
Your code is fine, I suspect your problem is how you are launching it.
You need to launch python from your '2014_07_13_test' directory.
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
For instance:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
In test.py:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
And all should be well...
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
There is a better shebang you should use.
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
Hey Guys,
I get a weird ModuleNotFoundError in one of my projects and i do not really understand why. So here is my Folder Structure:
Project\
| main.py
| packageabc\
| | __init__.py
| | abc.py
| | packagexyz\
| | | xyz.py
In every .py file is an abc/xyz or main class. Now the interesting part. Inside the abc.py I do an import like this:
from packagexyz.xyz import xyz
This works perfectly fine!
Inside the main.py I do an import like this:
from packageabc.abcimport abc
Now I get an ModuleNotFoundError. Error Code looks like this:
from packagexyz.xyz import xyz ModuleNotFoundError: no module named packagexyz.
So the import in the abc.py File that works perfectly when tested inside the file does not work if the abc.py file is imported. So why is that?
I'm trying to build a "portable" Python package based on those available from https://github.com/indygreg/python-build-standalone/releases.
I've extracted this into /opt/staging/ansible and pip installed ansible without issue (the main purpose of this package will be to allow staging machines to run ansible-pull).
However, I also need the 'firewall' module installed, but after installing it, Python still fails to import it. I confirmed the module library locations:
root@b-test3 /opt/staging/ansible/bin # ./python3 -m 'site'
sys.path = [
'/opt/staging/ansible/bin',
'/opt/staging/ansible/lib/python310.zip',
'/opt/staging/ansible/lib/python3.10',
'/opt/staging/ansible/lib/python3.10/lib-dynload',
'/opt/staging/ansible/lib/python3.10/site-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: TrueI even went as far as to manually target the site-packages directory:
# ./pip3 install -Ut /opt/staging/ansible/lib/python3.10/site-packages/ firewall Collecting firewall Using cached firewall-0.2.0-py3-none-any.whl Collecting boto (from firewall) Using cached boto-2.49.0-py2.py3-none-any.whl (1.4 MB) Collecting ec2>=0.2 (from firewall) Using cached ec2-0.4.0-py2.py3-none-any.whl (6.2 kB) Installing collected packages: boto, ec2, firewall Successfully installed boto-2.49.0 ec2-0.4.0 firewall-0.2.0
But still...
root@b-test3 /opt/staging/ansible/bin # ./python3 Python 3.10.13 (main, Aug 26 2023, 07:12:19) [Clang 16.0.3 ] on linux Type "help", "copyright", "credits" or "license" for more information. Cannot read termcap database; using dumb terminal settings. >>> import firewall Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'firewall' >>>
I've checked all the usual suspects I can think of. This is an Oracle Linux 8 VM, so it's got Python3.6 by default. I can install firewall there and import it no problem.
In my Python 3.10 package, I can install other modules without issue (and without any tricks). Ansible installed fine and runs as expected. I also installed pandas as a test and could import it successfully.
Anyone got any clues why firewall isn't working?