Conda environment settings in VSCODE

table of Contents

1. The jupyter plug-in of VSCODE connects to the jupyter service in existing mode:

2. Command line terminal settings:


This article assumes that you:

Conda has been installed on WIN10 and jupyter has been installed in the main environment;

Virtual environment has been created;

The connection configuration between the virtual environment kernel and the main environment jupyter has been completed;

VSCODE and its python and jupyter plugins have been installed.

If it is not completed, please make the above preparations first.

 

Start the topic below:

 

1. The jupyter plug-in of VSCODE connects to the jupyter service in existing mode:

Why connect to an existing jupyter service instead of opening a local jupyter service directly in the jupyter plug-in of VSCODE? I also think, but it seems that the kernel switching operation of the jupyter plug-in can't really help me switch over, but it still stays in the previous environment. For example, in the figure below, the jupyter plug-in was originally in the main conda environment, python is 3.8, but I clicked on the upper right corner to switch to the conda virtual environment with python3.7, and then run the statement in the cell, the output python path and version still belong to python3 .8 conda main environment.

 

So, I don’t use the local mode at all. Instead, I start the jupyter service outside of VSCODE first, and then paste it into the jupyter plugin of vscode to connect in remote mode according to the obtained service address:

After starting the notebook from outside, copy the notebook server address, go back to the jupyter plugin, press Ctrl+Shift+P to open the command panel and enter the command:

Jupyter: Specify local or remote Jupyter server for connections

The menu of how to connect to jupyter pops up, as shown in the figure below. Select existing for the connection method, then paste and press enter.

There seems to be a small bug here. After pasting the notebook server address and pressing enter, the menu does not disappear. You need to press esc.

Then press Ctrl+Shift+P to open the command panel and enter the command:

Jupyter: Select a Kernel

As shown in the figure below, you can see the kernel that has been established on the server and which conda environment it belongs to. In addition, it also shows which conda environments can create a new kernel operation.

Here you can choose to connect to the already established kernel on the server, or you can create and connect to a new kernel on the server.

Finally, add the following content to the user's setting.josn:

    "jupyter.sendSelectionToInteractiveWindow": true,  //VSCODE的python编辑器中选定的的语句,按shift+enter后可传送到jupyter插件中执行,如果不需要这个功能,也可以注释掉

 

2. Command line terminal settings:

In fact, regarding the conda setting on VSCODE, the official website https://code.visualstudio.com/docs/python/environments#_conda-environments

Provides some precautions and provides a way for VSCODE to run in the conda virtual environment. First open the anaconda prompt, activate the environment, and then enter code. to start VSCODE.

 

But here we don't use the above method, but the following:

The idea is, inspired by the parameters in the anaconda startup shortcut, change the python command line terminal of vscode to cmd, and activate the conda environment at startup.

The specific operation is to add the following statement in the user's setting.json (references 1, 2):

    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe", //选用cmd作为命令行窗口
    "terminal.integrated.shellArgs.windows": [
        "/k",
        "D:\\anaconda3\\Scripts\\activate.bat D:\\anaconda3\\envs\\py3701" //此处修改为你conda虚拟环境文件夹位置
    ],
    "python.pythonPath": "d:\\anaconda3\envs\\py3701\python.exe",  //conda环境对应的解析器目录

Setting parameter reference:

1,https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration

2,https://blog.csdn.net/weixin_53927286/article/details/111601632

 

The scope of setting parameters is limited to user. If you want to set in the workspace, you need to press Ctrl+Shift+P to open the command panel and enter commands (references 3, 4):

Terminal: Manage Workspace Shell Permissions

3,https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration

4,https://github.com/microsoft/vscode/issues/104380

Guess you like

Origin blog.csdn.net/yocencyy/article/details/112504398