Python's inspect module implements a method to obtain the load module path
- 2020-12-18 01:51:41
- OfStack
This article mainly describes how to get the path of the module. It is necessary to declare that the module mentioned here can be a functional implementation of the module, or other modules.
The.getsourcefile for the inspect module is used (the module name you need to get)
Create test.py as follows:
import os
import inspect
class pathManager(object):
def __init__(self):
pass
def _abPath(self):
modulePath = inspect.getsourcefile(os)
abPath = os.path.split(modulePath)
return abPath[0]
if __name__ == "__main__":
getPath = pathManager()
getPath._abPath()
Execute python ES13en. py to see the results as follows:
clay@aclgcl-ubnt:~/Desktop/python$ python test.py
/usr/local/lib/python2.7/os.py
('/usr/local/lib/python2.7', 'os.py')
clay@aclgcl-ubnt:~/Desktop/python$
Can see we have direct access to: / usr local lib/python2. 7 / os. py, through os. path. split can intercept a simple path.