1
votes

I am on a Mac Catalina 10.15.7. Visual Studio Code Version: 1.52.1

Suddenly, my conda environments are not able to find pandas (or any other) module that are clearly installed in the activated environments in the VSCode terminal.

Below, I show you the output from the terminal in VSCode. I have already activated my conda environment, dm_assay. I show that when I try to install pandas, it says 'already installed'. I list the conda packages, and you can see pandas in the list. But when I open python repl and import pandas, I get a ModuleNotFoundError: No Module named 'pandas'. This was not happening yesterday.

I opened the normal terminal (not VScode terminal), and did the same thing, and now pandas import works. (?)

What might be wrong with VSCode terminal the prevents python from accessing available packages in the active conda env?

The python paths change with activating and deactivating conda envs in the normal terminals, but do not change in the VSCode terminal.

I have tried restarting my computer. I want to use terminal in VSCode as it is very convenient. Both terminals are using bin/zsh shell.

(dm_assay) me@mycomp % conda install pandas

Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

(dm_assay) me@mycomp % conda list

# packages in environment at /usr/local/Caskroom/miniconda/base/envs/dm_assay:
#
# Name                    Version                   Build  Channel
blas                      1.0                         mkl  
ca-certificates           2021.1.19            hecd8cb5_0  
certifi                   2020.12.5        py38hecd8cb5_0  
colorful                  0.5.4                    pypi_0    pypi
intel-openmp              2019.4                      233  
joblib                    1.0.0              pyhd3eb1b0_0  
libcxx                    10.0.0                        1  
libedit                   3.1.20191231         h1de35cc_1  
libffi                    3.3                  hb1e8313_2  
libgfortran               3.0.1                h93005f0_2  
llvm-openmp               10.0.0               h28b9765_0  
mkl                       2019.4                      233  
mkl-service               2.3.0            py38h9ed2024_0  
mkl_fft                   1.2.0            py38hc64f4ea_0  
mkl_random                1.1.1            py38h959d312_0  
ncurses                   6.2                  h0a44026_1  
numpy                     1.19.2           py38h456fd55_0  
numpy-base                1.19.2           py38hcfb5961_0  
openssl                   1.1.1i               h9ed2024_0  
pandas                    1.2.1            py38hb2f4e1b_0  
pip                       20.3.3           py38hecd8cb5_0  
prettyprinter             0.18.0                   pypi_0    pypi
pygments                  2.7.4                    pypi_0    pypi
python                    3.8.5                h26836e1_1  
python-dateutil           2.8.1                      py_0  
pytz                      2020.5             pyhd3eb1b0_0  
readline                  8.0                  h1de35cc_0  
scikit-learn              0.23.2           py38h959d312_0  
scipy                     1.6.0            py38h2515648_0  
setuptools                51.1.2           py38hecd8cb5_4  
six                       1.15.0           py38hecd8cb5_0  
sqlite                    3.33.0               hffcf06c_0  
threadpoolctl             2.1.0              pyh5ca1d4c_0  
tk                        8.6.10               hb0a8c7a_0  
wheel                     0.36.2             pyhd3eb1b0_0  
xz                        5.2.5                h1de35cc_0  
zlib                      1.2.11               h1de35cc_3  

(dm_assay) me@mycomp % python3

Python 3.8.2 (default, Nov  4 2020, 21:23:28) 
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>> quit()

(dm_assay) me@mycomp % python3 --version Python 3.8.2

(dm_assay) me@mycomp % conda --version conda 4.9.2

(dm_assay) me@mycomp % pip --version pip 20.3.3 from /usr/local/Caskroom/miniconda/base/envs/dm_assay/lib/python3.8/site-packages/pip (python 3.8)

In VSCode

(dm_assay) me@mycomp % echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/Caskroom/miniconda/base/envs/dm_assay/bin:/usr/local/Caskroom/miniconda/base/condabin

(dm_assay) me@mycomp % echo $SHELL /bin/zsh

(dm_assay) me@mycomp project % which python

/usr/bin/python

in Normal Terminal

(dm_assay) me@mycomp ~ % echo $PATH

/usr/local/Caskroom/miniconda/base/envs/dm_assay/bin:/usr/local/Caskroom/miniconda/base/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

(dm_assay) me@mycomp ~ % echo $SHELL

/bin/zsh

(dm_assay) me@mycomp ~ % which python

/usr/local/Caskroom/miniconda/base/envs/dm_assay/bin/python

I notice that in the normal terminal, the python path (output of which python command) changes when I activate and deactivate conda environments. However, in the VSCode terminal, the python path remains the same (usr/bin/python) no matter if a conda env is activated or not.

Why isn't conda working in the VSCode terminal? How do I made it act like my normal terminal?

2
Is the (dm_assay) just inherited from the parent shell somehow, or does it actually correctly reflect that this is the active environment? - tripleee
The title of this question is really weird. I don't think "conda glitch" is transparent or particularly descriptive. Besides, there are probably several duplicate questions about environments not being activated correctly inside Visual Studio Code. - tripleee
(dm_assay) appears after I type conda activate dm_assay, so I assume it is a reflection of the active, but dysfunctional, environment. - NicoWheat

2 Answers

0
votes

I have experienced the same in the Windows operating system, and I figured out why that happens in windows, and I think it's the same reason in Mac also;
the problem is when you close the vscode with the opened terminal when you restart the terminal opens automatically, but at there whatever environment you have configured into that directory by using .vscode does not execute.
you can see that, because there is just the terminal without activation any env.

but if there is no terminal opened at the start, when you open the terminal it executes the environment activation command [most of the time]. automatically enviroment activation command has run

NOTE: the best way to solve this, whether or not the terminal has opened is;\ Click the little + icon I have circled by blue, then then it created a new terminal, the definitely your environment activates on time. Also, you can close the first terminal by selecting it in dropdown and click bin icon
this same problem appears even in the python virtual environment and pip environments. \

check that out this trick in Mac, because I'm not really sure.

0
votes

In VS Code, the internal terminal it uses is integrated from the system's own terminal. Therefore, if the system terminal can be used, the internal terminal of VS Code can also be used.

Usually, when VS Code uses the python environment, the modules are stored in "lib\site-packages" of the corresponding python environment. You could use the command "pip show pandas" in the VS Code terminal to check the installation location of the module "pandas":

enter image description here

In addition, it is recommended that you find the installation package of the module "pandas" in this environment, and then try to delete it and reinstall it to avoid problems due to damaged or incompatible files inside the module:

enter image description here

If it still doesn't work, please try to restart VS Code to let it reload the terminal.