where is the 'itertools' file

青春壹個敷衍的年華 提交于 2019-11-29 18:51:25

问题


import itertools
print itertools#ok

the code is ok

but i can't find the itertools file.

who can tell me where is the 'itertools file'


my code is run python2.5

import itertools
print itertools.__file__


Traceback (most recent call last):
  File "D:\zjm_code\mysite\zjmbooks\a.py", line 5, in <module>
    print itertools.__file__
AttributeError: 'module' object has no attribute '__file__'

回答1:


>>> import itertools
>>> itertools.__file__
'/usr/lib64/python2.6/lib-dynload/itertools.so'

The fact that the filename ends with .so means it's an extension module written in C (rather than a normal Python module). If you want to take a look at the source code, download the Python source and look into Modules/itertoolsmodule.c (you can also view it in your browser at http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?view=log).

Edit (as an answer to the comment below): it also works with Python 2.5:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> itertools.__file__
'/usr/lib/python2.5/lib-dynload/itertools.so'



回答2:


If you're looking for the source file (in C, of course), it's for example online here.




回答3:


For the windows users (I'm running Python 2.7.2, Win7x64, default installer package) the call to __file__ will flame out as @zjm1126 has noted, I suspect the problem being that itertools is a builtin on the windows package. If you'd picked say, exceptions? You'd get the same behaviour on another platform (e.g. Python 2.6.1 on my macbook) - Windows just happens to have a few more builtins like itertools.

It's not strictly an answer as such, but you could parse sys.modules which would give you a hint as to where it's coming from:

>>> import sys
>>> sys.modules['itertools']

<module 'itertools' (built-in)>

which points at itertools being built-in to your python executable.

Also, the imp.find_module response is providing the same information: the weird return tuple is by spec (see: http://docs.python.org/2/library/imp.html#imp.find_module) and telling you that the module is of type 6, which is the enumeration for imp.C_BUILTIN




回答4:


try this

>>> import imp
>>> imp.find_module("itertools")

update: since yours is None, another go through a manual way. Do a sys.path

>>> import sys
>>> sys.path
['', '/usr/lib/python2.6/lib-dynload' ]

then depending on your system, use your system's search facility to find it. on my linux system

$ find /usr/lib/python2.6/lib-dynload -type f -iname "*itertools*"
/usr/lib/python2.6/lib-dynload/itertoolsmodule.so

OR, just search the entire system for the file with name "itertools".



来源:https://stackoverflow.com/questions/2010413/where-is-the-itertools-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!