62

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?

Alex Flint
  • 6,040
  • 8
  • 41
  • 80
  • 1
    Discussion on JetBrains support forum: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206603365-debug-a-run-configuration-with-a-package-module-instead-of-script – Anton Tarasenko Nov 04 '17 at 12:05
  • There is now [a better answer](https://stackoverflow.com/a/51268846/1953800) that would probably help more people if accepted. – Alex Feb 18 '19 at 16:59

5 Answers5

70

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

codeape
  • 97,830
  • 24
  • 159
  • 188
chugreevd
  • 848
  • 8
  • 5
44

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)

J. R. Petrus
  • 441
  • 1
  • 4
  • 4
  • 2
    Great trick, this also enables you to use the profiling option of pycharm ! – harmv Aug 16 '16 at 08:51
  • 1
    This still wouldn't work for me. I still got the error: couln't find module. Can it have something to do with the fact that I'm using Conda to manage environments (including PyCharm)? – E. Muuli Jul 26 '17 at 11:40
  • For PyCharm 2017.3.2 (Professional Edition), @andrewdotn's answer can work for `Run` but not for `Debug`. You answer can work for both `Run` and `Debug`. – Cloud Jan 02 '18 at 02:34
  • 2
    In my Ubuntu installation, the script is located there: ```./usr/lib/python*/runpy.py``` – slaadvak Apr 25 '18 at 13:40
  • What if I want to debug algo2.py? `C:\Python36\Scripts\pylivetrader.exe run algo2.py` – gseattle Apr 30 '19 at 07:20
31

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

andrewdotn
  • 32,721
  • 10
  • 101
  • 130
  • 3
    This doesn't work because I'm using "from __future__ import absolute_import" and then later I do "from . import foo". – Alex Flint Feb 27 '14 at 22:19
  • Oh, ok. I’ve updated the answer to handle that case. – andrewdotn Mar 02 '14 at 16:32
  • 1
    Any chance of running this from a remote interpreter? – dtheodor Feb 18 '15 at 16:42
  • 5
    That worked for running the module, but not when debugging it. If I click the debug icon instead of the run icon, I get the error No module found. – Waleed Abdulla Aug 05 '16 at 01:05
  • 1
    This works when just running the application. Profiling however fails with a "Import by filename is not supported." In order to get profiling to work you need the 'runpy.py' trick below – harmv Aug 16 '16 at 08:49
  • If you are using `__main__.py` as module file you may need to add it as part of the script (as in `foo.baz.__main__`) even if it's not needed with a regular `python -m` execution. – djromero Feb 15 '17 at 16:13
8

IntelliJ IDEA / PyCharm 2017

Field "Script" is optional in the recent versions of JetBrains IDEs. Specifying -m foo.bar in "Script parameters" is enough:

enter image description here

Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91
  • Thanks! That's a great update. This should be the new accepted answer. – Nate Glenn Nov 19 '17 at 14:19
  • 1
    Doesn't seem to work in PyCharm 2017.3.1 (Professional Edition), apparently because PyCharm actually calls `/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py ` with lots of options, including `--file XXX` which it sees as `--file -m` using your solution. – Codie CodeMonkey Jan 06 '18 at 23:14
4

In PyCharm 2016 specifying -m without a script path doesn't work, as they use a wrapper script that doesn't accept the -m argument.

Here's my solution, which works for run & debug configurations: https://github.com/amnong/misc/tree/master/pycharm_runner

Edit: I just read J. R. Petrus's comment, and my solution is very similar. BTW I also tried to support proper entry points using pkg_resources, but for some reason pkg_resources.load_entry_point() could not find my project's distribution...

Amnon Grossman
  • 567
  • 1
  • 5
  • 12