Visual Studio Code configures Python programming environment

Programming Language: python
Integrated Development Environment (IDE): Visual Studio Code

Identify the Python language

Install the Python plugin (Extensions) in VScode
Python plugin

Explain the Python language

Python is a scripting language that does not need to be compiled . The interpreter is responsible for interpreting it into a machine language that the computer can process.

Download the Python interpreter

Python Official Download Site- Download the latest version for Windows

Install

Python installation interface
①Check Add python.exe to PATH, add environment variables
②Choose custom installation
Customize the installation interface
①Checked by default, Python documentation, must be installed
②Checked by default, pip.exe, recommended installation, used to download from PYPI (Python Package Index) Download and install Python third-party packages (packages)

In Python, packages are how you obtain any number of useful code libraries, typically from PyPI.

③The default development environment, the integrated development environment IDLE that comes with Python, is recommended to install

Check if the installation is successful

Open the command prompt (cmd), enter python, press Enter
If the installation is successful and the environment variable has been configured, the python version will be displayed
Successful installation

Reminder: Configure environment variables

设置
高级系统设置
高级
环境变量

environment variable
If there is a Path variable, click Edit (if not, create a new Path variable)
Edit environment variables
to create two new paths:
①Python installation path: configure the environment variable of Python.exe
②Python installation path\Scripts: configure the environment variable of pip.exe

Create a program file (.py)

Create a new folder as a Python workspace (WorkSpace) to store Python files
Open the folder in VSCode > File > Open Folder Create
a new Python file (.py) in VSCode > file > New File, and this folder exists middle

Prepare to run the environment

Programs written in the Python language must run in the Python environment, which includes an interpreter and a series of packages.
By default, any Python interpreter installed runs in its own global environment. Any package installed or uninstalled in the global environment affects all programs running in it. Installing too many packages in the global environment can cause all Program runs slowly.
To solve this problem, a virtual environment can be created for each workspace, which will contain a copy of the interpreter, and installing or uninstalling packages in this virtual environment will only affect the programs in the current project.

全局环境
虚拟环境1
虚拟环境2
虚拟环境3

Create a virtual environment

Ctrl+ Shift+ POpen the command panel (Command Palette), enter Python: Create Environment
Select environment type
Select to create a virtual environment with Venv
select timer
Select the interpreter used to create a virtual environment

The above operation is equivalent to entering the following code in Terminal:

python -m venv .venv

After the virtual environment is created, a folder .venvnamed

work in a virtual environment

Ctrl+ Shift+ POpen the command panel, enter Python: Select Interpreter
select interpreter
to select the interpreter in the virtual environment, and the virtual environment will be activated (when you open the Python file in this folder with Vscode again, it will run in the last selected environment by default)

The selected interpreter will be displayed in the status bar in the lower right corner of the interface:
Status Bar

The command to activate the virtual environment in Terminal is as follows:

.venv\scripts\activate

The command to close the virtual environment in Terminal is as follows:

deactivate

Install the package using pip

After the virtual environment is created, there will be files under the .venv > Script folder pip.exe. It is recommended to install the required packages in the virtual environment

Enter the following code in Terminal to update pip to the latest version

python -m pip install --upgrade pip

The display Requirement already satisfiedindicates that pip is already the latest version, as shown in the figure below:
new

The command to install the package in Terminal is as follows:

python -m pip install xxx

Guess you like

Origin blog.csdn.net/weixin_50249477/article/details/128701441