简体   繁体   中英

Python import module vs import _module

While configuring PyDev's Forced Builtins in Aptana I noticed that some modules were referenced by default with an _ (underscore) prefix.

So I open a Python interpreter and to my surprise, the imports below work:

import ast
import _ast
import bisect
import _bisect
import csv
import _csv
# ... and so on

Now if I do a dir() on the imported modules, I see different contents:

>>> dir(csv)
['Dialect', 'DictReader', 'DictWriter', 'Error', ...] # and so on
>>> dir(_csv)
['Dialect', 'Error', ...] # node that DictReader and DictWriter are missing

Finally, help() tells me they are clearly different modules:

>>> help(_csv)
Help on module _csv:

NAME
    _csv - CSV parsing and writing.

FILE
    /usr/lib64/python2.6/lib-dynload/_csv.so
...

>>> help(csv)
Help on module csv:

NAME
    csv - CSV parsing and writing.

FILE
    /usr/lib64/python2.6/csv.py
...

So, what's the difference between import module and import _module ? Is there a convention behind it or something like that?

Some modules use some C code to implement parts that need the speed. The main module still uses Python glue, and the _module version contains the C extension.

See for example the csv.py module ; it imports from the _csv.c C library for most of the functionality, with only the Dialect , Sniffer , DictReader and DictWriter classes implemented in pure Python.

The module plus _module convention is just that, a convention. Not all C extensions follow this pattern.

_module is usually the part of module that is written in C. module is a python wrapper around it. You shouldn't ever need to import _modules yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM