Home >Backend Development >Python Tutorial >The best combination for writing Python code on Windows!

The best combination for writing Python code on Windows!

WBOY
WBOYforward
2023-04-14 13:31:03897browse

The best combination for writing Python code on Windows!

How to do Python development on Windows? Should I use a plain text editor like the masters do, or should I use a more complete IDE? Should I use the built-in command line tool, or do I need to install a new Terminal? This article will show you how to use MS Terminal and VS Code officially maintained by Microsoft to protect Python development.

One of the great benefits of using Windows is that it has so many applications, and even a powerful GPU can do other "work" in your free time. However, unlike Linux or macOS, developing on Windows will always encounter many challenges. Whether it is file encoding, environment control or project compilation, there will always be some magical gains during the development process.

These are especially prominent for beginners: we may have various dependency errors when installing a certain library, various encoding errors when we read and write text, etc.

So how to do Python development on Windows? I believe that the masters will have their own solutions, but this article hopes to introduce the Terminal and Visual Studio Code officially released by Microsoft, hoping that they can build a smoother Windows development experience.

The best combination for writing Python code on Windows!

Visual Studio Code is one of the coolest code editors a programmer can use, open source, extensible and lightweight that can be used on all platforms editor. It’s these qualities that make Microsoft’s VS Code popular and a great platform for Python development. Many readers may be familiar with common Python IDEs such as PyCharm and Jupyter Notebook, but VS Code will not disappoint you.

In this article, you will learn the features of Microsoft Terminal and Visual Studio Code, including:

What is Microsoft Terminal

  • How does Microsoft Terminal work?
  • ## Install Visual Studio Code
  • Discover and install Python extensions
  • Write simple Python applications
  • Learn how to run and debug existing Python in VS Code Procedure
  • Connect VS Code to Git and GitHub and share your code with the world
We assume that you understand Python development and already have some version of it installed on your system Python (such as Python 2.7, Python 3.6/3.7, Anaconda or others). Because VS Code is compatible with all major platforms, you may see slightly different UI elements and may need to modify some commands.

Emerging Microsoft Terminal

Windows Terminal is an open source terminal application launched by Microsoft at the Build developer conference in May this year. MS Terminal supports all the advantages of Command Prompt and PowerShell. Basically, the command line can be integrated with Linux. In addition, running the command prompt is no problem.

After MS Terminal was open sourced, the number of stars on GitHub grew very fast and currently exceeds 50,000. This is enough to show that this project has attracted great attention. With the open source improvements of the community, this tool must be very useful.

The best combination for writing Python code on Windows!

    MS Terminal open source address: https://github.com/microsoft/terminal
Of course, MS Terminal is currently available I downloaded and installed the program directly, and the community experience is also very good. Therefore, if we do Python development on Windows, the command line tool can use MS Terminal, which can solve a large part of package installation, environment control and other problems.

How is the effect of MS Terminal

The core function of MS Terminal is to support multiple tabs, and each tab can be connected to a command line shell or application, such as Command Prompt or Access Raspberry Pi, etc. via SSH. The following picture shows the support of this kind of multi-tab:

The best combination for writing Python code on Windows!

#In addition, in addition to functions, the more important thing is appearance, just like we often use zsh to provide more beautiful appearance The same command line. Although zsh currently has a GitHub collection of 94,000, the ReadMe document clearly states that it is best used on macOS or Linux. The newly released MS Terminal is much better than before in terms of interface and text style.

Background transparency and text highlighting can be customized, and symbols such as emoji can also be defined. The following is a basic display. We can adjust the entire interface according to our needs.

The best combination for writing Python code on Windows!

The entire project is still under active development, and many functions are being improved and added. However, since it is an open source project officially maintained by Microsoft, we are still very confident that at least the command line part can reduce various errors during the development process.

Installation and configuration VS Code

We have introduced the command line tools that are indispensable in development. Now we should talk about VS Code, which is the core tool to support Python development. Below we introduce how to use VS Code elegantly from initial installation and environment management to writing, testing, and publishing code.

Visual Studio Code can be installed on any platform. The official website provides complete installation instructions for Windows, Mac and Linux, and updates the editor monthly with new features and bugfixes. You can find all the installation content on the Visual Studio Code website:

The best combination for writing Python code on Windows!

# Additionally, aside from the similar name, Visual Studio Code (VS Code for short) is similar to the larger Windows-based Visual Studio has almost nothing else in common.

Visual Studio Code natively supports multiple languages, and it has an extension model with a rich ecosystem of support for additional components. VS Code is updated monthly, and you can learn about updates on the Microsoft Python blog. Any user can clone Microsoft's VS Code Github repository and contribute their own code

VS Code UI has been documented in detail and will not be described here:

The best combination for writing Python code on Windows!

Python Extensions

As mentioned above, VS Code supports development in multiple programming languages ​​through a well-documented extension model. The Python extension enables users to develop Python in Visual Studio Code, with the following features:

  • Supports both Python 3.4 and above, as well as Python 2.7 version
  • Completed using IntelliSense Code completion
  • Linting
  • Debugging support
  • Code snippet support
  • Unit test support
  • Automatically use conda and virtual environment
  • Code editing in the Jupyter environment and Jupyter notebooks

The best combination for writing Python code on Windows!

The Visual Studio Code extension has more than just programming capabilities:

  • Keymaps allow users who are already familiar with Atom, Sublime Text, Emacs, Vim, PyCharm or other environments to get started more easily.
  • Themes customize the UI whether you like to code in light, dark or something more colorful.
  • Language packs provide a localized experience.

Here are some other useful extensions and settings:

  • GitLens provides a number of useful Git features directly in the editing window, including non-responsible comments and repositories Develop features.
  • Auto-save can be easily performed by selecting File, Auto Save from the menu. The default delay is 1000 milliseconds and can be reconfigured.
  • Settings Sync allows users to synchronize their VS Code settings across different devices with GitHub. This helps keep the running environment consistent if users are working on different computers.
  • Docker makes it quick and easy for users to work with Docker, helping author Dockerfiles and docker-compose.yml, packaging and deploying projects, and even generating appropriate Docker files for projects.

Of course, you may find other extensions useful when using VS Code. Please share your findings and setups in the comments!

Click the "Extensions" icon on the Activity Bar to access and install new extensions and themes. Users can enter keywords to search for extensions, sort search results in a variety of ways, and install extensions quickly and easily. In this article, type python in the Extensions item of the activity bar and click Install to install the Python extension:

The best combination for writing Python code on Windows!

Users can find and install any of the above extensions in the same way .

Visual Studio Code Configuration File

It is worth mentioning that Visual Studio Code is highly configurable through User and Workspace Settings.

User settings are global across all Visual Studio Code instances, while Workspace Settings are local to a specific folder or project workspace. Workspace settings provide great flexibility to VS Code and will be mentioned throughout this article. Workspace settings are stored as .json files in a folder local to the project workspace named .vscode.

Start a new Python program

Let’s explore Python development in Visual Studio Code with a new Python program. In VS Code, type Ctrl N to open a new file. (You can also select "File" - "New" from the menu.)

No matter how you do it, you should see a VS Code window similar to the following:

The best combination for writing Python code on Windows!

After opening the new file, you can enter the code.

Enter Python code

As a test, we can quickly code the Sieve of Eratosthenes, which can find numbers smaller than a known number. all prime numbers). Type the following code into the new tab you just opened:

The best combination for writing Python code on Windows!

Wait, what’s going on? Why doesn't Visual Studio Code do any keyword highlighting, any automatic formatting, or anything really useful? What does it offer?

The answer is that VS Code does not know what type of file it is processing. The buffer is called Untitled-1, and if you look at the lower right corner of the window, you can see Plain Text.

To activate the Python extension, save the file (choose File-Save from the menu or File-Save File from the command palette or just use Ctrl S) as sieve.py. VS Code will see the .py extension and correctly convert the file into Python code.

Now your window view should look like this:

The best combination for writing Python code on Windows!

That’s much better! VS Code will automatically reformat the file into Python code, you can verify this by checking the language mode in the lower left corner.

If you have multiple Python installations (such as Python 2.7, Python 3.x, or Anaconda), you can change what VS Code wants by clicking the language mode indicator or selecting Python: Select Interpreter from the command panel. The Python interpreter used. By default, VS Code supports using pep8 format, but you can also choose black or yapf.

The best combination for writing Python code on Windows!

Now you can add the rest of the Sieve code. To see IntelliSense, type this code directly without cutting and pasting, and you should see something like this:

The best combination for writing Python code on Windows!

As you type the code, VS Code will The lines below the if statement are automatically and appropriately indented, adding closing brackets, and giving content hints.

Running Python Code

Now that the code is complete, you can run it. There's no need to have the editor do this: Visual Studio Code can run this program directly in the editor. Save the file (Ctrl S), then right-click in the editor window and select Run Python File in Terminal:

The best combination for writing Python code on Windows!

You will See the terminal pane appear at the bottom of the window and display the code output.

Editing an Existing Python Project

In the Sieve of Eratosthenes example, you created a Python file. This is great as an example, but many times, you need to create a larger project and develop on it over a longer period of time. A typical new project workflow might look like this:

  • Create a folder to hold the project (may contain a new GitHub project)
  • Change to new folder
  • Use the command code filename.py to create initial Python code

Using Visual Studio Code on a Python project (rather than a single Python file) opens up more capabilities and allows VS Code to really shine . Let's see how it works in a larger project.

