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.
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
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.
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:
# 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:
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:
The Visual Studio Code extension has more than just programming capabilities:
Here are some other useful extensions and settings:
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:
Users can find and install any of the above extensions in the same way .
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.
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:
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:
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:
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.
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:
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:
You will See the terminal pane appear at the bottom of the window and display the code output.
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:
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:
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:
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.
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:
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.
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:
You can see these functions in the Debug view on the activity bar:
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.
Git integrationVS 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:If your project folder contains a .git folder, VS Code will automatically turn on all Git / GitHub features. You can perform many tasks:
All these features are available directly from the VS Code UI:
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:
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!