Home>Article>Development Tools> Let’s talk about how to configure the python environment in vscode

Let’s talk about how to configure the python environment in vscode

青灯夜游
青灯夜游 forward
2021-12-07 19:02:29 4566browse

How to configure the python environment invscode? The following article will introduce to you how to configure the python environment in Visual Studio Code. I hope it will be helpful to you!

Let’s talk about how to configure the python environment in vscode

Preliminary configuration

  • Install python (It is recommended to install it directly in the system directory and check Select 'ADD TO PATH' to avoid unnecessary trouble)

  • Install Visual Studio Code (the official website speed is sometimes fast and sometimes slow, it is recommended to download by chance, or use a reliable ladder to download ). [Recommended study: "vscode introductory tutorial"]

  • Check whether python and VSC are installed

    • python: In cmd or Enter python --version in the terminal. If the version number can be output, the installation is successful (the latest version: 3.9.1)
    • pip: python's current mainstream package management tool. In the latest version, pip will be along with python. Install together and complete the system variable settings. Enter pip -V[–version] in cmd or terminal. If the version number can be correctly output, the installation is successful

1. Establish a virtual environment (venv)

The role of venv can be understood as a python environment established for a specific project, which can be understood as a child. When we develop this project, we only need to operate the venv. Problems with the venv will not have an impact on the overall situation. To create venv, you need:

1. Create a folder in the appropriate path (you may wish to set it to venv-demo)

2. Run terminal or cmd in the folder, Enter the following command

python -m venv env

Then a folder named env will be generated under the folder, which is the virtual environment generated for us .

3. Enter the following command in terminal or cmd

Set-ExecutionPolicy -Scope CurrentUser remotesigned

Set the current user to be able to run wps scripts .

You can also run wps as an administrator and remove the -Scope attribute to set all users to be able to run wps scripts (those previously set can ignore step 3)

4. Enter env folder, execute the following command

.\Scripts\activate

to open the virtual environment. Enterexitto exit.

2. Configure pip

Due to well-known reasons, the installation speed of pip without modifying the source of pip in mainland China will be very impressive. So we need to configure pip in the virtual environment.

1. Under the premise of opening the virtual environment, enterpip config debugto check whether there is a corresponding pip configuration file. We only need to create the pip configuration file in the virtual environment.

2. Create the pip.ini file in the env folder

3. Open the pip.ini file and make the following settings:

[global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com

Many commercial companies and universities in China have established pip As for the mirror site, I personally think Alibaba is the best here. Set the index-url and trusted-host respectively, and runpip install [module_name]and the download speed will be acceptable.

3. Set up VSC

  • Open the venv-demo folder with VSC

  • At this time VSC may remind you to install the pylint package, just install it

  • Clickctrl shift pto open the settings panel and searchPython:Select Interpreteroption, select python.exe of the virtual environment env as the interpreter

  • At this time clickctrl~Open the terminal, you can see that you have automatically entered the virtual environment of env

  • Create the demo.py file in venv-demo, write anything, and then enterpython demo.pyin the terminal. It can run normally and the configuration is successful.

For more knowledge about VSCode, please visit:vscode tutorial! !

The above is the detailed content of Let’s talk about how to configure the python environment in vscode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete