How to retrieve a module's path?

后端 未结 20 1685
无人及你
无人及你 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:16

    If you installed it using pip, "pip show" works great ('Location')

    $ pip show detectron2

    Name: detectron2
    Version: 0.1
    Summary: Detectron2 is FAIR next-generation research platform for object detection and segmentation.
    Home-page: https://github.com/facebookresearch/detectron2
    Author: FAIR
    Author-email: None
    License: UNKNOWN
    Location: /home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages
    Requires: yacs, tabulate, tqdm, pydot, tensorboard, Pillow, termcolor, future, cloudpickle, matplotlib, fvcore
    
    0 讨论(0)
  • 2020-11-22 06:17

    There is inspect module in python.

    Official documentation

    The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

    Example:

    >>> import os
    >>> import inspect
    >>> inspect.getfile(os)
    '/usr/lib64/python2.7/os.pyc'
    >>> inspect.getfile(inspect)
    '/usr/lib64/python2.7/inspect.pyc'
    >>> os.path.dirname(inspect.getfile(inspect))
    '/usr/lib64/python2.7'
    
    0 讨论(0)
  • 2020-11-22 06:17

    So I spent a fair amount of time trying to do this with py2exe The problem was to get the base folder of the script whether it was being run as a python script or as a py2exe executable. Also to have it work whether it was being run from the current folder, another folder or (this was the hardest) from the system's path.

    Eventually I used this approach, using sys.frozen as an indicator of running in py2exe:

    import os,sys
    if hasattr(sys,'frozen'): # only when running in py2exe this exists
        base = sys.prefix
    else: # otherwise this is a regular python script
        base = os.path.dirname(os.path.realpath(__file__))
    
    0 讨论(0)
  • 2020-11-22 06:18

    As the other answers have said, the best way to do this is with __file__ (demonstrated again below). However, there is an important caveat, which is that __file__ does NOT exist if you are running the module on its own (i.e. as __main__).

    For example, say you have two files (both of which are on your PYTHONPATH):

    #/path1/foo.py
    import bar
    print(bar.__file__)
    

    and

    #/path2/bar.py
    import os
    print(os.getcwd())
    print(__file__)
    

    Running foo.py will give the output:

    /path1        # "import bar" causes the line "print(os.getcwd())" to run
    /path2/bar.py # then "print(__file__)" runs
    /path2/bar.py # then the import statement finishes and "print(bar.__file__)" runs
    

    HOWEVER if you try to run bar.py on its own, you will get:

    /path2                              # "print(os.getcwd())" still works fine
    Traceback (most recent call last):  # but __file__ doesn't exist if bar.py is running as main
      File "/path2/bar.py", line 3, in <module>
        print(__file__)
    NameError: name '__file__' is not defined 
    

    Hope this helps. This caveat cost me a lot of time and confusion while testing the other solutions presented.

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

    I don't get why no one is talking about this, but to me the simplest solution is using imp.find_module("modulename") (documentation here):

    import imp
    imp.find_module("os")
    

    It gives a tuple with the path in second position:

    (<open file '/usr/lib/python2.7/os.py', mode 'U' at 0x7f44528d7540>,
    '/usr/lib/python2.7/os.py',
    ('.py', 'U', 1))
    

    The advantage of this method over the "inspect" one is that you don't need to import the module to make it work, and you can use a string in input. Useful when checking modules called in another script for example.

    EDIT:

    In python3, importlib module should do:

    Doc of importlib.util.find_spec:

    Return the spec for the specified module.

    First, sys.modules is checked to see if the module was already imported. If so, then sys.modules[name].spec is returned. If that happens to be set to None, then ValueError is raised. If the module is not in sys.modules, then sys.meta_path is searched for a suitable spec with the value of 'path' given to the finders. None is returned if no spec could be found.

    If the name is for submodule (contains a dot), the parent module is automatically imported.

    The name and package arguments work the same as importlib.import_module(). In other words, relative module names (with leading dots) work.

    0 讨论(0)
  • 2020-11-22 06:22
    import os
    path = os.path.abspath(__file__)
    dir_path = os.path.dirname(path)
    
    0 讨论(0)
提交回复
热议问题