I’m trying to run an ipynb file in vscode and I’m stuck. The extension host keeps crashing and I think I might’ve found a culprit.
Some info about what I’m working with:
- I’m working on a relatively old macOS (10.13.6)
- I have a completely fresh anaconda install (conda 23.7.2)
The steps that I took to find the error:
- When I run
python --version
in my base environment in the terminal I getPython 3.11.5
.which python
gives me/Users/myname/anaconda3/bin/python
- When I run the same in the terminal from the environment
/Users/myname/Projects/myproject/.conda
I getPython 3.11.5
.which python
gives me/Users/myname/Projects/myproject/.conda/bin/python
- When I run the same from any environment in vscode I get
Python 2.7.16
.which python
gives me/usr/bin/python
I think this might be the reason why the Jupyter extension keeps crashing. (the only code I’m trying to run is print("hello")
)
2
Answers
There are multiple Python environments in your environment, and you need to set it in VSCode.
You can use shortcuts Ctrl+Shift+P and type "Python: Select Interperter" to choose your python 3.11.5.
Then you have to choose the Python kernel in the jupyter-notebook.
Read document about jupyter-notebook environment for more details.
I found myself facing the same error some time ago and the issue you’re facing is a compatibility issue between the Python version used by VSCode’s Jupyter extension and the environment you’re trying to run your code in.
First, check Jupyter extension settings by opening VSCode settings (File > Preferences > Settings) and search for "Jupyter". Make sure that you have selected the correct Python interpreter in the Jupyter extension settings.
Next, create a new conda environment specifically for your project. This will help isolate your project’s dependencies and avoid potential conflicts.
Once you’ve created the environment, activate it in VSCode. Open a new terminal within VSCode and activate the environment.
In the bottom status bar of VSCode, click on the interpreter version and select the Python interpreter from the newly created environment (e.g., myenv). This ensures that VSCode uses the correct Python interpreter for running your code.
Finally, after selecting the correct interpreter, close and reopen VSCode to make sure the changes take effect.
Now try running your Jupyter notebook again. Hopefully, it will use the correct Python interpreter and resolve any version compatibility issues. If the extension still crashes, check the Jupyter extension logs for more detailed error messages and post them here. (To access the logs, go to the Output section of VSCode (View > Output) and select "Jupyter" from the dropdown.)
Hopefully everything will work out.