Run module as script breaks import

I am trying to create a Python program with the structure shown at the end.

The problem is that actions should also be executed as scripts (I already included the main one in them). When I try to execute DummyAction.py , the import keeps complaining, they cannot find misc .

How can I use DummyAction.py as a script and still use functions in utils.py ?

DummyAction.py contains a class called DummyActionClass , and the same for DummyTrigger.py . There are several functions in utils.py that are used by both the action and the triggers, while MMD.py contains the main one.

 /MMD β”œβ”€β”€ __init__.py β”œβ”€β”€ MMD.py β”œβ”€β”€ /actions β”‚  β”œβ”€β”€ __init__.py β”‚  β”œβ”€β”€ DummyAction.py β”œβ”€β”€ /misc β”‚  β”œβ”€β”€ __init__.py β”‚  β”œβ”€β”€ utils.py └── /triggers β”œβ”€β”€ DummyTrigger.py └── __init__.py 

Import into DummyAction.py and DummyTrigger.py :

 from misc import utils 

And the error:

 File "DDM/actions/DummyAction.py", line 11, in <module> from misc import utils ImportError: No module named misc 
+1
source share
3 answers

Seeing the updated question, I think the problem is that you should do the import, including the root of your dependency tree: MMD.

Therefore, they should look like this:

 from MMD.misc import utils 

And also you need to call python with the -m option:

 python -m MMD.actions.DummyAction 

Edit: You said that MMD.py contains the main thing, but cannot be your executable file, but because it is a module (located inside the directory with the __init__.py file). MMD is similar to your library, so you need the executable to be outside and use that library.

You can find [here] some guidelines for organizing your project.

If you can change the structure of the project, I suggest doing it as follows:

 MMD/ β”œβ”€β”€ runner.py └── mmd β”œβ”€β”€ __init__.py β”œβ”€β”€ main.py β”œβ”€β”€ /actions β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ DummyAction.py β”œβ”€β”€ /misc β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ utils.py └── /triggers β”œβ”€β”€ DummyTrigger.py └── __init__.py 

Then, in any file inside the MMD directory, each import should start with MMD , for example:

 from mmd.misc import utils from mmd.actions import DummyActions 

And you put your main code, which is now inside MMD.py inside the Main class in main.py , with something like:

 # main.py from mmd.misc import utils class Main: def start_session(self): utils.function() # etc ... 

And then in runner.py you do something like:

 # runner.py from mmd.main import Main cli = Main() cli.start_session() 

So, inside the MMD directory that calls python runner.py , you have to execute your code, and you can also make the runner.py , so just ./runner.py will run your code.
And run your module with:

 python -m MMD.actions.DummyAction 

I would do it this way because this method is open to future implementation (and almost looks like linear guides).

If instead you cannot, try removing __init__.py from the MMD directory.

+1
source

I assume this is the directory structure you are talking about, in which case python does not know where to look for utils.py - it tries a local directory, then refuses several places in the path. It is simple enough to change the path:

 import sys sys.path.append("/MMD/misc") import utils 

and you should be away.

0
source

I found "workarround"

 try: #When executing from the main from misc import utils except: #When executing as a standalone script from MMD.misc import utils 

What allow:

  • Call the module as a script (when using other modules): python -m MMD.actions.DummyAction
  • Call the main program and use the module: python MMD/MMD.py

Although I'm not sure if it uses the attempt correctly - except for the block with the import, so feel free to add comments or other solutions.

Complete solution:

MMD.main

 from misc import utils from actions import DummyAction class MMD(): def __init__(self): a = DummyAction.DummyActionClass() utils.foo() if __name__ == '__main__': d = MMD() 

Then in actions/DummyAction.py :

 try: #When executing from the main from misc import utils except: #When executing as a standalone script from MMD.misc import utils class DummyActionClass(): def __init__(self): utils.foo() if __name__ == '__main__': a = DummyActionClass() 

Finally, in misc/utils.py :

 def foo(): print "Foo was called" 
0
source

Source: https://habr.com/ru/post/907723/


All Articles