Visual Studio Code集成终端未使用正确的Anaconda环境

时间:2019-02-26 04:27:04

标签: python visual-studio-code conda

我正在使用Anaconda环境将Visual Studio Code用于Python项目。

打开集成终端时,我看到以下内容:

jim@main:~/Projects/ITP/thesis$ source /home/jim/INSTALL/anaconda3/bin/activate
(base) jim@main:~/Projects/ITP/thesis$ conda activate research
(research) jim@main:~/Projects/ITP/thesis$

为我键入了sourceconda命令,因为我在用户设置中选择了Python => Terminal:Activate Environment打开。它正在尝试激活正确的环境。这也是我在窗口左下方看到的环境。

问题是当我尝试使用Python或IPython时,我发现执行了错误的Python版本。路径也是错误的。

(research) jim@main:~/Projects/ITP/thesis$ which python
/home/jim/INSTALL/anaconda3/bin/python
(research) jim@main:~/Projects/ITP/thesis$ which ipython
/home/jim/INSTALL/anaconda3/bin/ipython
(research) jim@main:~/Projects/ITP/thesis$ ipython
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: 

它是在默认的Anaconda环境中运行Python,而不是应该被激活的环境。这是路径:

In [1]: import sys

In [2]: sys.path
Out[2]: 
['',
 '/home/jim/INSTALL/anaconda3/bin',
 '/home/jim/INSTALL/python',
 '/home/jim/INSTALL/ipython',
 '/home/jim/INSTALL/anaconda3/lib/python36.zip',
 '/home/jim/INSTALL/anaconda3/lib/python3.6',
 '/home/jim/INSTALL/anaconda3/lib/python3.6/lib-dynload',
 '/home/jim/.local/lib/python3.6/site-packages',
 '/home/jim/INSTALL/anaconda3/lib/python3.6/site-packages',
 '/home/jim/INSTALL/anaconda3/lib/python3.6/site-packages/IPython/extensions',
 '/home/jim/.ipython']

当我从普通终端执行相同的命令时,我会得到正确的结果:

jim@main:~$ source /home/jim/INSTALL/anaconda3/bin/activate
(base) jim@main:~$ conda activate research
(research) jim@main:~$ which ipython
/home/jim/INSTALL/anaconda3/envs/research/bin/ipython
(research) jim@main:~$ ipython
Python 3.7.2 (default, Dec 29 2018, 06:19:36) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys                                                                                                                                                                              

In [2]: sys.path                                                                                                                                                                                
Out[2]: 
['/home/jim/INSTALL/anaconda3/envs/research/bin',
 '/home/jim/INSTALL/python',
 '/home/jim/INSTALL/anaconda3/envs/research/lib/python37.zip',
 '/home/jim/INSTALL/ipython',
 '/home/jim/INSTALL/anaconda3/envs/research/lib/python3.7',
 '/home/jim/INSTALL/anaconda3/envs/research/lib/python3.7/lib-dynload',
 '',
 '/home/jim/INSTALL/anaconda3/envs/research/lib/python3.7/site-packages',
 '/home/jim/INSTALL/anaconda3/envs/research/lib/python3.7/site-packages/IPython/extensions',
 '/home/jim/.ipython']

我正在使用Python 2019.1.0扩展名。

如何使它正常工作?打开集成终端后,是否可以更改为我发出的命令?

1 个答案:

答案 0 :(得分:0)

我有这个工作。

问题与我的.bashrc文件中设置的PATH有关。以前我有这个:

export PATH="/home/jim/INSTALL/anaconda3/bin:$PATH"

Anaconda安装通常将anaconda bin目录添加到路径之后,而不是之前。我进行此调整是因为我希望终端中的默认Python是Anaconda基本环境,而不是计算机上安装的其他环境。

要修复此问题,我将其切换回了并添加了激活命令。这可能是使Anaconda Python环境成为终端机中默认Python的正确方法。

export PATH="$PATH:/home/jim/INSTALL/anaconda3/bin"
source /home/jim/INSTALL/anaconda3/bin/activate

为调试此问题,我在终端和VS Code的集成终端中查看了PATH环境变量。

echo $PATH

在激活conda环境时,VS Code的集成终端似乎对PATH环境变量执行了不同的操作,从而导致了不同的行为。