tags:

views:

135

answers:

3
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__'
A: 

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".

ghostdog74
it returns (None, 'itertools', ('', '', 6))
zjm1126
+10  A: 
>>> 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'
Antoine P.
thanks,pitrou,i think you are right in python2.6,but my code run error in python 2.5,it has not a attribute named '__file__'
zjm1126
What is your Python build (version, OS, origin)? Perhaps the module has been compiled statically rather than dynamically. But `__file__` works in Python 2.5, too: see edited answer above. In any case, the answer I gave above is right: the module is written in C and its source code is available in the official Python source tree.
Antoine P.
hi Antoine P.my system is windows_xp, the code you give me run error,i don't know what happen.
zjm1126
+2  A: 

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

Alex Martelli