Find Python Module File Name

I got the module name containing the os.path.isfile . Jedi lib gave me a genericpath (no file path). Now I want to get the full name of the PY file using this genericpath module. For instance. "C: \ PY27 \ Lib \ genericpath.py". How should I do it? The Jedi can't do this?

+4
source share
3 answers

You can check the value of __file__ :

 >>> import genericpath >>> genericpath.__file__ '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/genericpath.pyc' 
+6
source

If __file__ does not work, do it right: inspect :

 >>> import somemodule >>> import inspect >>> inspect.getfile(somemodule) '/usr/lib64/python2.7/somemodule.pyc' 
+4
source

Like this:

 >>> import re >>> re.__file__ '/usr/lib/python2.7/re.pyc' 

For packages that are not part of the Python kernel, you can also use __path__ :

 >>> import requests >>> requests.__file__ '/usr/local/lib/python2.7/dist-packages/requests-1.1.0-py2.7.egg/requests/__init__.pyc' >>> requests.__path__ ['/usr/local/lib/python2.7/dist-packages/requests-1.1.0-py2.7.egg/requests'] 
+3
source

Source: https://habr.com/ru/post/1499852/


All Articles