Python import module vs import _module
Question: While configuring PyDev's [Forced
Builtins](http://pydev.org/manual_101_interpreter.html#PyDevInterpreterConfiguration-
ForcedBuiltins) 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?


Answer: 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](http://hg.python.org/cpython/file/2.7/Lib/csv.py); it imports from the
[`_csv.c` C library](http://hg.python.org/cpython/file/2.7/Modules/_csv.c) 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.

