37

When I want to run one of our many python scripts I hit run, which creates a new run configuration. The working directory for that configuration is the folder in which the python script file resides. Instead, it should run from a project wide fixed folder. I'd like to set that somehow but I really cannot figure out why, which is a bit odd as I'd expect many people to want to have this functionality. Do you know how to pull this off?

ikku100
  • 809
  • 1
  • 7
  • 16

2 Answers2

43

If you hit the Edit configurations... button in the submenu appearing when clicking on your currently selected run configuration in the top right of PyCharm, you are able to edit the fields (and therefore also the working directory) of Python run configurations by selecting Python from the Templates (also called Defaults in versions pre-2018) and specifying the desired values.

Don't forget to hit Apply/OK before creating new run configurations, which should now contain the default values you specified!

As far as I know, these defaults are local to one project. See also: https://www.jetbrains.com/help/pycharm/2016.2/creating-and-editing-run-debug-configurations.html

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
mxscho
  • 1,990
  • 2
  • 16
  • 27
  • 3
    Interesting, I didn't know you could add defaults that way. However, if I do as you described and after saving the defaults/python config, and then go to a new file and hit 'run', then its working directory is still set to its path, and not the working directory of the default run configuration? – ikku100 Oct 18 '16 at 13:21
  • 5
    Sorry that I couldn't answer earlier. You probably mean the temporary configuration (that's how PyCharm's documentation is calling it) which is not updating itself. If you rightclick inside a file and hit `Debug`/`Run`, it will search all the existing temporary configurations for one matching the file and run/debug this one. It will not create a new temporary configuration when you rightclick like that. So if you want to have the default settings in these ones, you have to recreate the corresponding temporary configs (or delete them and auto-create them by rickclicking inside the file). – mxscho Oct 18 '16 at 15:34
  • 2
    Unlike that old documentation link's long text, the current way is to get to "Edit configuration templates" from the Edit Configurations window. – matanster Jul 07 '22 at 18:55
  • 1
    As is the answer is still outdated, could be nice to update it. – matanster Apr 18 '23 at 13:16
0

@mxscho's answer works. Here I list the steps how to set working directory for auto-detected test cases.

Click Edit Configurations.... The working directory is the directory which contains the current Python file by default.

enter image description here

Click Edit configuration templates.... In the open window, set working directory for Python tests -> autodetect. enter image description here

Afterwards, when you run a test case, the new created run configuration would inherit the working directory setting from the configuration template.

I am using PyCharm 2023.1.2 (Community Edition).

Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63