initialize#

用于 conda init 的后端逻辑。

本模块中的章节有

  1. 顶层函数

  2. 计划创建器

  3. 计划运行器

  4. 单独操作

  5. 辅助函数

顶层函数组成并执行完整计划。

计划通过组合各种单独操作来创建。计划数据结构是字典列表,其中每个字典代表一个单独操作。字典包含两个键——functionkwargs——其中 function 是本模块中单独操作函数的名称。

每个单独操作必须

  1. 返回 Result (即 NEEDS_SUDO、MODIFIED 或 NO_CHANGE)

  2. 如果 context.dry_run 为 True,则没有副作用

  3. 如果 context.verbose,则详细描述正在进行或提议的更改

计划运行器函数将计划(字典列表)作为参数,然后协调每个单独操作的执行。run_plan_elevated() 的文档字符串详细说明了该策略的实现方式。

#

函数#

install(conda_prefix)

initialize(conda_prefix, shells, for_user, for_system, ...)

initialize_dev(shell[, dev_env_prefix, conda_source_root])

_initialize_dev_bash(prefix, env_vars, unset_env_vars)

_initialize_dev_cmdexe(prefix, env_vars, unset_env_vars)

make_install_plan(conda_prefix)

make_initialize_plan(conda_prefix, shells, for_user, ...)

创建用于在 shell 中初始化 conda 的计划。

run_plan(plan)

run_plan_elevated(plan)

此函数的策略在 Unix 和 Windows 之间有所不同。两种策略都使用

run_plan_from_stdin

()

run_plan_from_temp_file(temp_path)

print_plan_results(plan[, stream])

make_entry_point(target_path, conda_prefix, module, func)

make_entry_point_exe(target_path, conda_prefix)

install_anaconda_prompt(target_path, conda_prefix, reverse)

_install_file(target_path, file_content)

install_conda_sh(target_path, conda_prefix)

install_Scripts_activate_bat(target_path, conda_prefix)

install_activate_bat(target_path, conda_prefix)

install_deactivate_bat(target_path, conda_prefix)

install_activate(target_path, conda_prefix)

install_deactivate(target_path, conda_prefix)

install_condabin_conda_bat(target_path, conda_prefix)

install_library_bin_conda_bat(target_path, conda_prefix)

install_condabin_conda_activate_bat(target_path, ...)

install_condabin_rename_tmp_bat(target_path, conda_prefix)

install_condabin_conda_auto_activate_bat(target_path, ...)

install_condabin_hook_bat(target_path, conda_prefix)

install_conda_fish(target_path, conda_prefix)

install_conda_psm1(target_path, conda_prefix)

install_conda_hook_ps1(target_path, conda_prefix)

install_conda_xsh(target_path, conda_prefix)

install_conda_csh(target_path, conda_prefix)

_config_fish_content(conda_prefix)

init_fish_user(target_path, conda_prefix, reverse)

_config_xonsh_content(conda_prefix)

init_xonsh_user(target_path, conda_prefix, reverse)

_bashrc_content(conda_prefix, shell)

init_sh_user(target_path, conda_prefix, shell[, reverse])

init_sh_system(target_path, conda_prefix[, reverse])

_read_windows_registry(target_path)

_write_windows_registry(target_path, value_value, ...)

init_cmd_exe_registry(target_path, conda_prefix[, reverse])

init_long_path(target_path)

_powershell_profile_content(conda_prefix)

init_powershell_user(target_path, conda_prefix, reverse)

remove_conda_in_sp_dir(target_path)

make_conda_egg_link(target_path, conda_source_root)

modify_easy_install_pth(target_path, conda_source_root)

make_dev_egg_info_file(target_path)

make_diff(old, new)

_get_python_info(prefix)

属性#

CONDA_INITIALIZE_RE_BLOCK = '^# >>> conda initialize >>>(?:\\n|\\r\\n)([\\s\\S]*?)# <<< conda initialize <<<(?:\\n|\\r\\n)?'#
CONDA_INITIALIZE_PS_RE_BLOCK = '^#region conda initialize(?:\\n|\\r\\n)([\\s\\S]*?)#endregion(?:\\n|\\r\\n)?'#
class Result#
NEEDS_SUDO = 'needs sudo'#
MODIFIED = 'modified'#
NO_CHANGE = 'no change'#
install(conda_prefix)#
initialize(conda_prefix, shells, for_user, for_system, anaconda_prompt, reverse=False)#
initialize_dev(shell, dev_env_prefix=None, conda_source_root=None)#
_initialize_dev_bash(prefix, env_vars, unset_env_vars)#
_initialize_dev_cmdexe(prefix, env_vars, unset_env_vars)#
make_install_plan(conda_prefix)#
make_initialize_plan(conda_prefix, shells, for_user, for_system, anaconda_prompt, reverse=False)#

