在 Python / Spyder 中设置工作目录,使其可重现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38393628/
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
Set working directory in Python / Spyder so that it's reproducible
提问by Heisenberg
Coming from R, using setwd
to change the directory is a big no-no against reproducibility because others do not have the same directory structure as mine. Hence, it's recommended to use relative path from the location of the script.
来自 R,使用setwd
更改目录是一个很大的反对可重复性的禁忌,因为其他人没有与我的目录结构相同的目录结构。因此,建议使用脚本位置的相对路径。
IDEs slightly complicate this because they set their own working directory. In Rstudio, I can easily get around this problem with Rstudio's projects, setting the project's directory to be my script folder.
IDE 稍微复杂一点,因为它们设置了自己的工作目录。在 Rstudio 中,我可以通过 Rstudio 的项目轻松解决这个问题,将项目的目录设置为我的脚本文件夹。
With Python and Spyder, there doesn't seem to be any solution. Spyder does not have a feature like Rstudio's project. Setting the directory to the script's locationdoes not work while doing interactive analysis (since __file__
is not available).
使用 Python 和 Spyder,似乎没有任何解决方案。Spyder 没有像 Rstudio 的项目那样的功能。在进行交互式分析时,将目录设置为脚本的位置不起作用(因为__file__
不可用)。
What to do so that the working directory in Python / Spyder is reproducible?
怎样做才能使 Python / Spyder 中的工作目录可重现?
回答by OSagnostic
To do this automatically, put this at the beginning of your script:
要自动执行此操作,请将其放在脚本的开头:
from os import chdir, getcwd
wd=getcwd()
chdir(wd)
回答by NinComPoop
In the interim, you can use os.chdir
在此期间,您可以使用 os.chdir
import os
os.chdir('C:\Users\me\Documents')
回答by Wojciech Pawlik
I tried this and it works.
我试过这个,它的工作原理。
import os
abspath = os.path.abspath('') ## String which contains absolute path to the script file
os.chdir(abspath) ## Setting up working directory
回答by Hack-R
It appears they did consider this as a feature in Spyder based on this GitHub ticket, but it is still waiting implementation as of mid-May:
看来他们确实认为这是基于此 GitHub 票证的 Spyder 中的一项功能,但截至 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
https://github.com/spyder-ide/spyder/issues/3154
@ccordoba12 ccordoba12 added this to the wishlist milestone on May 14
@ccordoba12 ccordoba12 于 5 月 14 日将此添加到愿望清单里程碑
回答by Akash Mantry
Well, there are a lot of things that you can try! 1. Change the directory to the current directory in the toolbar. 2. Change the Global directory to the current directory in Preferences>Global Working Directory. Click 'the current file directory' radio button.
嗯,有很多事情你可以尝试!1. 将目录更改为工具栏中的当前目录。2. 在首选项>全局工作目录中将全局目录更改为当前目录。单击“当前文件目录”单选按钮。
Hope it helps!
希望能帮助到你!