gpt4 book ai didi

python - Python 中的路径搜索

转载 作者:行者123 更新时间:2023-12-01 05:02:05 25 4
gpt4 key购买 nike

我想这个问题以前已经得到过回答,但我找不到准确描述搜索过程的线程。如果我错了,请道歉。我

Python 查找模块的确切搜索过程是什么?哪些路径按什么顺序考虑? (例如 sys.pathPYTHONPATH 定义、site-packages 等)

我复制了下面的信息 the link评论中提供了以下内容,但我有以下问题:

  • 安装相关默认值到底是什么?
  • 如果您激活了 virtualenvconda 的虚拟环境,会发生什么情况?

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

最佳答案

您要寻找的答案就在这里:https://docs.python.org/2/tutorial/modules.html见第 6.1.2 节。

根据网站情况

the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.

关于python - Python 中的路径搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25875484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com