How to get the current running module path/name

前端 未结 8 1966
不知归路
不知归路 2021-02-03 19:19

I\'ve searched and this seems to be a simple question without a simple answer.

I have the file a/b/c.py which would be called with python -m a.b.c

相关标签:
8条回答
  • 2021-02-03 19:58

    you should hardcode a.b.c in your help, if you distribute the package as such then that's the way to call it regardless of where a is located in the filesystem, as long as it's on the PYTHONPATH it'll be imported.

    0 讨论(0)
  • 2021-02-03 19:59

    One liner But OS dependent

    it does not work in interpreter! since file is meaningless there in the interpreter and is not defined.

    does not require os module to be imported.

    modulename=__file__.split("\\")[-1].split('.')[0]
    

    Explanation: X:\apple\pythonabc.py | will output pythonabc.py

    select the last element after splitting with slashes, then select the first element by splitting it with dot '.'. because first step gives module.py, second step gives 'module' only. __file__ is a unique variable and returns the filepath of current module.

    Comment any flaws or has any other pitfalls.

    0 讨论(0)
  • 2021-02-03 20:06

    When run with -m, sys.path[0] contains the full path to the module. You could use that to build the name.

    source: http://docs.python.org/using/cmdline.html#command-line

    Another option may be the __package__ built in variable which is available within modules.

    0 讨论(0)
  • 2021-02-03 20:11

    I think you're actually looking for the __name__ special variable. From the Python documentation:

    Within a module, the module’s name (as a string) is available as the value of the global variable __name__.

    If you run a file directly, this name will __main__. However, if you're in a module (as in the case where you're using the -m flag, or any other import), it will be the complete name of the module.

    0 讨论(0)
  • 2021-02-03 20:13

    Number of options are there to get the path/name of the current module.

    First be familiar with the use of __file__ in Python, Click here to see the usage.

    It holds the name of currently loaded module.

    Check/Try the following code, it will work on both Python2 & Python3.

    » module_names.py

    import os
    
    print (__file__)
    print (os.path.abspath(__file__))
    print (os.path.realpath(__file__))
    

    Output on MAC OS X:

    MacBook-Pro-2:practice admin$ python module_names.py 
    module_names.py
    /Users/admin/projects/Python/python-the-snake/practice/module_names.py
    /Users/admin/projects/Python/python-the-snake/practice/module_names.py
    

    So here we got the name of current module name and its absolute path.

    0 讨论(0)
  • 2021-02-03 20:18

    Why does nobody mentioned the .__module__?

    When doing a self.__module__ you will get the module path. You can also do this outside of the class:

    Class A:
       self.__module__  # gets module.filename
    
    def get_module():
        A.__module__ # also gets module.filename
    
    0 讨论(0)
提交回复
热议问题