简体   繁体   中英

Visual Studio Code Integrated Terminal not using correct Anaconda environment

I am using Visual Studio Code for a Python project using an Anaconda environment.

When I open up the integrated terminal I see this:

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$

The source and conda commands are typed for me because I have the Python => Terminal: Activate Environment open selected in my User Settings. It is attempting to activate the correct environment. This is also the environment I see in the bottom left of the window.

The problem is when I try to use Python or IPython I find the wrong Python version is executed. The path is all wrong also.

(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]: 

It is running Python from the default Anaconda environment, not the one that is supposedly activated. Here is the path:

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']

When I execute the same commands from a normal terminal I get the correct results:

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']

I am using the Python 2019.1.0 extension.

How do I get this to work? Is it possible for me to change the commands issued for me when I open the integrated terminal?

I got this to work.

The problem had to do with my PATH as set in my .bashrc file. Previously I had this:

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

Anaconda installations typically add the anaconda bin directory after the path, not before. I made this adjustment because I wanted the default Python in the terminal to be the Anaconda base environment and not the other environment installed on my machine.

To fix it I switched it back and added an activate command. This is probably the proper way to make the Anaconda Python environment the default Python in a terminal.

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

To debug this I looked at the PATH environment variable in my terminal and in VS Code's integrated terminal.

echo $PATH

VS Code's integrated terminal seems to do different things to the PATH environment variable when activating a conda environment, causing the different behaviors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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