设置为使用带有VS Code和Integrated Git终端的anaconda时卡住 [英] Stuck when setting up to use anaconda with VS Code and Integrated Git terminal

查看:174
本文介绍了设置为使用带有VS Code和Integrated Git终端的anaconda时卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习数据科学,因此使用了一些非常流行的Python模块,例如Pandas,Matplotlib,Numpy等.因此,我清理了安装的Anaconda,现在将其用作我的默认Python解释器,还使用Conda来安装软件包和制作虚拟环境.我使用VS Code作为日常文本编辑器.但是,当使用Anaconda Python解释器在VS Code中使用集成的Git终端时,我遇到了一些问题.

I want to learn Data Science and so have used some really popular Python modules likes Pandas, Matplotlib, Numpy, etc. So I clean installed Anaconda and am now using it as my default Python interpreter and also using Conda for installing packages and making virtual environments. I use VS Code as my daily text editor. But I have run into some issues when using the integrated Git terminal in VS Code with the Anaconda Python interpreter.

我面临着两个问题.我看到的第一个问题是何时使用CMD运行Python.如果我在cmd中键入并输入python,则会出现anaconda提供的Python解释器.但我也收到警告:

There are a couple of issues that I am facing. One of the first issues that I see is when I am using CMD to run Python. If I type and enter python in cmd, the Python interpreter provided by anaconda comes up. But I also get a warning:

警告: 此Python解释器在conda环境中,但尚未激活该环境.库可能无法加载.要激活此环境,请参阅 https://conda.io/activation

Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation

我没想到会得到这个输出.无论如何,VS代码中还有另一个问题.但是首先,我想提一提的是,在安装Anaconda时,我已经选中了添加到PATH",因此那里没有任何问题.现在,当我用VS Code打开一个新的终端时,将自动运行C:/Users/User/Anaconda3/Scripts/activate,然后运行conda activate base.但是,如前所述,当conda activate base运行时,会自动出现CommandNotFoundError.它指出Your shell has not been properly configured to use 'conda activate'. If using 'conda activate' from a batch script, change your invocation to 'CALL conda.bat activate'

I didn't expect to get this output. Anyway, there's another problem in VS code. But first I would like to mention that I have checked "Add to PATH" when installing Anaconda so no issues there. Now, when I open a new Terminal in VS Code, automatically C:/Users/User/Anaconda3/Scripts/activate is run and then conda activate base is run. But when conda activate base is run, automatically, as mentioned, I get a CommandNotFoundError. It states Your shell has not been properly configured to use 'conda activate'. If using 'conda activate' from a batch script, change your invocation to 'CALL conda.bat activate'

然后告诉我初始化我的shell,所以我做了conda init bash,但还是没有运气.这使我可以谈论.bash_profile.我认为它必须与此bash配置文件有关.无论如何,这就是我的bash个人资料中的内容

And then I am told to initialize my shell, so I did conda init bash but still no luck. And this brings me to talk about .bash_profile. I think it has to do something with this bash profile. Anyway, this is what is in my bash profile


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

只是问题的摘要:

  • 运行Anaconda Python解释器时CMD中出现意外警告

  • Unexpected warning in CMD when running Anaconda Python interpreter

在VS Code中打开新终端时,自动运行Anaconda脚本并conda激活基础

Automatically run Anaconda Scripts and conda activate base when opening new Terminal in VS Code

Conda初始化bash无效

Conda init bash not helping

P.S我尝试在CMD和Git Bash中使用conda activate [env_name],它们可以正常工作.换句话说,Anaconda和Conda可以在VS Code终端之外完美运行.

P.S I have tried using conda activate [env_name] in CMD and also in Git Bash and they work without any issues. In other words, Anaconda and Conda work perfectly outside of VS Code terminal.

推荐答案

