简体   繁体   English

Windows 中的 Anaconda 和 Git Bash - conda:找不到命令

[英]Anaconda and Git Bash in Windows - conda: command not found

I've installed Anaconda and set Path environment variable to C:\Anaconda3; C:\Anaconda3\Scripts我已经安装了 Anaconda 并将 Path 环境变量设置为C:\Anaconda3; C:\Anaconda3\Scripts C:\Anaconda3; C:\Anaconda3\Scripts . C:\Anaconda3; C:\Anaconda3\Scripts

Then I try to run in Git Bash然后我尝试在 Git Bash 中运行

conda install python

But there is an error message "bash: conda: command not found".但是出现错误信息“bash: conda: command not found”。 I would like to know why.我想知道为什么。

To be able to run conda on gitbash you need to add it to the path.为了能够在 gitbash 上运行 conda,您需要将它添加到路径中。 Many times I've seen that's done by default - as shown in the setup for this workshop .很多次我都看到这是默认完成的 - 如本次研讨会的设置所示 If it doesn't, as it seems your case, then you can run their setup directly by running:如果没有,就像你的情况一样,那么你可以通过运行直接运行他们的设置:

. /c/Anaconda3/etc/profile.d/conda.sh

After running that you should be able to run conda commands.运行后,您应该能够运行 conda 命令。

To keep this setup permanently you can add such line on your .profile or .bashrc file ( read more about their differences ).要永久保留此设置,您可以在.profile.bashrc文件中添加这样的行(阅读有关它们差异的更多信息)。 A way of doing so is running the follwing:这样做的一种方法是运行以下内容:

echo ". /c/Anaconda3/etc/profile.d/conda.sh" >> ~/.profile

You may encounter problems if the path where Anaconda was installed contains spaces ( eg , C:\\Program Files ).如果安装 Anaconda 的路径包含空格(例如C:\\Program Files ),您可能会遇到问题。 In that case you would need to change the anaconda location or edit conda.sh script with something like:在这种情况下,您需要更改 anaconda 位置或使用以下内容编辑conda.sh脚本:

sed -e '/^_CONDA_EXE=.*/a alias myconda="${_CONDA_EXE/ /\\\\ }"' \
    -e 's/\$_CONDA_EXE/myconda/g' /c/Program\ Files/Anaconda3/etc/profile.d/conda.sh > conda_start.sh

This sed command inserts a new alias definition myconda which changes the anaconda path from Program Files to Program\\ Files so bash doesn't stop whit an error like:这个 sed 命令插入了一个新的别名定义myconda ,它将 anaconda 路径从Program Files更改为Program\\ Files因此 bash 不会因为出现以下错误而停止:

bash: /c/Program: No such file or directory

The second sed command replaces the _CONDA_EXE variable by the new alias created.第二个 sed 命令用创建的新别名替换_CONDA_EXE变量。

Since the above doesn't modify the file provided by anaconda, you will need to update your .profile file to load the file we've just created, conda_start.sh , instead.由于上述内容没有修改 anaconda 提供的文件,因此您需要更新.profile文件以加载我们刚刚创建的文件conda_start.sh

Joining @dvdgc13.加入@dvdgc13。 In my case, I fixed the problem by adding就我而言,我通过添加解决了问题

. C:/Users/user/Anaconda3/etc/profile.d/conda.sh

to my .bash_profile .到我的.bash_profile

在此处输入图片说明

First, you need to move to the directory where conda is located.首先,你需要移动到conda所在的目录