创建用于在 shell 中初始化 conda 的计划。

Bash:在 Linux 上,当打开终端时,会加载 .bashrc(因为它是交互式 shell)。另一方面,在 macOS 上,当在 Terminal.app 中执行时,默认会加载 .bash_profile。macOS 上的某些其他程序也会这样做,这就是为什么我们在 .bash_profile 中初始化 conda。在 Windows 上,打开 bash 的方式有很多种,具体取决于它的安装方式。Git Bash、Cygwin 和 MSYS2 默认都使用 .bash_profile。

PowerShell:PowerShell 可以将其路径存储在多个位置,具体取决于它是 Windows PowerShell、Windows 上的 PowerShell Core 还是 macOS/Linux 上的 PowerShell Core。解决此问题的最简单方法是询问 PowerShell 的不同可能安装位置,它们的配置文件在哪里。

run_plan(plan)#
run_plan_elevated(plan)#

此函数的策略在 Unix 和 Windows 之间有所不同。两种策略都使用子进程调用,其中子进程以提升的权限运行。使用子进程调用的可执行文件是 python -m conda.core.initialize,因此请参阅本模块底部的 if __name__ == "__main__"

对于 Unix 平台,我们将计划列表转换为 json,然后使用 sudo python -m conda.core.initialize 调用此模块,同时将计划 json 通过管道传递到 stdin。我们从 stdout 收集 json,以获取具有提升权限的计划执行结果。

对于 Windows,我们创建一个临时文件,其中保存计划的 json 内容。子进程读取文件的内容,使用更新的执行状态修改文件的内容,然后关闭文件。然后,此进程读取该文件的内容以获取各个操作的执行结果,然后删除该文件。

run_plan_from_stdin()#
run_plan_from_temp_file(temp_path)#
print_plan_results(plan, stream=None)#
make_entry_point(target_path, conda_prefix, module, func)#
make_entry_point_exe(target_path, conda_prefix)#
install_anaconda_prompt(target_path, conda_prefix, reverse)#
_install_file(target_path, file_content)#
install_conda_sh(target_path, conda_prefix)#
install_Scripts_activate_bat(target_path, conda_prefix)#
install_activate_bat(target_path, conda_prefix)#
install_deactivate_bat(target_path, conda_prefix)#
install_activate(target_path, conda_prefix)#
install_deactivate(target_path, conda_prefix)#
install_condabin_conda_bat(target_path, conda_prefix)#
install_library_bin_conda_bat(target_path, conda_prefix)#
install_condabin_conda_activate_bat(target_path, conda_prefix)#
install_condabin_rename_tmp_bat(target_path, conda_prefix)#
install_condabin_conda_auto_activate_bat(target_path, conda_prefix)#
install_condabin_hook_bat(target_path, conda_prefix)#
install_conda_fish(target_path, conda_prefix)#
install_conda_psm1(target_path, conda_prefix)#
install_conda_hook_ps1(target_path, conda_prefix)#
install_conda_xsh(target_path, conda_prefix)#
install_conda_csh(target_path, conda_prefix)#
_config_fish_content(conda_prefix)#
init_fish_user(target_path, conda_prefix, reverse)#
_config_xonsh_content(conda_prefix)#
init_xonsh_user(target_path, conda_prefix, reverse)#
_bashrc_content(conda_prefix, shell)#
init_sh_user(target_path, conda_prefix, shell, reverse=False)#
init_sh_system(target_path, conda_prefix, reverse=False)#
_read_windows_registry(target_path)#
_write_windows_registry(target_path, value_value, value_type)#
init_cmd_exe_registry(target_path, conda_prefix, reverse=False)#
init_long_path(target_path)#
_powershell_profile_content(conda_prefix)#
init_powershell_user(target_path, conda_prefix, reverse)#
remove_conda_in_sp_dir(target_path)#
modify_easy_install_pth(target_path, conda_source_root)#
make_dev_egg_info_file(target_path)#
make_diff(old, new)#
_get_python_info(prefix)#
temp_path#