在 Git Bash 中设置 Conda

2025年9月25日 54点热度 0人点赞 0条评论

下面的内容转载自https://discuss.codecademy.com/t/setting-up-conda-in-git-bash/534473,本人测试效果相当好

A) Open Git Bash
A)打开 Git Bash

Now that you have an idea of how conda should look, go ahead and open Git Bash. As you can see, there is nothing on the prompt to indicate whether you are inside an active conda environment or not. Unfortunately, it’s not quite that simple. If you type conda list in Git Bash, you will receive an error that looks something like this:
现在你已经了解了 conda 应该是什么样子,继续打开 Git Bash。正如你所见,提示符上没有任何内容表明你是否处于活动的 conda 环境中。不幸的是,事情并没有那么简单。如果你在 Git Bash 中输入 conda list ,你会收到类似这样的错误:

bash: conda: command not found

This is because conda has only been set up to run from Anaconda Prompt by default.
这是因为 conda 默认仅设置为从 Anaconda Prompt 运行。

B) Add the conda shell script to your .bashrc
B)将 conda shell 脚本添加到你的 .bashrc

In order to make the conda command available in Git Bash, you need to add conda’s shell script to your .bashrc file. This is the same file that you store your bash aliases in (such as the sqlite3 alias you probably created when you followed these instructions).
为了使 conda 命令在 Git Bash 中可用,你需要将 conda 的 shell 脚本添加到你的 .bashrc 文件中。该文件也用于存储你的 bash 别名(例如,你可能在按照这些说明操作时创建的 sqlite3 别名)。

The shell script we need is located inside of the folder your Anaconda distribution added to your computer. If you remember where that is, you can navigate there using the File Explorer. If you don’t remember, the easiest way to find it is to use the search bar and search for “Anaconda3” and scroll down to the folders:
我们需要的 Shell 脚本位于 Anaconda 发行版添加到计算机的文件夹中。如果您记得它的位置,可以使用文件资源管理器导航到那里。如果您不记得了,最简单的方法是使用搜索栏搜索“Anaconda3”,然后向下滚动到文件夹:

Open this folder, then navigate to etc -> profile.d.
打开此文件夹,然后导航到 etc -> profile.d 。

Inside of the profile.d folder, you should see a file called conda.sh. This is the file you want to add to your .bashrc file.
在 profile.d 文件夹中,你应该看到一个名为 conda.sh 的文件。这是你想要添加到 .bashrc 文件的文件。

Now, there are a few ways you can do this: a) you can open your .bashrc and type it in there; b) you can type the path to conda.sh in Git Bash and add it to your .bashrc from there; or c) you can open Git Bash in the profile.d folder and utilize the bash command PWD in order to lower the risk of spelling errors in the path. The last option is my preferred choice, so that is what I will cover here.
现在,有几种方法可以做到这一点:a) 打开 .bashrc 文件并在其中输入;b) 在 Git Bash 中输入 conda.sh 的路径,然后将其添加到 .bashrc 文件;或者 c) 在 profile.d 文件夹中打开 Git Bash,并使用 bash 命令 PWD ,以降低路径拼写错误的风险。我更喜欢最后一种方式,因此我将在此介绍它。

Go ahead and close Git Bash, and then right click inside the file explorer and choose “Git Bash Here”:
继续关闭 Git Bash,然后在文件资源管理器中右键单击并选择“Git Bash Here”:

This will open a new instance of Git Bash to the correct directory.
这将打开 Git Bash 的新实例到正确的目录。

Now, so long as the path to your file has no spaces, you can add conda.sh to your .bashrc with this simple command:
现在,只要文件路径没有空格,您就可以使用以下简单命令将 conda.sh 添加到您的 .bashrc 中:

echo ". ${PWD}/conda.sh" >> ~/.bashrc

If the path does contain spaces (such as in your username), you will need to add single quotes in the command like so:
如果路径确实包含空格(例如您的用户名),则需要在命令中添加单引号,如下所示:

echo ". '${PWD}'/conda.sh" >> ~/.bashrc

After running this command, you will need to close Git Bash and reopen it for it to take effect.
运行此命令后,您需要关闭 Git Bash 并重新打开它才能生效。

C) Reopen Git Bash and activate conda!
C)重新打开 Git Bash 并激活 conda!

When you reopen Git Bash, you still won’t see (base) in your prompt — that’s because you haven’t activated conda yet! To activate it, simply type the command conda activate and hit Enter. You should see (base) pop up above your prompt:
当你重新打开 Git Bash 时,你仍然不会在提示符中看到 (base) ——那是因为你还没有激活 conda !要激活它,只需输入命令 conda activate 并按 Enter 键。你应该会看到 (base) 弹出在提示符上方:

image

And that’s it! You’ve successfully enabled conda in your Git Bash terminal. Now Git Bash can do virtually everything Anaconda Prompt can do, just make sure to use the conda activate command whenever you want to use conda in a new Git Bash window (you can also use conda deactivate if you want to exit your conda environment and use a different installation of Python).
就这样!您已成功在 Git Bash 终端中启用 conda 。现在,Git Bash 几乎可以完成 Anaconda Prompt 的所有功能,只需确保在新的 Git Bash 窗口中使用 conda 时使用 conda activate 命令即可(如果您想退出 conda 环境并使用其他 Python 安装,也可以使用 conda deactivate 命令)。

MuWinds

这个人很懒,什么都没留下

文章评论