我自己已经找到答案了,想在这里分享.首先,在编写问题时,我正在使用Git Bash作为VS Code中的终端(我仍在使用它).因此,问题在于,当我在Git Bash或VS Code Terminal中运行命令conda init bash时,Conda基本上只是将用于激活Conda环境的命令放在.bash_profile中,因为它是在登录Bash期间获取的.但是,VS Code中的集成Terminal是Git Bash会话的子外壳.这就是为什么.bash_profile不在VS Code中提供的原因,因为.bash_profile仅在主Bash会话期间提供. .bashrc文件是在VS Code中创建终端会话时来源的文件.因此,您真正需要做的是获取conda init bash放入.bash_profile中的代码,并将其粘贴到您的.bashrc文件中,并自动将.bashrc文件作为该.bash_profile源.

I have figured out the answer myself and would like to share it here. First of all at the time of writing the question I was using Git Bash as my Terminal in VS Code (am still using it). So the issue was that when I ran the command conda init bash in Git Bash or the VS Code Terminal, Conda just basically put the command used for activating Conda environments in the .bash_profile since it is sourced during logging into Bash. But the integrated Terminal in VS Code is a subshell of a Git Bash session. That is why .bash_profile is NOT sourced in VS Code since .bash_profile is only sourced during the main Bash session. The .bashrc file is the file that is sourced when creating a Terminal session in VS Code. So what you actually need to do is take the code that is put into .bash_profile by conda init bash and paste it into your .bashrc file and make the .bash_profile source that .bashrc file automatically.

因此,总而言之,使用conda init bash会将conda命令放在.bash_profile中,并且通常由Git Bash提供,但是VS Code Git Bash终端将使用.bashrc.

So, to sum up, using conda init bash will put the conda command in the .bash_profile and it is usually sourced by Git Bash, but VS Code Git Bash terminal will use .bashrc.

因此,您可以将代码从.bash_profile剪切并粘贴到.bashrc(如前所述),或者如果需要,只需遵循以下步骤:将代码放入.bash_profile:

So you can just cut and paste the code from .bash_profile to .bashrc (as already mentioned) or if you want, just simply follow this: put this code in your .bash_profile:

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

然后在您的.bashrc中,输入以下代码:

And in your .bashrc, put this code:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('{path_to_your_conda.exe}' 'shell.bash' 'hook')"
# <<< conda initialize <<<
# You can get conda.exe path by using `which conda` in Git Bash

现在,当您在VS Code Terminal中打开Git Bash会话时,可以使用conda activate env_name激活您拥有的任何环境.

Now, when you open up a Git Bash session in VS Code Terminal, you can use conda activate env_name to activate any environments you have.

现在一切都应该在VS Code终端中按预期工作,但是我想进一步详细说明.如果需要,可以跳过conda init bash流程(不建议这样做,请继续阅读以获取其他知识,但请仅遵循上述步骤).这是conda 4.4.0中引入的功能.到那时为止,激活conda环境的方法是使用source activate,但是该命令不是跨平台的,这意味着该命令不能在Windows之类的OS中使用. 因此,他们通过引入以下命令进行了此更改:conda activate env_name,因此尽管有OS平台,conda环境也变得更加易于使用.

Everything is now supposed to work as expected in VS Code terminal but I would like to further elaborate about something. If you want, you can skip the conda init bash process (NOT recommended, just read on for additional knowledge but please follow the above steps only). This is a feature that was introduced in conda 4.4.0. Till then the way of activating conda environments was by using source activate but that command was NOT cross-platform, meaning that the command could not be used in OSes like Windows. So they made this change by introducing commands like: conda activate env_name so that conda environments become much easier to use despite the OS platform.

conda activate也具有其他优点.这直接来自他们的发布文档:

conda activate also has other advantages. This is directly from their release docs:

conda activate:重新激活了环境激活的逻辑和机制.在conda 4.4中,conda activate和conda deactivate现在是激活和停用环境的首选命令.您会发现它们比以前的conda版本中的源代码激活和源代码停用命令更加活泼. conda activate命令还具有以下优点:(1)在所有OS,shell和平台上通用,并且(2)与其他包(如Python virtualenv的Activate脚本)的脚本没有路径冲突.

conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activate and conda deactivate are now the preferred commands for activating and deactivating environments. You'll find they are much more snappy than the source activate and source deactivate commands from previous conda versions. The conda activate command also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like Python virtualenv's activate script.

我使用了此问题作为参考.查看它以了解更多信息.

I used this question as a reference. Check it out to learn more.

已经说过,即使您在VS Code Git Bash终端中使用source activate env_name仍然可以正常使用 . source activate env_name不需要事先设置或配置.但是强烈建议您仅使用conda init设置所有内容,然后使用conda activate env_name.

Having said that, using source activate env_name will still work if you are using Git Bash, even in VS Code Git Bash terminal. source activate env_name requires no prior set up or config. But it is highly recommended that you only use conda init to set everything up and then use conda activate env_name.

[注意] :在Windows上查找和修改所述的.bashrc.bash_profile通常并不像在Linux上那样简单.但是可以这样轻松完成:

[NOTE]: Locating and modifying the said .bashrc and .bash_profile on Windows is usually not as simple as it is on Linux. But can be done fairly easily like this:

不用说,但是,您应该已经安装了Git Bash.据我所知,安装Git Bash应该会自动创建.bashrc.bash_profile或两者都创建.这些文件称为点文件"(因为它们以点开头),默认情况下,这些文件在大多数OS上以及在Windows上都是隐藏的.如果它们是由Git Bash在您的系统上自动创建的,则很可能将它们放置在您的主目录中. Windows上的主目录为C:\Users\<you>\.话虽如此,请遵循以下步骤:

It goes without saying but, you should have Git Bash installed. Having Git Bash installed should, as far as I know, automatically create .bashrc or .bash_profile or maybe both. These files are called "dotfiles" (since they start with a dot) and these are by default hidden on most OSes and definitely on Windows. If they were auto-created by Git Bash on your system, it is most likely that they are placed in your home directory. Home directory on Windows is C:\Users\<you>\. With that said, follow this:

  1. 打开Git Bash并使用以下内容转到您的主目录:cd.只需键入此内容,您就会进入您的主目录
  2. 输入以下命令:ls -a,您将看到所有文件,甚至是隐藏文件.查找.bash_profile.bashrc.两者都应该存在.如果是这样,则准备按照上述说明进行操作.但是,如果其中一个不存在或两者都不存在,请使用以下命令创建它们:touch .bashrc && touch .bash_profile.现在,当您再次键入以下内容时,应该会看到这些文件:ls -a
  3. 就是这样.现在您已经有了.bashrc.bash_profile,您可以按照上面的说明进行操作.另外,要更快地访问这两个文件,请使用VS Code:code ~/.bashrccode ~/.bash_profile这样打开它们.现在,按照说明修改这两个文件.
  1. Open Git Bash and go to your home directory with: cd. Just type this and you will be in your home directory
  2. Enter this command: ls -a and you will see all your files, even hidden ones. Look for .bash_profile and .bashrc. Both should be present. If they are, you are ready to follow the above instructions. But if one is not there or if both are missing create them using: touch .bashrc && touch .bash_profile. You should now see these files when you again type: ls -a
  3. That's it. Now that you have your .bashrc and .bash_profile, you can follow the above instructions. Also, to access these two files quicker, open them like this with VS Code: code ~/.bashrc or code ~/.bash_profile. Now, modify these two files as per the instructions.

在问题中,我还谈到了自动激活Conda环境的VS Code. VS Code这样做没有问题,因为这是默认行为. 我将其误解为问题.但是,如果有人希望阻止VS Code自动执行此操作,则建议您尝试在用户设置中进行设置:

In the question, I have also talked about VS Code activating Conda environments automatically. There's no issue with VS Code doing that since this is the default behavior. I misinterpreted that as something that's an issue. But if anyone was looking to stop VS Code from automatically doing that, I would recommend trying to set this in the user settings:

"python.terminal.activateEnvironment": false

这篇关于设置为使用带有VS Code和Integrated Git终端的anaconda时卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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