'__path__'来自何处

时间:2010-01-05 02:47:09

标签: python

我找不到谁定义了'__path__',为什么'__path__'可以使用。

import os
import sys
import warnings 
import ConfigParser # ConfigParser is not a virtualenv module, so we can use it to find the stdlib

dirname = os.path.dirname

distutils_path = os.path.join(os.path.dirname(ConfigParser.__file__), 'distutils')
if os.path.normpath(distutils_path) == os.path.dirname(os.path.normpath(__file__)):
    warnings.warn(
        "The virtualenv distutils package at %s appears to be in the same location as the system distutils?")
else:
    __path__.insert(0, distutils_path)#who defined me.???
    exec open(os.path.join(distutils_path, '__init__.py')).read()

2 个答案:

答案 0 :(得分:8)

您真的需要阅读一些Python文档并学习该语言的基础知识。

我检查过,你似乎会说中文。以下是中文的Python文档资源:

http://www6.uniovi.es/python/doc/NonEnglish.html#chinese

现在,回答你的问题。我不确定答案是什么,所以我用谷歌。我在Google上搜索了“Python __path__”并很快找到了:

http://docs.python.org/tutorial/modules.html

  

6.4.3。多个目录中的包

     

包支持一个特别的   属性,__path__。这是   初始化为包含的列表   持有的目录的名称   在代码之前打包__init__.py   在该文件中执行。这个   变量可以修改;这样做   影响未来的模块搜索   和包含在中的子包   封装

     

虽然此功能并不常见   需要,它可以用来扩展   在包中找到的一组模块。

答案 1 :(得分:3)

我找到了__path__变量的以下描述:

  

它被初始化为一个列表   item,包含目录名称   包(一个子目录)   sys.path上的目录)。更改   __path__更改搜索的目录列表   包的子模块。

此处:http://www.python.org/doc/essays/packages.html

该页面讨论了Python 1.5中的“内置包支持”,但它仍可能适用。

我不能告诉你更多因为我不使用Python。我在Google搜索中找到了此链接。

编辑:是的!我想提醒你我们讨论的内容yesterday但是一个好的开始将是阅读史蒂夫的中文Python文档。

相关问题