如果您使用 --prefix
标志在 conda
中创建一个 python 环境并激活它,激活后环境将通过其整个路径显示。这可能是一条很长的路径,因此 conda-documentation建议如下修复。
conda config --set env_prompt '({name})'
The problem is when you
deactivate
this environment, then you don't go back to the default behavior. Even if you activate the base environment, it would show you the entire path of the base environment instead of just(base)
.
例如,我在C盘(Windows 10)的路径Users/username/Documents/GitHub/test_flask/.env
下安装了conda环境。
BEFORE
应用命令 (conda config --set env_prompt '({name})'
),激活后看起来像这样:
(C:\Users\username\Documents\GitHub\test_flask\.env) C:\Users\username\Documents\GitHub\test_flask>
AFTER
应用命令 (conda config --set env_prompt '({name})'
),如果我激活环境,它看起来像这样:
(.env) C:\Users\username\Documents\GitHub\test_flask>
太棒了!
但是现在如果我停用这个环境和/或激活我的 base
环境,我会得到这个:
'(Anaconda3)'C:\Users\username\Documents\GitHub\test_flask>
不过,我想回去:
'(base)'C:\Users\username\Documents\GitHub\test_flask>
So, how to fix this?
最佳答案
解决方案
事实证明,您需要执行以下操作才能恢复默认行为。
在这里,我们在 .condarc
文件中设置了 env_prompt
变量的默认行为,同样是在停用安装在非默认位置的环境之前。
假设您在项目目录下创建 python 环境目录 (.env
),如下所示:
conda env create --prefix ./.env -f envirnment.yml
按照以下步骤激活和停用环境。
# for activating env
conda config --set env_prompt '({name})'
conda activate ./.env
# for deactivating env
conda config --set env_prompt '({default_env})'
conda deactivate
conda activate base
env_prompt
变量的说明
来源:conda-config: .condarc
file
### .condarc file (env_prompt section)
# # env_prompt (str)
# # Template for prompt modification based on the active environment.
# # Currently supported template variables are '{prefix}', '{name}', and
# # '{default_env}'. '{prefix}' is the absolute path to the active
# # environment. '{name}' is the basename of the active environment
# # prefix. '{default_env}' holds the value of '{name}' if the active
# # environment is a conda named environment ('-n' flag), or otherwise
# # holds the value of '{prefix}'. Templating uses python's str.format()
# # method.
# #
# env_prompt: '({default_env}) '
关于anaconda - 如何恢复到 .condarc 中 env_prompt 参数的默认行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60122569/