Home > OS >  VSCode recognises conda environment but not pip packages in Unix
VSCode recognises conda environment but not pip packages in Unix

Time:05-25

I have a conda environment conda_env, which I activate in a Jupyter notebook on Visual Studio Code. The environment contains packages installed using both conda install and pip install.

The notebook resides in a Unix shell on a remote server. I am able to import packages installed with conda and residing inside the environment path (miniconda/envs/conda_env/...). However, when I try to import packages that were installed with pip, the import fails.

ModuleNotFoundError: No module named 'django-extensions'

I can import the packages if I start a Python environment on the command line inside the conda environment. Below shows the path the package is found in.

import django_extensions
print(django_extensions.__file__)
# ~/.local/lib/python3.9/site-packages/django_extensions/__init__.py

I eventually found a workaround solution by putting this line into the notebook.

sys.path.append("~/.local/lib/python3.9/site-packages/")

My question is, is there a way to do this across the entire VSC, without me having to manually put that line in inside every notebook?

Thank you!

CodePudding user response:

The fact that

sys.path.append("~/.local/lib/python3.9/site-packages/")

"solves" the issue indicates that the packages were installed with pip install --user which is not installing into the environment and generally is not recommended for Conda users. If you wish to install with Pip in an environment, do not use the --user flag.

Having packages installed in user site leads to confusion among users and I recommend users either avoid them altogether (delete any ~/.local/lib/python*) or install the package conda-forge::conda-ecosystem-user-package-isolation.

  • Related