R.G.
R.G.

Reputation: 861

"Cannot start process, the working directory ... does not exist" error in PyCharm

When I run my Main.py script, PyCharm keeps telling me Cannot start process, the working directory /home/myname/PyCharmProjects/MyProjectName/mypackage does not exist.

This error occurred after creating a package (mypackage) for test purposes, moving files to it (including Main.py), and moving the files back to the root folder afterwards.

The package mypackage was empty after that, but PyCharm still thought that Main.py is located in that package. I could still run the program until I deleted the empty package, but path errors occurred. After deleting the package, I was unable to start it.

Upvotes: 45

Views: 89912

Answers (14)

Victor deMatos
Victor deMatos

Reputation: 973

Ubuntu/Windows

Set the working directory correctly

1. File-> Settings

2. Build, Execution, Deployment -> Console -> Python Console

3. Working directory: [The path to the directory where the file you're currently working on resides.]

obs.: on MacOS: Command ⌘ + comma , to open the preferences menu.

Upvotes: 6

lbl
lbl

Reputation: 1

cd your project_dir

rm -rf .idea

restart your pycharm

Upvotes: 0

Sinux
Sinux

Reputation: 1808

My problem was caused by when I move a file from a directory which has been set as Test sources root to another normal directory, and then I deleted that test source directory, after doing this, that file not work any more.

Delete the file and create it again from @R.G work for me.

Upvotes: 0

Contango
Contango

Reputation: 80382

This worked for me on PyCharm 2021.3.2:

enter image description here

Upvotes: 0

Sildeag
Sildeag

Reputation: 104

I am a beginner so appreciated this 6 year and 6 month discussion. I couldn't add a comment. I setup PyCharm 2022.1.1 and a Python 3.10.4 virtual environment with pyside2 and pyside6 on Ubuntu 22.04. I tried all the suggestions above and could get none of them to work for me. Following these instructions: https://pythonpyqt.com/how-to-install-pyqt5-in-pycharm/ both pyside2-designer (Qt5Designer) and pyside6-designer from my virtual environment were added as external tools. pyside2-designer failed with the working directory error and pyside6-designer worked without any error. Looking closer at pyside2-designer selecting the working directory with the browse button, I still could not create file - exit code was 127 - but the Qt5 Designer was available for existing ui files. I used pyside2 because PyQt5-tools could not be pip installed in my python3.10.4 virtual env.

Upvotes: 0

Marcel
Marcel

Reputation: 464

In my case Run -> Edit Configuration didn't help.
I've solved it changing the value of "WORKING_DIRECTORY" attribute in .idea -> workspace.xml

<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/your/correct/path/here" />

Upvotes: 1

yunfei bai
yunfei bai

Reputation: 1

open the qtdesigner work dictionary setting and choose your project path then click OK, don't not use the mysterious work dictionary path by default

Upvotes: 0

Sheldonfrith
Sheldonfrith

Reputation: 3305

I experienced this problem after moving my project to a different root directory. None of the above solutions worked for me.

I solved it by opening my entire project folder, instead of just the python file I was trying to run. And then running the file I wanted, while the entire project was loaded into PyCharm.

Upvotes: 0

Babak Akbarzade
Babak Akbarzade

Reputation: 41

I had this problem because I renamed my project, it was "xx" I renamed it to "yy", what I did was I went through the directory of .idea in the "yy", in any of those files (all XML files) if there were the name "xx", I replaced it with "yy"

Upvotes: 0

Oleg Ban
Oleg Ban

Reputation: 1

The issue kept popping up over and over in PyCharm. So I created a new project and loaded the needed script. Then I provided the directory to path and assigned the default Python version that I wanted to use... and it worked. Then I was able to finally use "execute line in console" once again.

Upvotes: 0

Nqsir
Nqsir

Reputation: 899

I had the same problem, mine is probably related to the explaination gave by the others, it comes from the dir .idea, files *.xml contain the variable $DIR_PROJECT$.

Therefore, as the attribution of a new path didn't work, I just deleted my .idea, that is automatically loaded each time I open my project's directory.

It automatically regenerated the .idea, asked for the script path... And it worked perfectly

CAREFUL => You will automatically lose your project settings, you are deleting the "settings file"

Upvotes: 4

engineer1221
engineer1221

Reputation: 11

I was getting this same error, and the path in "edit configurations" was correct. However, this is what eventually got my code working again.

1) I commented out all of the code in my file ("ctrl" + "a" + "ctrl" + "/")

2) I commented something I knew would compile in the file. (my list of imports)

3) I ran the python file. This time, it actually completely compiled and after that I was able to uncomment the rest of my code and everything worked again.

Upvotes: 1

Guest
Guest

Reputation: 491

It happens because when you create a file it automatically assigns the working directory to it's configuration, which of course is the one where you created it.

You can change that by going into Run -> Edit Configurations. Click on the folder icon in Script path: and correct the path to the file. Click OK to save and you should be able to Run the file again.

Upvotes: 49

R.G.
R.G.

Reputation: 861

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.)

Upvotes: 5

Related Questions