Spyder: Make all paths relative to the current script's directory

Created on 4 May 2016  路  11Comments  路  Source: spyder-ide/spyder

Description of your problem

What steps will reproduce the problem?
When I open a file from a Python script in Spyder (for instance with pandas.read_csv), I always need to specify the full path of the file. Though, I'd like to be able to only use the file names, so that they be relative to the current script's directory. The working directory option in Spyder doesn't do that.

I tried some workarounds, such as sys.argv[0], os.getcwd() or __file__ but they all failed to point to the right directory (__file__ is not even recognized).

What is the expected output? What do you see instead?
I'd like to be able to import a file like that:

pandas.read_csv("foo.csv")

instead of

pandas.read_csv("C:\SOME_LONG_PATH\foo.csv")

Please provide any additional information below

Versions and main components

  • Spyder Version: 2.3.8
  • Python Version: 2.7
  • Operating system: Windows 8

    Dependencies

Please go to the menu entry Help > Optional Dependencies (or
Help > Dependencies), press the button Copy to clipboard
and paste the contents below:
IPython >=1.0 : 4.1.2 (OK)
jedi >=0.8.1;<0.9.0: 0.9.0 (NOK)
matplotlib >=1.0 : 1.5.1 (OK)
pandas >=0.13.1 : 0.18.0 (OK)
pep8 >=0.6 : 1.7.0 (OK)
pyflakes >=0.5.0 : 1.1.0 (OK)
pygments >=1.6 : 2.1.1 (OK)
pylint >=0.25 : None (NOK)
qtconsole >=4.0 : 4.2.0 (OK)
rope >=0.9.2 : 0.9.4 (OK)
sphinx >=0.6.6 : 1.3.5 (OK)
sympy >=0.7.3 : 1.0 (OK)
zmq >=2.1.11 : 15.2.0 (OK)

Help wanted IPython Console Enhancement

Most helpful comment

Would love a way to determine the path of current file. The solution that is suggested everywhere, namely:
os.path.dirname(os.path.realpath(__file__))

, does not work in Spyder. It gives the error:
NameError: name '__file__' is not defined

All 11 comments

I cannot reproduce this. When I put pandas.read_csv("foo.csv") in a Python script file, change the working directory to be the same as the directory in which the script is saved (using "Configure" in the "Run" menu), and then run the script (using "Run" in the "Run" menu), then the foo.csv file in the script's directory is read. Are you doing something different?

I tested on Windows 7, not Windows 8.

I've did some tests and the working directory is only set if you run the whole script once (with the "Run" button). But if you just execute pieces of code within a file, the right working directory won't be set relatively to that file. That's a problem if you're doing experiments.

That clarifies things; I now understand why sys.argv[0] and __file__ do not work. I agree that it's convenient to execute parts of a script, I do it myself, and usually I want the working directory to be the same as the scripts directory.

My solution is to try to start Spyder from the correct directory. Failing that, type one of the following commands to change the working directory:

  • os.chdir(directoryname)
  • %cd directoryname (only works in IPython console)

Alternatively, you can select the correct directory in the text box at the right of the toolbar and click the "Set as current console's working directory" button (in the upcoming 3.0 release of Spyder, you no longer have to click the button).

I think there are situations where you want to execute pieces of code in a different directory, so I'm not sure it's a good idea to have the working directory automatically set to the directory of the script, but maybe it would be good to have an option for this.

Thanks for your feedback. Well, it's a pity that the current working directory not be automatically set to the script that is currently executed. If you work on several .py files located in different directories, it's convenient to load input files that are located in the same directory as the .py file just by typing their names and not a full path. Failing that, I'm going to set the current working directory in the toolbar as you suggested.

Another problem is the import of home-made libraries. For instance, my libs are located in "c:\somepath\libs" and I'd like to import a library that way:
import MyLib

So, after running Spyder, I type these lines to make the import possible:
import sys
sys.path.append("c:\somepath\libs")
This is not very convenient. Is there a nice way to do it?

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.

Yeah, that would be very nice! I hope this will be implemented soon.

Would love a way to determine the path of current file. The solution that is suggested everywhere, namely:
os.path.dirname(os.path.realpath(__file__))

, does not work in Spyder. It gives the error:
NameError: name '__file__' is not defined

@GandalfSaxe, that doesn't work while running code interactively, and that's not something particular to Spyder. If you run the same code in a Python interpreter (i.e. what you get when you execute python in a system terminal) you'll see the same error.

To correctly get the path of the current file, you need to run that file with Run > Run or F5 and not using cells or line by line.

I mean, when running the whole file with F5, os.path.dirname(os.path.realpath(__file__)) works.

Ah I see. Thanks.

Was this page helpful?
0 / 5 - 0 ratings