question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make all paths relative to the current script's directory

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
gandalfsaxecommented, Jun 8, 2017

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

2reactions
ccordoba12commented, Jun 9, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relative paths in Python [duplicate] - Stack Overflow
I do have a relative path from the script but when I call the script it treats that as a path relative to...
Read more >
Simple trick to work with relative paths in Python | by Mike Huls
First we'll go up one folder using the '..' (this is the same as in a terminal). This will navigate us to the...
Read more >
How to get a relative path in Python | Towards the Cloud
To get a relative path in Python you first have to find the location of the working directory where the script or module...
Read more >
Using relative paths in Linux scripts - Tjelvar Olsson
To illustrate the use of relative paths in scripts create a file named analysis.sh in your scripts directory, i.e. with the relative path...
Read more >
Paths explained: Absolute, relative, UNC, and URL—Help
A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found