(some path such as C/Anaconda3/Scripts or ../miniconda3/Scripts or anaconda3/bin ) (某些路径,例如C/Anaconda3/Scripts../miniconda3/Scriptsanaconda3/bin

then, open the terminal.然后,打开终端。

( or, if you use Windows and can't find where the conda is, try moving to directory such as C:\\Users\\User_Name\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Anaconda3 (64-bit) and open the Anaconda prompt .) 或者,如果您使用 Windows 并且找不到 conda 所在的位置,请尝试移动到诸如C:\\Users\\User_Name\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Anaconda3 (64-bit)打开 Anaconda 提示符。)

Then, do this:然后,这样做:

conda init

or something like或类似的东西

conda init bash

if you use Mac OS:如果您使用 Mac 操作系统:

conda init zsh

will work well.会很好用。

if you wanna use different shell,如果你想使用不同的外壳,

conda init [shell_name]

[shell_name] can be: bash, cmd.exe, fish, powershell, tcsh, xonsh, zsh, etc. [shell_name] 可以是: bash、cmd.exe、fish、powershell、tcsh、xonsh、zsh等。

use conda init --help for more info.使用conda init --help获取更多信息。

I tried to do the same thing as you did but I couldn't get it to work.我试图做和你一样的事情,但我无法让它工作。 starriet had the working answer but I am going to make it easier for everyone else reading. starriet 有有效的答案,但我会让其他人更容易阅读。 You can directly open command windows with explorer instead of struggling with paths.您可以使用资源管理器直接打开命令窗口,而不必为路径而烦恼。

  1. Find your Anaconda3 folder with Windows Explorer This could be a user install where would be in your user folder such as "C:/Users/your_name/Anaconda3".使用 Windows 资源管理器查找您的 Anaconda3 文件夹这可能是用户安装,位于您的用户文件夹中,例如“C:/Users/your_name/Anaconda3”。

  2. Shift + Right Click on the explorer and click on " Open PowerShell Windows Here ". Shift + 右键单击资源管理器,然后单击“在此处打开 PowerShell Windows ”。 Note: you can just click " Git Bash " and you open Bash instead which makes no difference to the command.注意:您只需单击“ Git Bash ”,然后打开 Bash,这对命令没有任何影响。

Shift+右键单击

  1. Type in " conda init " in the PowerShell window.在 PowerShell 窗口中输入“ conda init ”。 This works even if you don't have the right paths because the command line looks for the right exe in the current dictionary.即使您没有正确的路径,这也有效,因为命令行会在当前字典中查找正确的 exe。 If you scroll down in the explorer, you should be able to find it.如果您在资源管理器中向下滚动,您应该能够找到它。

My PowerShell window would look a little different than yours because of my prompt but it makes no difference.由于我的提示,我的 PowerShell 窗口看起来与您的略有不同,但没有区别。 PowerShell 窗口

  1. Exit the PowerShell and open Git Bash.退出 PowerShell 并打开 Git Bash。 Type " conda " to confirm that things work.输入“ conda ”以确认一切正常。

I tried many ways, but they are incomplete until I run the following commands:我尝试了很多方法,但在我运行以下命令之前它们是不完整的:

  1. Go to the path of anaconda3 is C:\Users\USER_NAME\anaconda3 and open commend line over there and print the following: ( YOUR_PATH = C:\Users\USER_NAME\anaconda3 )转到 anaconda3 的路径是C:\Users\USER_NAME\anaconda3并在那里打开命令行并打印以下内容:( YOUR_PATH = C:\Users\USER_NAME\anaconda3 )

     echo 'export PATH="$PATH:[YOUR_PATH]:[YOUR_PATH]/Scripts"' >>.bashrc echo 'alias python="winpty python.exe"' >>.bashrc
  2. If Git bash is opened, close it and reopen it again and type the following to make sure anaconda and python work without problems:如果打开了 Git bash,请将其关闭并再次重新打开并键入以下内容以确保 anaconda 和 python 正常工作:

     conda --version python -- version

If you see the versions are printed, everything works well.如果您看到版本已打印,则一切正常。

In my case conda command was recognised in cmd, but not in bash.在我的例子中, conda命令在 cmd 中被识别,但在 bash 中不被识别。 I used conda init bash instead of simple conda init in cmd.我在 cmd 中使用了conda init bash而不是简单的conda init This command modified the .bash_profile file, which was residing in my C:\Users\xyz directory, and added the following code in it此命令修改了驻留在我的C:\Users\xyz目录中的.bash_profile文件,并在其中添加了以下代码

# >>> conda initialize >>>

# !! Contents within this block are managed by 'conda init' !!
eval "$('/C/Users/Saifullah/miniconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<

now it is working in bash terminal too.现在它也在 bash 终端中工作。

For MAC users, do this:对于 MAC 用户,请执行以下操作:

$ echo ". /usr/local/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc
$ source ~/.bashrc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM