Python PyCharm 告诉我“无法启动进程,工作目录......不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33229441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:03:19  来源:igfitidea点击:

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

pythonpathpycharm

提问by R.G.

I've seen this question being asked before (at least twice), but I haven't found a solution so far, so I'll ask the question again with some more details.

我之前看到有人问过这个问题(至少两次),但到目前为止我还没有找到解决方案,所以我会再问这个问题,并提供更多细节。

The Problem

问题

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

当我运行我的 python 主文件时,PyCharm 一直告诉我Cannot start process, the working directory /home/myname/PyCharmProjects/MyProjectName/mypackage does not exist.

When did this error occur?

这个错误是什么时候发生的?

After I created a package mypackagefor test purposes, moved files to it (inluding my main file), and moved the files back to the root folder afterwards.

在我mypackage为测试目的创建了一个包之后,将文件移到其中(包括我的主文件),然后将文件移回根文件夹。

The package mypackagewas empty after that, but PyCharm still thought that the main file (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.

之后该包mypackage为空,但 PyCharm 仍认为主文件 ( Main.py) 位于该包中。我仍然可以运行该程序,直到我删除了空包,但发生了路径错误。删除包后,我无法启动它。

Additional Info

附加信息

  • I can still run the other files that used to be in mypackageand are now in my root directory again
  • I can still create and run new files in my root directory
  • 我仍然可以运行mypackage以前在我的根目录中的其他文件
  • 我仍然可以在我的根目录中创建和运行新文件

采纳答案by R.G.

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

经过一段时间的测试,我找到了一个解决方案(但不是 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.)

删除文件并重新创建。(或者重命名或移动它并使用旧名称创建一个新文件,两者都应该有效。)

回答by Guest

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 OKto save and you should be able to Runthe file again.

您可以通过进入Run -> Edit Configurations. 单击中的文件夹图标Script path:并更正文件的路径。单击OK以保存,您应该可以Run再次访问该文件。

回答by engineer1221

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" + "/")

1)我注释掉了文件中的所有代码(“ctrl”+“a”+“ctrl”+“/”)

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

2)我评论了我知道会在文件中编译的内容。(我的进口清单)

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.

3)我运行了python文件。这一次,它实际上完全编译了,之后我能够取消对其余代码的注释,一切都再次运行。

回答by Nqsir

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

我有同样的问题,我的可能与其他人给出的解释有关,它来自 dir .idea,文件*.xml包含变量$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.

因此,由于新路径的归属不起作用,我只是删除了我的.idea,每次打开项目目录时都会自动加载。

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

它会自动重新生成.idea,要求提供脚本路径......并且它完美地工作

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

CAREFUL=> 您将自动丢失项目设置,您正在删除“设置文件”

回答by Oleg Ban

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.

这个问题在 PyCharm 中反复出现。所以我创建了一个新项目并加载了所需的脚本。然后我将目录提供给 path 并分配了我想要使用的默认 Python 版本......并且它起作用了。然后我终于能够再次使用“在控制台中执行行”。