0

In a top folder I have a python file (helpers.py) which contains a single function:

def play(name, verbose=False):
        if verbose:
        print name + "with verbose on"
    else:
        print name + "verbose off"

And in a subfolder named project I have second python file (program.py) that imports and uses helpers.py:

from .. import helpers as hp

def main(device,verbose=False):
    hp.play(device)
    #here goes the code

if __name__ == "__main__":
    #Test the program
    main('Foo')
    main('Foo', verbose=True)

Both folders contain the __init__.py file to allow module importing. I want to execute the program.py file to test the main function. Following How to fix "Attempted relative import in non-package" even with __init__.py I try to run :

python -m top.project.program

getting

# /usr/bin/python: No module named top.project

I don't understand what I am doing wrong. Any help? thanks

Community
  • 1
  • 1
diegus
  • 1,168
  • 2
  • 26
  • 57

2 Answers2

0

-m doesn't indicate an import; it is expecting the path to the module you wish to execute. So it probably wants -m top/project/program.py (although you may need to provide an absolute path).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

The answer is in a comment of the accepted answer in the link I posted in my question. It matters from which directory you are executing your python command from. The code works when executing the following shell command:

python -m top.project.program 

From the top directory only. It is not working when executed from its subdirectory project.

diegus
  • 1,168
  • 2
  • 26
  • 57