Get name of current script in Python

前端 未结 18 2262
北荒
北荒 2020-11-28 00:54

I\'m trying to get the name of the Python script that is currently running.

I have a script called foo.py and I\'d like to do something like this in ord

相关标签:
18条回答
  • 2020-11-28 01:17

    As of Python 3.5 you can simply do:

    from pathlib import Path
    Path(__file__).stem
    

    See more here: https://docs.python.org/3.5/library/pathlib.html#pathlib.PurePath.stem

    For example, I have a file under my user directory named test.py with this inside:

    from pathlib import Path
    
    print(Path(__file__).stem)
    print(__file__)
    

    running this outputs:

    >>> python3.6 test.py
    test
    test.py
    
    0 讨论(0)
  • 2020-11-28 01:21

    all that answers are great, but have some problems You might not see at the first glance.

    lets define what we want - we want the name of the script that was executed, not the name of the current module - so __file__ will only work if it is used in the executed script, not in an imported module. sys.argv is also questionable - what if your program was called by pytest ? or pydoc runner ? or if it was called by uwsgi ?

    and - there is a third method of getting the script name, I havent seen in the answers - You can inspect the stack.

    Another problem is, that You (or some other program) can tamper around with sys.argv and __main__.__file__ - it might be present, it might be not. It might be valid, or not. At least You can check if the script (the desired result) exists !

    the library lib_programname does exactly that :

    • check if __main__ is present
    • check if __main__.__file__ is present
    • does give __main__.__file__ a valid result (does that script exist ?)
    • if not: check sys.argv:
    • is there pytest, docrunner, etc in the sys.argv ? --> if yes, ignore that
    • can we get a valid result here ?
    • if not: inspect the stack and get the result from there possibly
    • if also the stack does not give a valid result, then throw an Exception.

    by that way, my solution is working so far with setup.py test, uwsgi, pytest, pycharm pytest , pycharm docrunner (doctest), dreampie, eclipse

    there is also a nice blog article about that problem from Dough Hellman, "Determining the Name of a Process from Python"

    BTW, it will change again in python 3.9 : the file attribute of the main module became an absolute path, rather than a relative path. These paths now remain valid after the current directory is changed by os.chdir()

    So I rather want to take care of one small module, instead of skimming my codebase if it should be changed somewere ...


    Disclaimer: I'm the author of the lib_programname library.

    0 讨论(0)
  • 2020-11-28 01:23

    The Above answers are good . But I found this method more efficient using above results.
    This results in actual script file name not a path.

    import sys    
    import os    
    file_name =  os.path.basename(sys.argv[0])
    
    0 讨论(0)
  • 2020-11-28 01:23

    The first argument in sys will be the current file name so this will work

    import sys
    print sys.argv[0] # will print the file name
    
    0 讨论(0)
  • 2020-11-28 01:24

    we can try this to get current script name without extension.

    import os
    
    script_name = os.path.splitext(os.path.basename(__file__))[0]
    
    0 讨论(0)
  • 2020-11-28 01:24

    Since the OP asked for the name of the current script file I would prefer

    import os
    os.path.split(sys.argv[0])[1]
    
    0 讨论(0)
提交回复
热议问题