Python - Module Not Found
Solution 1
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
.
Solution 2
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>"
Solution 3
you need a file named __init__.py
(two underscores on each side) in every folder in the hierarchy, so one in src/
and one in model/
. This is what python looks for to know that it should access a particular folder. The files are meant to contain initialization instructions but even if you create them empty this will solve it.
Solution 4
You need to make sure the module is installed for all versions of python
You can check to see if a module is installed for python by running:
pip uninstall moduleName
If it is installed, it will ask you if you want to delete it or not. My issue was that it was installed for python, but not for python3. To check to see if a module is installed for python3, run:
python3 -m pip uninstall moduleName
After doing this, if you find that a module is not installed for one or both versions, use these two commands to install the module.
- pip install moduleName
- python3 -m pip install moduleName
Solution 5
It's easier if you use this code
python3 -m module.sub_module
For example:
python3 -m entrypoint.settings
user962206
Updated on July 05, 2022Comments
-
user962206 almost 2 years
I am a beginner with Python. Before I start, here's my Python folder structure
-project ----src ------model --------order.py ------hello-world.py
Under
src
I have a folder namedmodel
which has a Python file calledorder.py
which contents follow:class SellOrder(object): def __init__(self,genericName,brandName): self.genericName = genericName self.brandName = brandName
Next my
hello-world.py
is inside thesrc
folder, one level aboveorder.py
:import model.order.SellOrder order = SellOrder("Test","Test") print order.brandName
Whenever I run
python hello-world.py
it results in the errorTraceback (most recent call last): File "hello-world.py", line 1, in <module> import model.order.SellOrder ImportError: No module named model.order.SellOrder
Is there anything I missed?
-
user962206 almost 8 yearswhat should be the contents if init.py?
-
Padraic Cunningham almost 8 years
-
miraculixx almost 8 years
__init__.py
can be empty or can contain code. it is common for projects to expose their primary / public classes at the package level. In your case you could addfrom model.order import SellOrder
so that other code can dofrom project import Sellorder
instead offrom project.model.order import SellOrder
.
-
-
miraculixx almost 8 yearsalso it's considered good practice not to have a
src
directory. that makes sense too because if you import your code somewhere else you should be able toimport project.model
and notproject.src.model
. -
Timo over 5 yearsIn
src
,init.py
is not needed, see @Rafazz`s answer -
iyop45 almost 5 yearsThis is not what the question is asking
-
Verma Aman almost 5 yearsWell, this helped me. Those modules were working through PyCharm but when I ran my program through Terminal it showed
ModuleNotFoundError
and this solution fixed my problem. -
wovano over 3 yearsThat's why you should never use
#!/usr/bin/python
but#!/usr/bin/env python
(or#!/usr/bin/env python3
for Python3, or possibly#!/usr/bin/env python3.8
for a specific version). This will also work when using virtual environments, while the first form won't. -
Walker Sutton over 2 years@iyop45 damn, you're right
-
demberto over 2 yearsThank you so much for this :)
-
Guy about 2 yearsVery late to this thread, but I have the exact same structure as above, and using the solution above, I still can only run my program (
locust -f src\main.py ...
) if I usefrom src.module.submodule import main
. Even with init.py in place. Maybe mycwd
should be insidesrc
? -
Dekriel almost 2 yearsthis doesn't seem to be working. I have
__init__.py
in every folder but it still cannot be imported. Pycharm didn't show up with any errors, but the terminal kept showing up errors. -
Eduardo Reis almost 2 yearsFor some reason it works when I run the python file. But when executing the same commands in the interactive mode, it does not work. Very intriguing.