534

How do I get the path of a the Python script I am running in? I was doing dirname(sys.argv[0]), however on Mac I only get the filename - not the full path as I do on Windows.

No matter where my application is launched from, I want to open files that are relative to my script file(s).

10
  • 26
    It seems that Jeff forgot that not all python scripts are modules, please nominate for reopening. Commented Nov 22, 2011 at 11:26
  • @sorin oh, but they are; a module object can be created for any script file. Just because something never actually gets imported doesn't make it "not a module". The answer is the same, anyway: treat the script as a module (use some kind of bootstrap if really necessary) and then apply the same technique. Commented Dec 26, 2011 at 7:10
  • 10
    Yes, a script is a module, but this well-asked question should be re-opened. It has not been answered here, and the "duplicate" question is not a duplicate because it only answers how to get the location of a module you have loaded, not the one you are in. Commented Jan 19, 2012 at 18:38
  • 3
    see the "import inspect" solution at stackoverflow.com/questions/50499/… Commented Jan 19, 2012 at 18:55
  • 1
    I have changed the duplicate links so that they point at actual duplicate questions - this question does not need to be reopened. Commented Jun 10, 2014 at 13:09

4 Answers 4

660

Use this to get the path of the current file. It will resolve any symlinks in the path.

import os

file_path = os.path.realpath(__file__)

This works fine on my mac. It won't work from the Python interpreter (you need to be executing a Python file).

Sign up to request clarification or add additional context in comments.

18 Comments

Not so sure about that, here a result from OSX 10.6 NameError: global name '__file__' is not defined
Sorin: Perhaps you tried the expression in the shell?
Make a chdir before calling realpath and real path will fail, this is not the answer.
The NameError: global name '__file__' is not defined error just means you forgot to import os.
NameError: __file__ ... And I have imported os. Not sure why this is the accepted answer..?
|
176
import os
print os.path.abspath(__file__)

4 Comments

Thanks. This get the path of the .py file where the code is written, which is what I want, even though OP wanted another path.
This should be combined with information in the other answer: os.path.abspath(os.path.dirname(__file__)) will give the directory.
Thank you. Does it work for you on jupyter notebook?
this topic to work on notebook
152

7.2 of Dive Into Python: Finding the Path.

import sys, os

print('sys.argv[0] =', sys.argv[0])             
pathname = os.path.dirname(sys.argv[0])        
print('path =', pathname)
print('full path =', os.path.abspath(pathname)) 

9 Comments

I like how your answer shows a lot of different variations and features from python to solve the question.
This does not work if you call the script via another script from another directory.
That's probably something we should tell the author.
@SorinSbarnea see the subject of the question: get the path of the running script (the script which has been executed). An imported module is just a resource.
The question is not very clear, but your solution would work only if it is called at the beginning of the script (in order to be sure that nobody changes the current directory). I am inclined to use the inspect method instead. "running script" is not necessarily same as "called script" (entry point) or "currently running script".
|
62

The accepted solution for this will not work if you are planning to compile your scripts using py2exe. If you're planning to do so, this is the functional equivalent:

os.path.dirname(sys.argv[0])

Py2exe does not provide an __file__ variable. For reference: http://www.py2exe.org/index.cgi/Py2exeEnvironment

4 Comments

Unluckily this does not work on Mac, as the OP says. Please see sys.argv: it is operating system dependent whether this is a full pathname or not
(it's incredibly complex to get a really cross-browser solution...)
@Stefano that's not a problem. To make sure it's an absolute (full) pathname, use os.path.abspath(...)
this doesn't give the location of the .py file of interest when I use a jupyter notebook