How to use VSCode write Python code? (Win10 x64 systems)

To form good habits, point a praise walk

Have questions, please private letters, comments, and I will see to reply


  1. Download VSCode of
    the country, if not through special methods VSCode official website download speed is very slow;
    you can also use my offer blue cloud playing links: VSCodeUserSetup-x64-1.43.0.exe download;
    if you can not meet the above requirements on the network to download the articles on VSCode dazzling selection of their own right;
  2. A directory, the directory two: Install the plug-ins and compiler


When just starting to learn Python, in accordance with the fool-operation with a good Python teacher provided, and found that it comes with a ide: IDLE,
Here Insert Picture Description

  • IDLE enough lightweight, open fast, not too many messy things (even if they are very useful for veterans, but novices will produce a variety of interference)
  • IDLE comes with Python Shell is an interactive, time for me to begin to learn Python, editing can quickly learn Python knowledge
  • However, IDLE, after all, sent me after my entry abandoned, because the interface pretty, not smart operation, function is not strong ...

I know there is a very strong ide: PyCharm, but, ah ~ ~ ~, I have not used it directly into the arms of the Anaconda
But sometimes, too professional, powerful software also means that the "heavy sex "(which is open Zhendi slow)

VSCode official website, it reads, very interesting

If your primary purpose for using Python is Data Science, then you might consider a download from Anaconda. Anaconda provides not just a Python interpreter, but many useful libraries and tools for data science

That is: If you mainly use Python scientific data, recommend the use of Anaconda

But sometimes I just want to write some simple everyday Python code only, then, VSCode into my sight;
then plan to use VSCode configure the Python environment, I occasionally meet the needs of the Python programming language, this process will also be recorded, hoping to help you

A directory: Python extension

  1. Open VSCode
  2. Click on the sidebar Manage extensions icon
    Here Insert Picture Description
  3. Search Python , click install to

VSCode write Python code, in addition to attaching the plug, also need to install their own compiler Python

Two directories: Python

Step One: Download Python Interpreter

Official website to download

Python official download - Download the latest version for Windows

  1. Select the Python version they need to be download, jump to another page

  2. Then drop down to the bottom of the page, you can see multiple systems for you to choose
    macOS,
    Windows x86-64 (64-bit)
    Windows x86 (32-bit)

  3. There are three Windows system file for you to download
    embeddable zip file, archive file
    web-based installer, network installation, ie, after downloading, run this stuff when it networking download Python
    Executable Installer, a exe file, provides an interface of installation instructions

I use the executable installer, not just because I need Python compiler, I like to use IDLE to write some simple Python code, because it is lightweight enough

I provide with

Step Two: Configure environment variables

Right this computer> Properties> Advanced System Settings> Advanced> Environment Variables

Select Path, click Edit

Click OK also repeatedly oh, just open the pages to turn off all by determining

I assume that the installation path is: D:\Python, the environment variable configured for:

  • D:\Python Configuration environment variable Python compiler
  • D:\Python\Scripts Pip configuration environment variable, we passed pip installation, management package, you need to use this environment variable

Step 3: Check and update pip

win + r, input cmd,

  • Enter the following command in the console
py -3 --version

Here Insert Picture Description
So you can explain, download success

  • The pip update to the latest version (pip typical application is to install a third-party Python packages from PYPI (Python Package Index))
    Enter the following command in the console
python -m pip install --upgrade pip

Please wait for it to finish downloading

Contents Three: build Python workspace

In the following operation, after some settings, is generated in the workspace configuration files, but they only apply to the current workspace, not global (global setting this article do not speak, because the set and watching the workspace after the configuration files have a little idea, you will set the global handy)

One: Create a new folder for storing Python code you write, the position to decide, is not recommended on the desktop

I direct a folder named Python

Two: Open the folder with Python VSCode

  1. The most crisp way: Select the folder> right-click> open by Code

  2. If VSCode not registered to your right-click menu, you honestly open VSCode> file> open folder

Three: Select interpreter

Python is an interpreted language, now you need to tell the interpreter which use VSCode

opening the Command Palette (Ctrl+Shift+P), start typing the Python: Select Interpreter command to search

