[Python] How to get filename of the __main__ module in Python?

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página