Souvik Chanda
Souvik Chanda

Reputation: 31

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.

Upvotes: 3

Views: 9956

Answers (4)

Python_Learner
Python_Learner

Reputation: 1637

Remove (delete) the environment and recreate.

How I discovered this:

I created an environment without specifying a python version, conda put me on the latest version.

While installing packages, one of them required a lower version of python than installed by default. I downgraded and continued to install packages for my project.

I then spent hours trying to figure out why VS Code would throw ModuleNotFound errors and trying all the answers I could find.

Finally, I removed the environment, recreated it and VS Code worked.

Upvotes: 0

Computer&#39;s Guy
Computer&#39;s Guy

Reputation: 5363

Came to your answer having the same problem and in my case the conda environment was missing from the list of interpreters from the vscode command palette, specifically there was one with the wrong PATH, it had a <TOKEN> in the middle.

My solution

  • Open the command palette (Ctrl + Shift + P in linux)
  • Type > Python select interpreter and press enter
  • Paste the conda environment path, in my case /home/USER/miniconda3/envs/ldm

That will automatically work, didn't have to restart vscode, but you may want to try with the command Developer: Restart extension host from the command palette as well if you have an older version of vscode.

Upvotes: 0

Steven-MSFT
Steven-MSFT

Reputation: 8411

After you install the package you'd better reload the VSCode.

'Ctrl+Left-click' or 'F12' on the 'zipline' can navigation to the file under the zipline package?

Could you add these code in the python file?

import sys
print(sys.executable)
print(sys.path)

The outputs can show you which interpreter you are using and the locations where the interpreter looking for packages.

Upvotes: 0

Nishant Patel
Nishant Patel

Reputation: 1406

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

Upvotes: 4

Related Questions