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.
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>"
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?
Visual Studio Python Module Not Found
module suddenly not found
python - How do I troubleshoot "ModuleNotFound" errors - Stack Overflow
Module Not Found -- Importing My Own Modules
If you get the ModuleNotFoundError when trying to import a module of your own, then the problem is one of three things:
The module is in the wrong location. Fix this error by moving it to the correct location.
The import is incorrectly written to look in the wrong location. Fix this error by writing the import correctly.
You expected the module search path to contain the module, but it does not. Fix this by updating the module search path, typically by adding one or more directories to
sys.pathor by defining aPYTHONPATHenvironment variable. (Usually the other two options are preferred. It's unusual that you would need to do this.)
Three potential problems that might cause it:
Most times, It might be not having a src/ folder in your projects, it is advisable you do if you don't, You should also have a project dependency management system, I would recommend poetry.
Secondly, python interpreter, if you use a unix machine/mac you can cd to your env/folder, where python lives -> after you run env-command to create a virtual env, it helps point installed modules to where python lives. You should also practice using full path/ as against relative import paths , which might give attempted relative import with no known parent package.
run:
export PYTHONPATH=${PYTHONPATH}:${env/bin/python}/If you don't have a main.py file in your folder and you want to run a module or file using the
if __name__ == "__main__"to check if your code works, you can run this command on your terminalpython3 -m src.module.file_name
NB: In most cases, if you print(__package__), you get None. That is because you don't have a parent package or a main.py file