How to retrieve a module's path?

后端 未结 20 1724
无人及你
无人及你 2020-11-22 05:34

I want to detect whether module has changed. Now, using inotify is simple, you just need to know the directory you want to get notifications from.

How do I retrieve

相关标签:
20条回答
  • 2020-11-22 06:00

    If you would like to know absolute path from your script you can use Path object:

    from pathlib import Path
    
    print(Path().absolute())
    print(Path().resolve('.'))
    print(Path().cwd())
    

    cwd() method

    Return a new path object representing the current directory (as returned by os.getcwd())

    resolve() method

    Make the path absolute, resolving any symlinks. A new path object is returned:

    0 讨论(0)
  • 2020-11-22 06:00

    If the only caveat of using __file__ is when current, relative directory is blank (ie, when running as a script from the same directory where the script is), then a trivial solution is:

    import os.path
    mydir = os.path.dirname(__file__) or '.'
    full  = os.path.abspath(mydir)
    print __file__, mydir, full
    

    And the result:

    $ python teste.py 
    teste.py . /home/user/work/teste
    

    The trick is in or '.' after the dirname() call. It sets the dir as ., which means current directory and is a valid directory for any path-related function.

    Thus, using abspath() is not truly needed. But if you use it anyway, the trick is not needed: abspath() accepts blank paths and properly interprets it as the current directory.

    0 讨论(0)
  • 2020-11-22 06:03
    import module
    print module.__path__
    

    Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.

    While this feature is not often needed, it can be used to extend the set of modules found in a package.

    Source

    0 讨论(0)
  • 2020-11-22 06:04

    you can just import your module then hit its name and you'll get its full path

    >>> import os
    >>> os
    <module 'os' from 'C:\\Users\\Hassan Ashraf\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\os.py'>
    >>>
    
    0 讨论(0)
  • From within modules of a python package I had to refer to a file that resided in the same directory as package. Ex.

    some_dir/
      maincli.py
      top_package/
        __init__.py
        level_one_a/
          __init__.py
          my_lib_a.py
          level_two/
            __init__.py
            hello_world.py
        level_one_b/
          __init__.py
          my_lib_b.py
    

    So in above I had to call maincli.py from my_lib_a.py module knowing that top_package and maincli.py are in the same directory. Here's how I get the path to maincli.py:

    import sys
    import os
    import imp
    
    
    class ConfigurationException(Exception):
        pass
    
    
    # inside of my_lib_a.py
    def get_maincli_path():
        maincli_path = os.path.abspath(imp.find_module('maincli')[1])
        # top_package = __package__.split('.')[0]
        # mod = sys.modules.get(top_package)
        # modfile = mod.__file__
        # pkg_in_dir = os.path.dirname(os.path.dirname(os.path.abspath(modfile)))
        # maincli_path = os.path.join(pkg_in_dir, 'maincli.py')
    
        if not os.path.exists(maincli_path):
            err_msg = 'This script expects that "maincli.py" be installed to the '\
            'same directory: "{0}"'.format(maincli_path)
            raise ConfigurationException(err_msg)
    
        return maincli_path
    

    Based on posting by PlasmaBinturong I modified the code.

    0 讨论(0)
  • 2020-11-22 06:07

    This was trivial.

    Each module has a __file__ variable that shows its relative path from where you are right now.

    Therefore, getting a directory for the module to notify it is simple as:

    os.path.dirname(__file__)
    
    0 讨论(0)
提交回复
热议问题