I’m trying to build a Python program with the “structure” shown at the end.
The problem is that actions should be able to be executed as scripts as well (I’ve already included a main in them). When I try to execute DummyAction.py imports keep complaining they can’t find misc.
How can I use DummyAction.py as a script and still use the functions in utils.py?
DummyAction.py contains a class called DummyActionClass, and the same for DummyTrigger.py. In utils.py there are several functions that both actions and triggers use and MMD.py contains the main.
/MMD
├── __init__.py
├── MMD.py
├── /actions
│ ├── __init__.py
│ ├── DummyAction.py
├── /misc
│ ├── __init__.py
│ ├── utils.py
└── /triggers
├── DummyTrigger.py
└── __init__.py
The import in DummyAction.py and DummyTrigger.py is:
from misc import utils
And the error is:
File "DDM/actions/DummyAction.py", line 11, in <module>
from misc import utils
ImportError: No module named misc
Seen the updated question, I think the problem is that you should do the import including the root of your dependecies tree: MMD.
So they should all look like:
And also you need to call
pythonwith the-moption:Edit: You said that
MMD.pycontains the main but it can’t be your executable, and that’s because is a module (is inside a directory with an__init__.pyfile).MMDis like your library so you need the executable to be outside and use such library.You can find [here] some guidelines on how to organize your project.
If you can change you project structure I’ll suggest to do it like this:
Then in any file inside the
mmddirectory every import should start withmmd, for example:And you put your main code that now is inside
MMD.pyinside aMainclass inmain.py, with something like:And then in
runner.pyyou do something like:This way inside the
MMDdirectory callingpython runner.pyyou would execute your code, and you can also make executablerunner.pyso a simply./runner.pywill run your code.And run your module with:
I’d do it like this, becasue this way is open to future implementation (and is almost like in the line guides).
If instead you can’t, then you can give it a try at removing
__init__.pyfrom theMMDdirectory.