Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run module from command line?

Tags:

python

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

like image 673
Dims Avatar asked Dec 19 '25 08:12

Dims


1 Answers

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.

like image 142
Martijn Pieters Avatar answered Dec 21 '25 22:12

Martijn Pieters