snippetpythonCritical
How to retrieve a module's path?
Viewed 0 times
pathhowmoduleretrieve
Problem
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 a module's path in python?
How do I retrieve a module's path in python?
Solution
import a_module
print(a_module.__file__)Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:
import os
path = os.path.abspath(a_module.__file__)You can also try:
path = os.path.dirname(a_module.__file__)To get the module's directory.
Code Snippets
import a_module
print(a_module.__file__)import os
path = os.path.abspath(a_module.__file__)path = os.path.dirname(a_module.__file__)Context
Stack Overflow Q#247770, score: 1360
Revisions (0)
No revisions yet.