Suppose we write a calculator program that parses equations written in infix notation using a variant of Edsger Dijkstra's dispatching field algorithm.

To illustrate the project-focused nature of Visual Studio Code, we now begin by recreating the dispatch yard algorithm in Python as an equation evaluation library. Corresponding GitHub address: https://github.com/JFincher42/PyEval.

After the local folder is created, you can quickly open the entire folder in VS Code. Since we have already created the folder and base files, the preferred method (as described above) makes the following correction:

  • cd /path/to/project
  • code .

When you open it this way, VS Code understands and will use any virtualenv, pipenv, or conda environment it sees. You don't even need to start the virtual environment first. You can open a folder on the user interface (UI) through File, Open Folder in the menu, Ctrl K, Ctrl O on the keyboard, or File, Open Folder in the command panel.

The following is the equation eval library project created:

The best combination for writing Python code on Windows!

When Visual Studio Code opens the folder, it will also open the last opened file again ( This is configurable). You can open, edit, run and debug any file listed. The Explorer view in the left activity bar provides a view of all files in a folder and shows how many unsaved files are in the current tab set.

Support for Code Testing

VS Code can automatically recognize existing Python tests written in the unittest, pytest, or Nose frameworks, but only if these frameworks are installed in the current environment. The author wrote a unit test for the equation eval library in the unittest framework, which you can use in this example.

To run an existing unit test for any Python file in the project, right-click and select Run Current Unit Test File. You will be prompted to specify the test framework, where to search for tests in the project, and the filename pattern to use for the tests.

All of these are saved as workspace settings in the local .vscode/settings.json file and can be modified. For this equation project, you can choose unittest, the current folder, and the pattern *_test.py.

After the test framework is set up and the tests are displayed, you can click Run Tests on the Status Bar and select an option from the command panel to run all tests:

The best combination for writing Python code on Windows!

You can also run individual tests by opening the test file in VS Code, clicking Run Tests on the status bar, and then selecting Run Unit Test Method and other specific tests to run. This makes it simple to resolve individual test failures and rerun the failed tests, saving a lot of time. Test results appear in the Output pane under Python Test Log.

Debugging support

Even if VS Code is the code editor, it is possible to debug Python directly in VS Code. VS Code provides many features that are comparable to good code debuggers, including:

  • Automatic variable tracking
  • Watch expressions
  • Breakpoints
  • Call stack inspection

You can see these functions in the Debug view on the activity bar:

The best combination for writing Python code on Windows!

The debugger can control the built-in A Python application running in a terminal or external terminal instance. It can be attached to an already running Python instance and can even debug Django and Flask applications.

Debugging code in a single Python file is as easy as pressing F5 to launch the debugger. You can press F10 and F11 to skip and enter functions respectively, and press Shift F5 to exit the debugger. Set a breakpoint by pressing F9 or by clicking the lift margin in the editor window.

Before you start debugging more complex projects, including Django or Flask applications, you first need to set up and select a debug configuration. Setting up a debug configuration is relatively simple. From the Debug view, select the Configuration drop-down, then select Add Configuration and Python: Create a debug configuration file under the folder that allows users to set specific Python configurations and debug application-specific settings such as Django and Flask.

You can also perform remote debugging and debug Jinja and Django templates. Close the launch.json file in the editor and select the correct configuration for your application from the Configuration drop-down list. The best combination for writing Python code on Windows!

Git integration

VS Code not only has built-in support for source code control management, but also supports Git and GitHub. You can install support for other SCMs in VS Code and use them side by side. Users can access source control from the Source Control view:

The best combination for writing Python code on Windows!

If your project folder contains a .git folder, VS Code will automatically turn on all Git / GitHub features. You can perform many tasks:

  • Commit files to Git
  • Push changes to and pull changes from remote repositories
  • check- out Existing or creating new branches and tags
  • View and resolve merge conflicts
  • View diffs

All these features are available directly from the VS Code UI:

The best combination for writing Python code on Windows!

VS Code also recognizes changes made outside the editor and behaves correctly.

Committing recent changes in VS Code is fairly simple. Modified files appear in the Source Control view and are marked with an M, while new untracked files are marked with a U. Hover over a file and click the plus sign ( ) to stage changes. Add a commit message at the top of the view and click the checkmark to commit your changes:

The best combination for writing Python code on Windows!

You can also push local commits to GitHub in VS Code. Select Sync from the Source Control view menu, or click Synchronize Changes on the status bar next to the branch indicator.

So in the author's opinion, Visual Studio Code is one of the coolest general-purpose editors and the best candidate tool for Python development. I hope you can also try using the Visual Studio Code editor in Python development, I believe you will not be disappointed.

The above is the detailed content of The best combination for writing Python code on Windows!. For more information, please follow other related articles on the PHP Chinese website!

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