0

I have created a conda environment (say, called 'ds0') and installed some packages (e.g., python, pandas etc.). I then in vscode set the interpreter to be the one which I just created. I expect I could have my code working propery in the conda environment in vscode.

but then I have a problem, when I use the interactive mode in the ipynb file, I cannot import the packages, e.g.,

import pandas as pd  
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-11-f9ebee165770> in <module>
----> 1 import pandas as pd

ModuleNotFoundError: No module named 'pandas'

other code in the ipynb is running properly (e.g., to print a message etc.).

so it looks like vscode in the interactive mode is not using the right environment - but I did set the environment as 'ds0', and also if I save the ipynb file as a python script(e.g., as 'test1.py'), I can actually run it and vscode does recognize the pandas package.

so, how can I fix the issue so that I can run the code properly in the interacrive mode (e.g., in ipynb file).

** this is macbook m1 laptop, I just have the latest anaconda and vscode installed. I also tried to create additonal conda environments and the isse is the same. I am not sure if there is a problem with the ipykernel.

Thanks you!

Tao
  • 31
  • 1
  • 2
  • 6
  • When you run the code what do you see in the terminal output - and what conda environment does it specify there? How are you specifying the conda environment? – mullinscr Mar 21 '21 at 19:47
  • Does this answer your question? [Jupyter notebooks in vscode does not use active virtual environment](https://stackoverflow.com/questions/58119823/jupyter-notebooks-in-vscode-does-not-use-active-virtual-environment) – piterbarg Mar 21 '21 at 22:27
  • @piterbarg Thanks - but it does 'half' of the work. I installed ipython, ipykernel in the vscode terminal and the conda terminal (may be duplicated) manually - what happened was that, if I create a new ipynb file, I can select the both the interpreter and the kernel properly, and the code runs perfect - but if I open the previous ipynb file, I still have the problem - it looks like vscode is using different kernels for two different ipynb files. – Tao Mar 22 '21 at 20:07
  • @mullinscr thanks - when I use the code, it says, e.g., there is no module as 'pandas' etc. vscode is using a conda environment I just created (e.g., it is shown in the left bottom corner) but is not using the right kernel (the icon in the upper right hand side suggests that the jupyter server local is 'disconnected'). – Tao Mar 22 '21 at 20:10
  • Thanks both - I now 'cleaned' the problem, though I do not quite know if what I found was the culprit. What I did: I reinstalled the Mac OS Big Sur and I installed a clean miniconda and vscode however this does not solve the problem I deleted all the existing ipynb file and re- cloned from Github. this solves the problem - but I do not know why and how it solved the problem. – Tao Mar 22 '21 at 20:13
  • Glad you sorted it out, even if a bit of "dark magic" was involved. Would be good if you jotted down the steps you actually took as an Answer to your own question, for the good of the community (and the next poor soul with the same, or similar, problem) – piterbarg Mar 22 '21 at 20:18

2 Answers2

0

I now have a solution to the problem - though I do not quite know if what I found was the culprit.

What I did and what did NOT solve the problem:

I installed ipython, ipykernel in the vscode terminal and the conda terminal (may be duplicated) manually - what happened was that, if I create a new ipynb file, I can select the both the interpreter and the kernel properly, and the code runs perfect - but if I open the previous ipynb file, I still have the problem - it looks like vscode is using different kernels for two different ipynb files

What I did: I reinstalled the Mac OS Big Sur (this may be unnecessary though) and I installed a clean miniconda and vscode - however this does not solve the problem. vscode still does not use the right kernel (but it looks like it uses the right interpreter/env which I created via conda). It looks like vscode is not using the right kernel (the icon in the upper right hand side suggests that the jupyter server local is 'disconnected'), also it puts a warning msg saying 'invalid version -final'

What I did and what DID solve the problem:

Then I deleted all the existing ipynb file and re- cloned from Github. this solves the problem - but I do not know why and how it solved the problem.

Tao
  • 31
  • 1
  • 2
  • 6
0

In the top right corner, you can see the name of the kernel used ("Python 3.8.1 64-bit: Idle", for example). Click on that, and select the kernel you want to use. That's how I fixed the problem. The Kernel is named within the ipynb file, and if there's a mismatch between that and your system, the notebook will not run.