开发者

Python: "import posix" question

If I import os module, I can run the following to infer the location of os.py

>>> import os
>>> print os.__file__
/usr/lib/python2.6/os.pyc

However, when I import posix, it does not have the __file__ attribute. Is it because it is implemented as a part of the python runtime, not as a standard library?

How can I find out more information lik开发者_如何学运维e this using solely the python official documentation?


It's a C module. It can be either built into the Python binary or compiled as a shared library. In your case it is compiled in

The official docs say not to import it directly, and you should use the functionality provided via os


Run python interactively.

>>> import posix
>>> help(posix)

There's a lot of good stuff there.

FILE
    (built-in)


You can also use the 'inspect' module to find information (say source file path etc) about a python module. For example:

import inspect
import os
inspect.getsourcefile(os)
'/usr/local/lib/python2.7/os.py'

inspect.getsourcefile(inspect)
'/usr/local/lib/python2.7/inspect.py'

import sys        
inspect.getsourcefile(sys)
Traceback (most recent call last):
[...]
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'sys' (built-in)> is a built-in module
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