Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pycharm

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?

like image 286
Alex Flint Avatar asked Feb 27 '14 21:02

Alex Flint


People also ask

How do I run a module in PyCharm?

Choose Run | Run from the main menu or press Alt+Shift+F10 , and then select the desired run/debug configuration. This way, you can run any available run/debug configuration.

How do I create a config file in PyCharm?

Alternatively, press Alt+Shift+F10 , then 0 . In the left-hand pane of the run/debug configuration dialog, click Edit configuration templates…. In the Run/Debug Configuration Templates dialog that opens, select a configuration type. Specify the desired default parameters and click OK to save the template.

What are run configurations in PyCharm?

You can run your tests (test cases, test suites, and so on) using run/debug configurations, in the way similar to running ordinary applications. PyCharm provides a framework for creating special run/debug configurations for testing purposes, where a test can be specified as a target.

Where does PyCharm store run configurations?

By default, it is disabled, and PyCharm stores run configuration settings in . idea/workspace. xml.


2 Answers

In 2018.1 it is finally possible to specify the module name instead of the script path in the UI. There is a dropdown for changing it, to the left of the input field.

PyCharm Run Configuration

like image 198
chugreevd Avatar answered Oct 15 '22 19:10

chugreevd


There is a workaround I use for my scripts, which do use relative imports.

python -m actually invokes a script called runpy.py which is part of a standard Python installation. These two invocations are equivalent:

python -m my_module.a.b module_arguments python python_lib_directory/runpy.py my_module.a.b module_arguments 

Use the latter method to setup your Run/Debug Configuration:

Script: python_lib_directory/runpy.py

Script parameters: my_module.a.b module_arguments

Interpreter options: (leave blank, no -m required)

like image 42
J. R. Petrus Avatar answered Oct 15 '22 19:10

J. R. Petrus