2

Suppose there is a module somewhere, which I can import with

from sound.effects import echo

How can I run echo directly from command line?

Command

python sound/effects/echo.py

does not work since the relative path is generally incorrect

Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

7

If the module has top-level code executing on import, you can use the -m switch to run it from the command line (using Python attribute notation):

python -m sound.effect.echo

The module is then executed as a script, so a if __name__ == '__main__': guard will pass. See for example the timeit module, which executes the timeit.main() function when run from the command line like this.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343