How do I get the path of the current executed file in Python?

Python Programming

Question or problem about Python programming:

This may seem like a newbie question, but it is not. Some common approaches don’t work in all cases:

This means using path = os.path.abspath(os.path.dirname(sys.argv[0])), but this does not work if you are running from another Python script in another directory, and this can happen in real life.

This means using path = os.path.abspath(os.path.dirname(__file__)), but I found that this doesn’t work:

Related questions with incomplete answers:

I’m looking for a generic solution, one that would work in all above use cases.

Here is the result of a testcase: __file__= os.getcwd()= C:\zzz sys.argv[0]= __file__= os.getcwd()= C:\zzz
#! /usr/bin/env python
import os, sys

print " sys.argv[0]=", sys.argv[0]
print " __file__=", __file__
print " os.getcwd()=", os.getcwd()

#! /usr/bin/env python
import os, sys

print " sys.argv[0]=", sys.argv[0]
print " __file__=", __file__
print " os.getcwd()=", os.getcwd()

How to solve the problem:

Solution 1:

You can’t directly determine the location of the main script being executed. After all, sometimes the script didn’t come from a file at all. For example, it could come from the interactive interpreter or dynamically generated code stored only in memory.

However, you can reliably determine the location of a module, since modules are always loaded from a file. If you create a module with the following code and put it in the same directory as your main script, then the main script can import the module and use that to locate itself.


def we_are_frozen():
    # All of the modules are built-in to the interpreter, e.g., by py2exe
    return hasattr(sys, "frozen")

def module_path():
    encoding = sys.getfilesystemencoding()
    if we_are_frozen():
        return os.path.dirname(unicode(sys.executable, encoding))
    return os.path.dirname(unicode(__file__, encoding))


import module_locator
my_path = module_locator.module_path()

If you have several main scripts in different directories, you may need more than one copy of module_locator.

Of course, if your main script is loaded by some other tool that doesn’t let you import modules that are co-located with your script, then you’re out of luck. In cases like that, the information you’re after simply doesn’t exist anywhere in your program. Your best bet would be to file a bug with the authors of the tool.

Solution 2:

First, you need to import from inspect and os

from inspect import getsourcefile
from os.path import abspath

Next, wherever you want to find the source file from you just use


Solution 3:

this solution is robust even in executables

import inspect, os.path

filename = inspect.getframeinfo(inspect.currentframe()).filename
path     = os.path.dirname(os.path.abspath(filename))

Solution 4:

I was running into a similar problem, and I think this might solve the problem:

def module_path(local_function):
   ''' returns the module path without the use of __file__.  Requires a function defined
   locally in the module.
   return os.path.abspath(inspect.getsourcefile(local_function))

It works for regular scripts and in idle. All I can say is try it out for others!

My typical usage:

from toolbox import module_path
def main():
   pass # Do stuff

global __modpath__
__modpath__ = module_path(main)

Now I use __modpath__ instead of __file__.

Solution 5:

The short answer is that there is no guaranteed way to get the information you want, however there are heuristics that work almost always in practice. You might look at How do I find the location of the executable in C?. It discusses the problem from a C point of view, but the proposed solutions are easily transcribed into Python.

Hope this helps!