82

Suppose I have two modules:

a.py:

import b
print __name__, __file__

b.py:

print __name__, __file__

I run the "a.py" file. This prints:

b        C:\path\to\code\b.py
__main__ C:\path\to\code\a.py

Question: how do I obtain the path to the __main__ module ("a.py" in this case) from within the "b.py" library?

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • Note: The question and many of the answers here use Python 2 syntax for printing. For Python 3, add parentheses around the print's arguments, as it is now a function. – SuperStormer Jan 06 '22 at 18:03

6 Answers6

103
import __main__
print(__main__.__file__)
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
ironfroggy
  • 7,991
  • 7
  • 33
  • 44
  • 1
    Neat! Important to realise that one needs to import __main__ - I made a random guess and actually tried the second line, but it failed - now I know why. – Roman Starkov Mar 03 '09 at 16:35
  • 29
    `__main__` doesn't always have `__file__` attribute. – jfs Mar 03 '09 at 16:38
  • +1 because of romkyns request in the comments of the post below. – Jarret Hardie Mar 03 '09 at 18:20
  • 1
    @J.F.Sebastian Could we reliably use `inspect.getfile(__main__)` to get the file path? – jpmc26 Feb 10 '15 at 00:22
  • 3
    @jpmc26: I don't know. To get script's directory (not its name), you could use [`get_script_dir()`](http://stackoverflow.com/a/22881871/4279) – jfs Feb 10 '15 at 01:47
  • @jfs: If `__main__` doesn't have a `__file__` attribute, that's almost always because it doesn't come from a file - for example, if you're running interactively. In that case, trying to get the filename is a lost cause. – user2357112 Feb 13 '23 at 16:14
41

Perhaps this will do the trick:

import sys
from os import path
print(path.abspath(str(sys.modules['__main__'].__file__)))

Note that, for safety, you should check whether the __main__ module has a __file__ attribute. If it's dynamically created, or is just being run in the interactive python console, it won't have a __file__:

python
>>> import sys
>>> print(str(sys.modules['__main__']))
<module '__main__' (built-in)>
>>> print(str(sys.modules['__main__'].__file__))
AttributeError: 'module' object has no attribute '__file__'

A simple hasattr() check will do the trick to guard against scenario 2 if that's a possibility in your app.

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
Jarret Hardie
  • 95,172
  • 10
  • 132
  • 126
  • 1
    Wunderbar! I'm glad I could help. If this does answer your question, would you mind hitting the 'accept' button on the question? :-) – Jarret Hardie Mar 03 '09 at 14:33
  • I reverted the changes... in fairness, ironfroggy gave the answer that worked best for you, and I really think he deserves sole credit. – Jarret Hardie Mar 03 '09 at 18:40
  • 2
    Suppose the main script does not have a `__file__` attribute? How do you get the path then? You say to check if main has `__file__` the attribute, but your code doesn't show what to do if main doesn't have it. – Toothpick Anemone Mar 21 '18 at 20:10
  • @ToothpickAnemone: If `__main__` doesn't have a `__file__` attribute, that's almost always because it doesn't come from a file - for example, if you're running interactively. In that case, trying to get the filename is a lost cause. – user2357112 Feb 13 '23 at 16:12
17

The python code below provides additional functionality, including that it works seamlessly with py2exe executables.

I use similar code to like this to find paths relative to the running script, aka __main__. as an added benefit, it works cross-platform including Windows.

import imp
import os
import sys

def main_is_frozen():
   return (hasattr(sys, "frozen") or # new py2exe
           hasattr(sys, "importers") # old py2exe
           or imp.is_frozen("__main__")) # tools/freeze

def get_main_dir():
   if main_is_frozen():
       # print 'Running from path', os.path.dirname(sys.executable)
       return os.path.dirname(sys.executable)
   return os.path.dirname(sys.argv[0])

# find path to where we are running
path_to_script=get_main_dir()

# OPTIONAL:
# add the sibling 'lib' dir to our module search path
lib_path = os.path.join(get_main_dir(), os.path.pardir, 'lib')
sys.path.insert(0, lib_path)

# OPTIONAL: 
# use info to find relative data files in 'data' subdir
datafile1 = os.path.join(get_main_dir(), 'data', 'file1')

Hopefully the above example code can provide additional insight into how to determine the path to the running script...

popcnt
  • 4,519
  • 1
  • 16
  • 14
  • 1
    If you change directory (with `os.chdir`) between launching the script and calling `os.dirname(sys.argv[0])` the result is meaningless. – RobM Jun 28 '10 at 16:06
  • I use to make a `GLOBALS.py` module, that is at least imported by the main script, which executes something like above code and stores the result in a variable `MAINDIR`, so I can then access `GLOBALS.MAINDIR` from any other module, without worrying of the use of `os.chdir`. – Remi Sep 10 '11 at 16:43
  • If you wanted to know the starting directory of your script and you also want to use `os.chdir()`, you'll want to save the initial `os.getcwd()`. Since the user was on windows, I wanted to demonstrate something that would still also work within context of py2exe and freeze. – popcnt Aug 26 '12 at 16:12
9

Another method would be to use sys.argv[0].

import os
import sys

main_file = os.path.realpath(sys.argv[0]) if sys.argv[0] else None

sys.argv[0] will be an empty string if Python gets start with -c or if checked from the Python console.

lrsjng
  • 2,615
  • 1
  • 19
  • 23
  • How do you get the path to the `__main__` module if Python gets started with the `-c` option, or if `sys.argv[0]` is checked from the Python console? – Toothpick Anemone Mar 21 '18 at 21:09
4
import sys, os

def getExecPath():
    try:
        sFile = os.path.abspath(sys.modules['__main__'].__file__)
    except:
        sFile = sys.executable
    return os.path.dirname(sFile)

This function will work for Python and Cython compiled programs.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
James
  • 41
  • 1
1

I use this:

import __main__
name = __main__.__file__ # this gets file path
lastBar =int(name[::-1].find('/'))  # change '/' for linux or '\' for windows
extension = 3 #3 for python file .py
main_file_name=name[::-1][extension:lastBar][::-1] #result
Edu Fdez
  • 11
  • 2