Roman Starkov
Roman Starkov

Reputation: 61512

How to get filename of the __main__ module in Python?

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?

Upvotes: 88

Views: 66624

Answers (7)

SuB
SuB

Reputation: 2547

I'm using this to obtain filename instead of fullpath:

from pathlib import Path
import __main__

print(Path(__main__.__file__).name)

Upvotes: 0

Edu Fdez
Edu Fdez

Reputation: 21

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

Upvotes: 2

ironfroggy
ironfroggy

Reputation: 8115

import __main__
print(__main__.__file__)

Upvotes: 106

Jarret Hardie
Jarret Hardie

Reputation: 98042

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.

Upvotes: 43

lrsjng
lrsjng

Reputation: 2625

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.

Upvotes: 10

James
James

Reputation: 41

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.

Upvotes: 4

popcnt
popcnt

Reputation: 4645

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

Upvotes: 17

Related Questions