Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code not detecting package in conda environment

I used conda install -c Quantopian zipline to install the zipline package in a new conda environment. I activated the conda environment from within VS Code and my settings.json reads as follows:

{
    "python.pythonPath": "C:\\Anaconda3\\envs\\zipline\\python.exe"
}

The bottom bar in my VS Code shows that the 'zipline' conda environment is being used. However, the following import statement is throwing a ModuleNotFoundError.

from zipline.examples import buyapple

Error:

Traceback (most recent call last):
  File "d:\Studies\nsedata\zipline_setup.py", line 1, in <module>
    from zipline.examples import buyapple
ModuleNotFoundError: No module named 'zipline'

When I am importing the same package from within VS Code terminal, there's no issue:

(base) PS D:\Studies\nsedata> conda activate zipline
(zipline) PS D:\Studies\nsedata> python
Python 3.6.10 |Anaconda, Inc.| (default, May  7 2020, 19:46:08) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from zipline.examples import buyapple
>>>

What am I doing wrong here and what can be a possible fix? Will appreciate any help on this.

like image 819
Souvik Chanda Avatar asked Oct 25 '25 20:10

Souvik Chanda


1 Answers

As i can see you are using conda environment, you need to specify pythonPath of that specific conda environment instead of Base Conda path.

In your case its 'zipline' so in Command Palette, search for your conda environment and select it as pythonPath. Refer below image:

Yse the Python: Select Interpreter command from the Command Palette

enter image description here

To activate your conda env

Add the below settings to your settings.json:

“terminal.integrated.shell.windows”:“C:\\Windows\\System32\\cmd.exe”
“terminal.integrated.shellArgs.windows”: [“/K”, “C:\\<path-to-conda-installation>\\Scripts\\activate.bat C:\\<path-to-conda-installation> & conda activate <your-env-name>”]

Restart your vscode once the above settings are in place.

You can also try this amazing extension for vscode as a add on https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

like image 72
Nishant Patel Avatar answered Oct 27 '25 11:10

Nishant Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!