Alex Flint Alex Flint - 3 months ago 17
Python Question

Create a PyCharm configuration that runs a module a la "python -m foo"

My python entrypoint needs to be run as a module (not a script), as in:

python -m foo.bar


The following does not work (and is not supposed to):

python foo/bar.py


How can I create a run confirguration in pycharm that runs my code using the first invokation above?

Answer

According to man python, the -m option

-m module-name
Searches sys.path for the named module and runs the corresponding .py file as a script.

So most of the time you can just right-click on bar.py in the Project tool window and select Run bar.

If you really need to use the -m option, then specify it as an Interpreter option, with the module name as the Script in the Edit Configurations dialog:

enter image description here