2

I am confusing with python builtin modules and non builtin modules. Here I am using sys module to find the builtin modules,

import sys

def dump(module):
    print module, "=>",
    if module in sys.builtin_module_names:
        print "<BUILTIN>"
    else:
        module = __import__(module)
        print module.__file__

dump("os")
dump("sys")
dump("string")
dump("strop")
dump("zlib")

Output:

os => /usr/lib/python2.7/os.pyc
sys => <BUILTIN>
string => /usr/lib/python2.7/string.pyc
strop => <BUILTIN>
zlib => <BUILTIN>

Here my question is why python guys divided those modules ?. Is it any reason for this. I just need to know about this,if any one knows please explain.

dhana
  • 6,487
  • 4
  • 40
  • 63
  • This provides some explanation of their logic: http://docs.python.org/2/library/intro.html – Jayanth Koushik Feb 26 '14 at 09:40
  • It's probably a legacy thing, as these modules probably originally relied on special code in the interpreter proper. They just call C code, which currently isn't terribly unusual for a python module. – U2EF1 Feb 26 '14 at 09:43

1 Answers1

1

Built-in modules listed in sys.builtin_module_names (except __main__) do not have __file__ attribute.

According to the documentation - Data model:

Modules

__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

It seems like the code distinguish them to avoid AttributeError:

>>> import sys
>>> sys.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

But, there're other modules that are not listed in sys.builtin_module_names, but do not have __file__ attribute. For example:

>>> import cPickle
>>> cPickle.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

To avoid this exception, the function dump should be modified:

def dump(module):
    print module, "=>",
    module = __import__(module)
    print getattr(module, '__file__', 'No File')
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • -1 Many extension modules written in C do have a `__file__` attribute. –  Feb 26 '14 at 09:46
  • @delnan, I added `cPickle` example. Could you give another example? – falsetru Feb 26 '14 at 09:48
  • `_json`. And this time I checked it beforehand, sorry about the wrong example. –  Feb 26 '14 at 09:48
  • @delnan, Same for `_json`. (CPython 2.7.6, Windows) – falsetru Feb 26 '14 at 09:49
  • Huh. What's your platform and Python version? My Ubuntu's Python 2.7 and 3.3 both get _json from a `_json[...].so` file in `/usr/lib/` and set `__file__` accordingly. –  Feb 26 '14 at 09:49
  • @delnan, I confirmed that `_json.__file__` produce `so` file path in Linux. Changed the word in the answer accordingly. Thank you for comment. – falsetru Feb 26 '14 at 09:56
  • Okay, I've got some modules that are DLLs in 3.3 on my Windows machine: `_ssl`, `_ctypes`. If neither of those work in 2.7, check out `C:\Python27\DLLs` and pick any `foo.dll`, it should correspond to a module `foo`. –  Feb 26 '14 at 09:57
  • @delnan, I confirmed that your last comment. Thank you. BTW there's no `.dll` I can import, but `.pyd` files. – falsetru Feb 26 '14 at 10:01
  • @delnan, FYI, [Is a *.pyd file the same as a DLL?](http://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll). – falsetru Feb 26 '14 at 10:02
  • Yes, silly me. I'm well aware of the difference between `*.pyd` and `*.dll`, but because I know how virtually-identical they are, I keep calling them DLLs. –  Feb 26 '14 at 10:05
  • Note sure if it is actually related, but might use useful. From [what's new in Python3.4](http://docs.python.org/3.4/whatsnew/3.4.html#other-language-changes): Module `__file__` attributes (and related values) should now always contain absolute paths by default, with the sole exception of `__main__.__file__` when a script has been executed directly using a relative path. – Ashwini Chaudhary Feb 26 '14 at 12:16