2

PyCharm allows customization of the Python console. By default it adds WORKING_DIR_AND_PYTHON_PATHS to the sys.path:

import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS,WORKING_DIR_AND_PYTHON_PATHS + '/..', FILE_DIR])

enter image description here

Is there any variable for the "current file directory" (not current working directory)? This is needed to make relative paths work to other modules in same directory.

5
  • What do you mean by variable, env variable or what?To get current file dir you can use os.path.dirname(os.path.realpath(__file__)) Commented Dec 7, 2020 at 20:25
  • i'll try that one Commented Dec 7, 2020 at 21:05
  • That does not work: sys.path.extend(['/Users/steve/git/deepteam',os.path.dirname(os.path.realpath(__file__))]) NameError: name '__file__' is not defined Commented Dec 9, 2020 at 18:31
  • You have to put that line into the file you are executing and not into the pycharm preferences Commented Dec 17, 2020 at 6:29
  • I don't want to change the source code. In that case I would rather change the run configuration. Commented Dec 17, 2020 at 8:47

1 Answer 1

2

This can be done (using option 2 below) but not using the GUI shown in the question (option 1). It must be noticed there are 2 very different ways to launch the Console inside PyCharm.

1. As shown in the question by going to File > Settings > Build, Execution, Deployment > Console > Python Console.

2. Or using the Run/Debug configuration at Run > Edit Configurations.

What the GUI does in the 2 cases is very different

1. In the first case, the IDE simply calls the OS shell with the Python interpreter as first argument and the path to the Console plugin as second argument.

C:\path_to_venv\Scripts\python.exe

"C:\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py"

--mode=client --port=12345

There is one single variable of IDE magic to this (see also the comments in this answer):

Console. Python Console

The WORKING_DIR_AND_PYTHON_PATHS variable is hardcoded in PyCharm. It displays two paths: the project root and the working directory.

This means the IDE does not expose any other "magic variable" that would allow to retrieve the file before or after the Interpreter/Console is being called. Neither Python nor the OS have any way of knowing at this point what file/module you want to use, the only way would be hardcoding the file path as an environment variable (but this doesn't solve anything, because you would have to change the path every time you change file.)

2. The second option, does allow to transparently pass the module/file you currently have opened in the editor when you call the Console.

Basically by creating a run/configuration "template" using a using the "FileDir macro" whenever you run the debugger on any module opened in the editor a "temporary configuration" is created for that module that allows to retrieve the macro value from sys.argv. In this case the file is chosen by the IDE on-the-fly and the macro passes the path along with it.

C:\path_to_venv\Scripts\python.exe

"C:\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevd.py"

--multiproc --qt-support=auto --client 127.0.0.1 --port 12345

--file C:/path_to_module/teste2.py C:\path_to_module

This 2nd option is how the Console is supposed to be used in PyCharm to get the functionality in the question, as shown in the screenshot.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

great details! I'll come back to this at the end of the month - am awarding/assuming correct for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.