PyCharm. Getting the Project Dir in the "Run/Debug Configuration" Window

There are so called Macros in PyCharm which include $ProjectFileDir$ described as The directory of the project file. However it seems they can only be used in External Tools section and not in Run/Debug Configurations. However, there's very relevant issue titled Add support for macros in Run/Debug Configurations screen raised in another JetBrains' product — WebStorm. There, we find information that the path to any file underneath project's directory is stored relatively to the project's directory meaning that if the project is placed somewhere else in the filesystem the file would still be found. In other words paths you see are presented as absolute paths but they are in fact relative to the project's directory. I must admit it's confusing to say the least.

I created issue Add support for macros in run/debug configurations — feel free to vote on it.


As a silly workaround for the very specific but common case of running a main.py script in the project root, you can select to run by "Mondule name" instead of the default "Script path":

enter image description here

This works because the "Add source roots to PYTHONPATH" is ticked (default).

Tested in Pycharm CE 2019.2.3, Ubuntu 19.04.