1

I am working with Spyder IDE to run my Python code. It seems that my cwd is set to my home folder by default, and I have to manually convert it to the directory of whatever file I'm working in using os. This is very cumbersome. I would obviously like the cwd to simply be the directory of the file I'm working with.

I've checked under Tools > Preferences > Current working directory, but the working directory is not set to a fixed directory. What are my options?

  • Refer to [this](https://stackoverflow.com/questions/47916268/how-to-set-working-directory-pemanently-in-spyder-rstudio) solution – The Singularity Jan 08 '21 at 09:57
  • @Luke No, I want the opposite. I do *not* want my cwd to be a fixed directory. I want it to depend on the file I'm working in. – Mr. Palomar Jan 08 '21 at 09:59
  • If you want to automatically set the working directory use `os.chdir(os.path.dirname(file))` – The Singularity Jan 08 '21 at 10:02
  • @Luke I am aware and that's precisely what I'm doing now, but it makes absolutely no sense that I suddenly have to put that in front of every single one of my Python files. – Mr. Palomar Jan 08 '21 at 10:04
  • If you set the console directory to "The current working directory", the directory of your (current) Python script will be set as soon as you run it (default F5). Is that what you're looking for? – FObersteiner Jan 08 '21 at 10:35
  • @MrFuppes I guess so. What does it mean to set the console directory to the current working directory? – Mr. Palomar Jan 08 '21 at 11:13
  • Spyder runs your code through an IPython cosole, which then uses the directory of the .py file you run as working directory. This will be set individually for each script you run (you can check with `os.getcwd()`). But keep in mind that it is only set if you run the file, but not if you only run a few highlighted lines from the script (default F9). – FObersteiner Jan 08 '21 at 11:27
  • @MrFuppes I agree that that should be the case. The cwd should be set to the directory of the file whenever you run a script. But that's not what it's doing, and I'd like to fix that, hence the question. – Mr. Palomar Jan 08 '21 at 12:50

0 Answers0