Heisenberg
Heisenberg

Reputation: 8806

Set working directory in Python / Spyder so that it's reproducible

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.

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.

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 location does not work while doing interactive analysis (since __file__ is not available).

What to do so that the working directory in Python / Spyder is reproducible?

Upvotes: 25

Views: 85341

Answers (6)

Thomas W.
Thomas W.

Reputation: 31

as I wrote here, Mark8888 pointed out to run the whole script (run file (F5)) instead of just pieces of the script

this way multiple approaches should work to get the script file location and change the current working directory

import os
# directory of script file
print(os.path.abspath(os.path.dirname(__file__)))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# current working directory
print(os.getcwd())

also

import os
import sys
# directory of script file
print(os.path.abspath(os.path.dirname(sys.argv[0])))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
# current working directory
print(os.getcwd())

Upvotes: 2

Wojciech Pawlik
Wojciech Pawlik

Reputation: 88

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

Upvotes: 1

Akash Mantry
Akash Mantry

Reputation: 11

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.

Hope it helps!

Upvotes: 1

OSagnostic
OSagnostic

Reputation: 199

To do this automatically, put this at the beginning of your script:

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

Upvotes: 18

NinComPoop
NinComPoop

Reputation: 109

In the interim, you can use os.chdir

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

Upvotes: 9

Hack-R
Hack-R

Reputation: 23217

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:

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

Upvotes: 4

Related Questions