>> import os >>> print os.__file__ /usr/lib/...">

Python: question "import posix"

If I import the os module, I can run the following to locate 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 part of the python runtime, and not as a standard library?

How can I find out additional information like this using only official python documentation?

+6
source share
3 answers

This is the C module. It can be embedded in Python binary or compiled as a shared library. In your case, it is compiled into

White papers say don't import it directly, and you should use the functionality provided through os

+6
source

Run python interactively.

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

There are many good things.

 FILE (built-in) 
+3
source

You can also use the "check" module to find information (for example, path to the source file, etc.) about the python module. For instance:

 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 
+1
source

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


All Articles