Anaconda 环境 bash 前缀太长 [英] Anaconda environment bash prefix too long

查看:22
本文介绍了Anaconda 环境 bash 前缀太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 -p 选项指定路径的项目文件夹中创建了 anaconda 环境,即不在默认的 anaconda3/envs 文件夹中:

I created an anaconda environment in a project folder specifying the path with -p option, i.e. not in the default anaconda3/envs folder:

conda create -p venv

问题是,当我激活该环境时,终端中的 bash 前缀太长,即它会将环境的整个路径添加到提示符之前:

The problem is that when I activate that environment, the bash prefix in the terminal is too long, i.e. it prepends the entire path of the environment to the prompt:

(/path/to/the/environment/venv) user@machine: ~/path/to/environment/$

有没有办法解决这个问题,意思是缩短它或从提示中删除前缀?

Is there a way to fix this, meaning make it shorter or delete the prefix from prompt?

我的 $PS1:

[e]0;u@h: wa]${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$

推荐答案

Conda 提示自定义

自从 Conda v4.6.0env_prompt 配置选项可用于定制 PS1 更改.这是描述:

Conda Prompt Customization

Since Conda v4.6.0 there has been the env_prompt configuration option to provide for customization of the PS1 change. Here is the description:

$ conda config --describe env_prompt
# # 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}) '

对您的案例有帮助的一个选项是只使用 {name} 变量

One option that would help with your case would be to just use the {name} variable

conda config --set env_prompt '({name}) '

这将只显示环境的文件夹名称.例如,你的例子是

which will show only the env's folder name. E.g., your example would be

(venv) user@machine: ~/path/to/environment/$

注意,这将使 base 环境处于活动状态时,提示将显示 (anaconda3) 而不是 (base);否则,其他命名的环境应该照常出现.

Note, this will make it so that when the base env is active the prompt will show (anaconda3) instead of (base); otherwise, the other named envs should appear as usual.

如果你真的无法忍受,你可以运行 basename {default_env} 以在未命名的环境中获得与 {name} 相同的结果,并且仍然保留 基础.也就是说,

If you really can't stand that, you could run basename {default_env} to get the same result as {name} on unnamed envs and still retain base. That is,

conda config --set env_prompt '($(basename {default_env})) '

这篇关于Anaconda 环境 bash 前缀太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