在Python/Spyder中设置工作目录以使其重现

2024-05-16 02:25:09 发布

您现在位置:Python中文网/ 问答频道 /正文

来自R,使用setwd来更改目录对于可重复性是一个很大的禁忌,因为其他目录结构与我的目录结构不同。因此,建议使用脚本位置的相对路径。

ide稍微复杂一些,因为它们设置了自己的工作目录。在Rstudio中,我可以很容易地解决Rstudio项目的这个问题,将项目的目录设置为我的脚本文件夹。

使用Python和Spyder,似乎没有任何解决方案。Spyder没有Rstudio的项目那样的功能。Setting the directory to the script's location在进行交互分析时不起作用(因为__file__不可用)。

如何才能使Python/Spyder中的工作目录可复制?


Tags: the项目功能目录脚本文件夹解决方案ide
3条回答

要自动执行此操作,请将其放在脚本的开头:

from os import chdir, getcwd
wd=getcwd()
chdir(wd)

在此期间,您可以使用os.chdir

import os
os.chdir('C:\Users\me\Documents')

似乎他们确实认为这是Spyder基于GitHub票证的一项功能,但截至5月中旬仍在等待实现:

We could add an option to the Run dialog to automatically set the working directory to the one your script is being ran.

However, someone else will have to implement it. We're pretty busy with other things at the moment, sorry.

https://github.com/spyder-ide/spyder/issues/3154

@ccordoba12 ccordoba12 added this to the wishlist milestone on May 14

相关问题 更多 >