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>"
Python error "ImportError: No module named" - Stack Overflow
Why do I get a ModuleNotFoundError?
"No module named X found"
ModuleNotFoundError: No module named
Based on your comments to orip's answer, I guess this is what happened:
- You edited __init__.py on Windows.
- The Windows editor added something nonprinting, perhaps a carriage-return (end-of-line in Windows is CR/LF; in Unix it is LF only), or perhaps a Ctrl + Z (windows end-of-file).
- You used WinSCP to copy the file to your Unix box.
- WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
- The missing __init__.py file (now called __init__.py.bin) means Python doesn't understand toolkit as a package.
- You create the __init__.py file in the appropriate directory and everything works...?
I ran into something very similar when I did this exercise in LPTHW; I could never get Python to recognise that I had files in the directory I was calling from. But I was able to get it to work in the end. What I did, and what I recommend, is to try this:
(NOTE: From your initial post, I am assuming you are using an *NIX-based machine and are running things from the command line, so this advice is tailored to that. Since I run Ubuntu, this is what I did)
Change directory (cd) to the directory above the directory where your files are. In this case, you're trying to run the
mountain.pyfile, and trying to call thetoolkit.interface.pymodule, which are in separate directories. In this case, you would go to the directory that contains paths to both those files (or in other words, the closest directory that the paths of both those files share). Which in this case is thetoolkitdirectory.When you are in the
toolkitdirectory, enter this line of code on your command line:export PYTHONPATH=.This sets your PYTHONPATH to ".", which basically means that your PYTHONPATH will now look for any called files within the directory you are currently in, (and more to the point, in the sub-directory branches of the directory you are in. So it doesn't just look in your current directory, but in all the directories that are in your current directory).
After you've set your PYTHONPATH in the step above, run your module from your current directory (the
toolkitdirectory). Python should now find and load the modules you specified.
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 have been assigned a project and I need to do it using python. Now lately, I have been running into an error, "No module X found" in my VS code while I have already installed the modules via pip both using cmd and the terminal within VSC. I am running Python 3.12.2 (64-bit) interpreter. I have also edited the user/system paths to Scripts/ and Python312/ yet I still don't know what's causing the problem.
Also for some reason when I type: "python --version" In my cmd, I get a reply as "Python 3.9.13" which is a 2022 version and it's not even installed on my system so theres that.
Any help would be appreciated, thanks!