Pycharm Current Working Directory

How to set working directory for projects in PyCharm?

I have Pycharm 4.5, so things might have changed a bit.

Try going to Settings > Project > Project Structure

On this dialog, click your folder that has the source code in it, and then click the blue folder in the menu to note it as "source" folder. I believe this fixes a lot of the path issues in Pycharm

Here is the link to "content roots": https://www.jetbrains.com/pycharm/help/content-root.html

Add current file directory to Python interpreter path in PyCharm

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.

Sample Image

Show current directory in the Project Tool Window

In Spyder for example the Files tab is updated every time you cd.

No, PyCharm doesn't have this functionality in the Project Tool Window.

after changing directory (os.chdir)

The question here is where do you execute os.chdir? The above mentioned Project Tool Window isn't the Terminal, the Console, nor the Editor window (or one of the Run/Debug windows). The Project Tool Window does work inherently as a File Watcher, meaning if you change (e.g. delete, create, rename) files the window refreshes automatically to reflect those changes.

But the Project Tool Window is not meant to reflect Python code execution (which os.chdir is: a command in a running Python interpreter. Notice that the Project Tool Window is meant to work regardless of having a Python interpreter installed or running.)

Is there a way to set this?

The closest functionality PyCharm provides is configuring costum scopes and setting those as the View of the Project Tool Window. Choose Edit Scopes and if your aim is having a number of predefined locations the Scope Configuration Controls lets you set them beforehand.

Other than that, PyCharm has chosen a philosophy that for a number of functionalities like the one you're asking about it's better using the Operating System's file viewer or listing files in the terminal.

To configure a custom scope in the Project Tool window follow these steps

1. Create the custom scope

create the custom scope

2. Set the custom scope as the View in the Project Tool Window

set the custom scope as the view in the Project Tool Window

Change working directory of console in PyCharm

Settings → Build Execution Deployment → Console → Python Console

Working directory error

I finally solved the problem.

I think it all started because the first project that I opened with pycharm was in my "download" folder, so the working directory was automatically set to a temporal folder by default and allthough I moved the project to another folder and I manually changed the working directory from the terminal, it was not working.

The solution was creating a new project and giving a correct path to the new project. It seems very easy but it was not that obvious.

PyCharm tells me Cannot start process, the working directory ... does not exist

After testing for a bit, I've found a solution (but not an answer to why this error occurs in PyCharm):

Delete the file and create it again. (Or rename or move it and create a new file with its old name, both should work.)

Pycharm $python3 manage.py runserver issue

Did you check are you executing this command in proper directory?

If your manage.py file is stored in directory 'app' you should open terminal inside this directory and execute this command.

best regards,



Related Topics



Leave a reply



Submit