简体   繁体   中英

Can't run Jupyter Notebook in VSCode - Vanilla Python, Windows 10

I have an existing vanilla Python installed on my Windows 10 computer and I do not want to reinstall Anaconda.

When trying to run code from ipynb file in vscode, I get the following error:

Error: Jupyter cannot be started. Error attempting to locate jupyter:
at A.startServer (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:786120)
at async A.ensureServerAndNotebookImpl (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:785575)
at async A.ensureServerAndNotebook (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:785376)
at async A.submitCode (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:1:782328)
at async A.reexecuteCell (c:\Users\[username]\.vscode\extensions\ms-python.python-2020.2.64397\out\client\extension.js:75:879318)

Also with the following error from VSCode: 在此处输入图像描述


Below are some of the things I have tried:

Check if the VSCode Extension in Correctly Installed

"Jupyter" extensions is deprecated. I had the "Python" plugin from Microsoft installed which contained Jupiter Notebook support.

Jupyter Installed Correctly

I tried reinstalling jupyter:

> python -m pip install --upgrade pip
> pip install jupyter
> pip install notebook

Tried to Run Jupyter on Terminal/Command Line

> jupyter notebook    //didn't work
jupyter : The term 'jupyter' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or   
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ jupyter
+ ~~~~~~~
+ CategoryInfo          : ObjectNotFound: (jupyter:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

This gave an error not able to find jupyter executable. According to this post I tried the following and it worked:

> python -m notebook

Check if pointing to the right interpreter

As per this post I tried:

  1. Press Command+Shift+P to open a new command pallete
  2. Type >Python: Select Intepreter to start jupyter notebook server

But I only had one version of Python installed on my computer, and doing this didn't make a difference.

Check the Path

There was this comment about PYTHONPATH in this post but since the python directory is correctly referenced and python works from the command line, I did not investigate it further.

One thing to note is pip installs to my "C:/Users/[username]/appdata/Roaming/Python-38/" folder while my Python is installed in "C:\Program Files\Python38-32\" .

If you are having similar issues, please try the above steps mentioned in the question.

After reading this article I realised that I had to map the scripts installed with pip as well, even if it is in my roaming directory. https://discuss.python.org/t/windows-appdata-roaming-vs-local/2682 . A confusion that costed me so many hours.

Here are the steps to add the variable:

  1. Go to environmental variables (if you don't know how, here is some instructions: https://www.techjunkie.com/environment-variables-windows-10/ )
  2. In the "User variables for [username]" section, Edit "PATH" variable. (it can't be System variables section because only you will have access to your own roaming folder)
  3. Add "C:\Users[username]\AppData\Roaming\Python\Python38\Scripts" (or where the pip installs the scripts to to the PATH variable.

Finally restart VSCode for the new environmental variable to be updated for VSCode. Now run the scripts in the ipynb file and it should work. It may also complain that it needs other modules, in which case you can use 'pip' to install it.

NB: if you are not constrained by having an existing python version on your computer and not wanting to install more, you can also use the Python Anaconda Distribution. https://www.anaconda.com/distribution/

NB: if you want jupyter note to work for all users using your computer, you need to configure pip to download installs to a directory that is not in your "C:\Users[username]" folder, and add a System variable to it.

For me, another solution helped. I'm not quite sure, what was the issue though, but somehow the state stored for the exact workspace made Python extension crash. VSCode stores the states for all the workspaces, in its global config folder under /Code/User/workspaceStorage/ . See the path to the settings.json in this help paragraph for your OS and then just replace the end of the path. For Windows, for example, the settings path is %APPDATA%\Code\User\settings.json , so the state storage is

%APPDATA%/Code/User/workspaceStorage/

In this directory, there are many subdirectories with some hex names, which are hard to relate to the workspaces. To find out the id of the workspace

  1. Open it in VSCode
  2. Help → Toggle Developer Tools
  3. In Console tab there execute the following to get the workspace id:
vscode.context.configuration()["workspace"]["id"]

Then you can delete the workspaceStorage subfolder by the id of the workspaces.

Another approach is by using the workspaceStorage folder contents themselves. Each of this folder contains a workspace.json which usually includes the path to the workspace. So I wrote a little python script to help me browse them. At the end of the script, there is a draft on removing all the workspaces for the remote containers. Feel free to modify it according to your needs.

from glob import glob
import os, json, sys
from shutil import rmtree

if sys.platform.startswith("win32"): path = os.environ["APPDATA"] + '/Code/User/workspaceStorage/' # Windows
elif sys.platform.startswith("darwin"): path = os.environ["HOME"] + '/Library/Application Support/Code/User/workspaceStorage/' # Mac OS
elif sys.platform.startswith("linux"): path = os.environ["HOME"] + '/.config/Code/User/workspaceStorage/' # Linux

for f in glob(path + "*/*.json"):
    with open(f) as fr:
        ws = json.load(fr)
    d = ""
    if "folder" in ws.keys():
        d = ws["folder"]
    elif "workspace" in ws.keys():
        d = ws["workspace"]
    elif "configuration" in ws.keys():
        d = ws["configuration"]["fsPath"]
    ws_path = os.path.dirname(f)
    print(d, ws_path)
    if d.startswith("vscode-remote://attached-container") or d.startswith("vscode-remote://dev-container"):
        print("Inside a container")
        # rmtree(ws_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM