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.