Simply means that, in the VSCode, Ctrl + Shift + P or View> Command Palette, open command panel
input Python: Select Interpreter

select the installation path of Python (since I Anaconda installed, so there are two Python interpreter)

At this time, the automatic VSCode you generate settings.json file folder in .vscode
Here Insert Picture Description
four: Linter pylint

First create a Python file

the lower right corner VSCode will appear the following prompt, install to

Linter pylint tell VS Code this file .pyinterpreted as a Python program to VSCode use extensions and selected Python interpreter

After download the plug, which gives a prompt
Here Insert Picture Description
for the environment variable according to the requirements of its configuration, to add it to the path to the environment variable to

Three Ways to Run Python code: Directory four

Please ctrl + s before running the code, save the code oh

Method 1: Click the button

The easiest way to
the top right corner just click the editor of "Run Python File in Terminal" button

This button opens a terminal panel, which automatically activate your Python interpreter, and then runpython test.py
Here Insert Picture Description

Method two: right-click menu

Right-click in the editor window anywhere

Right menu has two options so
Here Insert Picture Description

  • Python File in Terminal RUN (it will run the complete code, and the code for you to save before running)
  • Selection RUN / Line Terminal in Python (which is used to run part of the code, sometimes very easy to use)
    select the part of the code, then right> Run Selection / Line in Python Terminal , or Shift + Enter

Method three: interactive interpreter

Ctrl + Shift + P or View> Command Palette, open command panel
inputPython: Start REPL

Open the interactive interpreter, as with the Python Shell function IDLE

interactive interpreter it can easily be used to test some of the Python syntax

Contents five: Debug

A: set breakpoints

To the left of the line number of a line of code, click on the following, a red dot
Here Insert Picture Description
II: initialize the debugger

F5 or Run> Start Debugging

Since this is the first time debug this file, so from the "command panel" opens a configuration menu, there are many types of debug configuration options we
Here Insert Picture Description
choose Python File ( use to display the currently selected Python interpreter to run in the current editor profile )

Debugging is a special run, the debugger will stop at the breakpoint in the first line

Here Insert Picture Description
add this:
In VSCode, the debugging work, you can use instead of the usual print Logpoints quick check variables. Logpoints similar breakpoints, except that the message to the console and it will not stop the program.

Interested can official website: Logpoints

Contents Six: installation and use of software packages

Python development so far, there are plenty of excellent code package for us to call

The most common is to install a third-party Python packages on PYPI (Python Package Index)

One:

  • When your Python code to call the third-party packages
  • Unless you are using Anaconda or before matplotlib package has been installed, you should see the message "ModuleNotFoundError: No module named 'XXX '"
  • This message indicates that the required packages on your system unusable

two:

  • We'd better avoid the installation package to the global interpreter environment, preferably using a virtual environment specific to the project, which includes a copy of the global interpreter
  • After activating the environment, then all the packages will be installed isolated from other environments. This isolation reduces the number of complex cases due to version conflicts arising.

Three: to create, activate the virtual environment

  • When you create a new virtual environment, VS Code will prompt you to set the default environment for the workspace folder. If you select this option, when you open a new terminal will automatically activate the environment
  • Ctrl + Shift + `or Terminal> New Terminal, open integrated terminal
  • Enter the following command
py -3 -m venv .venv

If when you enter the above command in a terminal Activate.ps1 IS not Digitally Signed. Not by You CAN RUN ON the this Script at The Current System , enter directly in the terminal Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Processcommand to allow PowerShell script execution

After command input, the following prompt message

Click Yes , equivalent to the following command (activated virtual environment)

.venv\scripts\activate

At this time, in the work area, VSCode will generate .venv folder, its subfolders Scripts, Lib \ site-packages stored packages we installed in this work area

In VSCode in, Ctrl + Shift + P or View> Command Palette, open a command panel
input Python: Select Interpreter, you need to select the version of Python Debugger

Xxx installation package, if desired, simply open the integrated terminal to enter the following command

python -m pip install xxx

Three: closed, open the virtual environment

shut down

deactivate

Open

.venv\scripts\activate

References:

Published 156 original articles · won praise 434 · views 210 000 +

Guess you like

Origin blog.csdn.net/Zhangguohao666/article/details/105040139