23

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?

Community
  • 1
  • 1
Heisenberg
  • 8,386
  • 12
  • 53
  • 102
  • none of these answers begin to work like the R library ["here"](https://here.r-lib.org/). – EngrStudent Oct 29 '21 at 12:25
  • An IDE which doesn't associate the default directory to the script directory, or to the user directory if the script hasn't been saved yet, was quite an oddity in 2016. Spyder unfortunately still belongs to this category 5 years later. In Python world c constant is expressed in km/h not in Gm/s ;-) – mins Nov 18 '21 at 10:46

6 Answers6

15

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

from os import chdir, getcwd
wd=getcwd()
chdir(wd)
OSagnostic
  • 169
  • 1
  • 5
  • 1
    This seems not to work any more. `wd Out[4]: 'C:\\Users\\Admin'` though the .py file from where I run the code is in a different directory. Generally, if you run the script with F5 instead, the dir is rightly assigned, see https://github.com/spyder-ide/spyder/issues/3154, see @Hack-R's answer. – questionto42 Aug 11 '20 at 15:23
8

In the interim, you can use os.chdir

import os
os.chdir('C:\Users\me\Documents')
NinComPoop
  • 99
  • 1
  • 2
4

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

Hack-R
  • 22,422
  • 14
  • 75
  • 131
2

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())
Thomas W.
  • 31
  • 1
  • 1
  • 3
1

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!

1

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
  • This seems not to work any more. `os.path.abspath('') Out[12]: 'C:\\Users\\Admin'` though the .py file from where I run the code is in a different directory. Generally, if you run the script with F5 instead, the dir is rightly assigned, see https://github.com/spyder-ide/spyder/issues/3154, see @Hack-R's answer. – questionto42 Aug 11 '20 at 15:23