Make all paths relative to the current script's directory
See original GitHub issueDescription 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)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:11 (5 by maintainers)

Top Related StackOverflow Question
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
pythonin 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 > RunorF5and not using cells or line by line.