3

I have used anaconda to set up multiple environments. I then use visual studio code to do all my editing. However, there seems to be a difference between what packages the jupyter / interactive window in vscode can find, and which packages the 'python.exe' called from the terminal can find.

Specifically: I have installed mne_bids using pip, since that was not in conda.

I can import this fine using the interactive window:

enter image description here

However, running the same bit of code from the dedicated terminal does not work:

python mne_test3.py
Traceback (most recent call last):
  File "mne_test3.py", line 3, in <module>
    import mne_bids
ModuleNotFoundError: No module named 'mne_bids'

So what do I need to change for me to be able to import the same packages in the terminal as well as the interactive window? Both of them are using the same conda environment. I have set the PYTHONPATH environment variable using the windows gui:

enter image description here

EDIT: Some helpful people are suggesting that I may have different conda environments activated. I don't think that is the case. In the image below can be seen the whole vscode interface:

enter image description here

Hopefully you can see that both the interactive window, as well as the powershell, are 'MNE' (which is the name of the conda environment with mne_bids in it).

It seems that the problem I have is similar to what is discussed in Visual Studio Code terminal doesn't activate Conda environment. I am working my way through some of the suggestions there.

Kaare
  • 531
  • 1
  • 7
  • 26
  • 1
    Are you changing the python interpreter in VSCode ? Not sure how anaconda handles this here, but virtualenv, for instance, doesn't and you end up needing to manually set a couple of stuff – Lucas Abreu Dec 14 '21 at 23:14
  • 1
    You shouldn't ever have to add the site packages directory to the PYTHONPATH – juanpa.arrivillaga Dec 15 '21 at 02:06

3 Answers3

3

In VSCODE, Press F1 and Type python: Select Interpreter. Or Press Python Kernel Button. Then you can select python interpreter that mne_bids module was installed.

Select Interpreter

Also, you can select kernel in VSCODE Jupyter Notebook(Interactive Window). Press F1 and Type Select Notebook Kernel or press select kernel button.

Select Kernel

And I think you should use conda virtual environment. See the documentation on managing Conda environments.

merv
  • 67,214
  • 13
  • 180
  • 245
BG Park
  • 169
  • 7
1

You have selected the different python environments for the Jupyter Interactive and python file.

You can get which python interpreter you have selected for python file from the bottom left of the VSCode, and which for Jupyter Interactive from the top-right.

You need not add PYTHONPATH in the system environment. And if you want to get the PYTHONPATH, you can take this:

import sys
from pprint import pprint
pprint(sys.path)
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
0

I stumbled on this thread:

Visual Studio Code terminal doesn't activate Conda environment

I tried both running 'conda init' in the terminal, and launching vscode from the anaconda prompt. somehow, a combination of these seems to have solved the issue, and when I start vscode from the start menu, I still have the option of changing the conda environment in the terminal...

Kaare
  • 531
  • 1
  • 7
  • 26