How to retrieve a module's path?

后端 未结 20 2250
无人及你
无人及你 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: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__))
    

提交回复
热议问题