“itertools”在哪里?文件

发布于 2024-08-16 12:12:44 字数 429 浏览 6 评论 0原文

import itertools
print itertools#ok

代码没问题,

但我找不到 itertools 文件。

谁能告诉我“itertools 文件”在哪里,


我的代码运行 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__'
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__'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-08-23 12:12:44
>>> import itertools
>>> itertools.__file__
'/usr/lib64/python2.6/lib-dynload/itertools.so'

文件名以 .so 结尾意味着它是用 C 编写的扩展模块(而不是普通的 Python 模块)。如果您想查看源代码,请下载 Python 源代码并查看 Modules/itertoolsmodule.c (您也可以在浏览器中查看它:http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?查看=日志)。

编辑(作为对下面评论的回答):它也适用于 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'
>>> 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'
掩饰不了的爱 2024-08-23 12:12:44

对于 Windows 用户(我正在运行 Python 2.7.2,Win7x64,默认安装程序包),对 __file__ 的调用将如 @zjm1126 所指出的那样熄灭,我怀疑问题在于 itertools 是 Windows 包中的内置函数。如果您选择例外?你会在另一个平台上得到相同的行为(例如我的 macbook 上的 Python 2.6.1) - Windows 只是碰巧有更多的内置函数,如 itertools。

严格来说,这并不是一个答案,但您可以解析 sys.modules ,这会提示您它来自哪里:

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

<module 'itertools' (built-in)>

它指向内置于您的 python 可执行文件中的 itertools 。

此外,imp.find_module 响应提供相同的信息:奇怪的返回元组是按规范的(请参阅:http://docs.python.org/2/library/imp.html#imp.find_module) 并告诉您该模块的类型为 6,这是 imp.C_BUILTIN

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

川水往事 2024-08-23 12:12:44

试试这个

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

更新:
由于你的没有,另一个通过手动方式。执行 sys.path

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

然后根据您的系统,使用系统的搜索工具来查找它。在我的 Linux 系统上

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

,或者,只需在整个系统中搜索名为“itertools”的文件。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文