“路径”从何而来

2024-09-30 15:31:38 发布

您现在位置: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()

Tags: thetopathimport定义virtualenvossys
2条回答

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

It is initialized to a list of one item, containing the directory name of the package (a subdirectory of a directory on sys.path). Changing __path__ changes the list of directories that are searched for submodules of the package.

这里:http://www.python.org/doc/essays/packages.html

该页讨论了Python1.5中的“内置包支持”,但它可能仍然适用。

我不能告诉你更多,因为我不使用Python。我在谷歌上找到了这个链接。

编辑:是的!我本来想提醒你我们讨论过的yesterday但是一个好的开始是阅读steveha的中文Python文档。

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

我查过了,你好像会说中文。以下是Python中文文档资源:

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

现在,回答你的问题。我不知道答案是什么,所以我用了谷歌。我在谷歌上搜索了“Python __path__”,很快发现:

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

6.4.3. Packages in Multiple Directories

Packages support one more special attribute, __path__. This is initialized to be a list containing the name of the directory holding the package’s __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.

While this feature is not often needed, it can be used to extend the set of modules found in a package.

相关问题 更多 >