There are two possible problems here:
- make sure Project is on the python path, otherwise it cannot find code
- code is an internal module in the python standard library which might cause name clashes, see https://docs.python.org/2/library/code.html . To avoid this, change the folder name to src, or anything else but code.
There are two possible problems here:
- make sure Project is on the python path, otherwise it cannot find code
- code is an internal module in the python standard library which might cause name clashes, see https://docs.python.org/2/library/code.html . To avoid this, change the folder name to src, or anything else but code.
This is what relative imports are for.
from . import Z # use the class as Z.Z
from .Z import Z # use the class as Z
Detailed explanation on StackOverflow of the whole system.
python - No Module named utils.utils , utils is not a package - Stack Overflow
i have a problem with importing packages in python
python - ModuleNotFoundError: No module named utils - Stack Overflow
ModuleNotFoundError: No module named 'utils.json_util'; 'utils' is not a package
In my python script I wrote:
import package.module
package.module.b()
for some reason it cant find the package and also calls it a module in the error
ModuleNotFoundError: No module named 'package'
i checked the spelling and the names of the module and the package
if you explain this it would me a lot thanks!
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.
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>"