Home >Backend Development >Python Tutorial >How to write Python code on Windows? Excellent strategy is coming!
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?
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.
One of the coolest code editors a programmer can use, Visual Studio Code is an open source, extensible, and lightweight editor available on all platforms. 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:
We assume 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.
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.
MS Terminal open source address: https://github.com/microsoft/terminal
Of course, MS Terminal can already directly download the installation program. 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.
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 figure shows the support of this kind of multi-tab:
#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 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. Of course, if readers have better command line tool recommendations on Windows, they can also leave a message at the end of the article.
We have introduced the command line tools that are indispensable in development. Now we should talk about VS Code, which is the core tool that supports 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 recorded in detail, so I won’t go into details here:
##Python extensionAs mentioned above, VS Code passes the detailed The documented extension model supports development in multiple programming languages. The Python extension enables users to develop Python in Visual Studio Code with the following features:After opening the new file, you can enter the code.
As a test, we can quickly code the Sieve of Eratosthenes (which finds all prime numbers less than a known number). 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.
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, making VS Code can 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 folders and base files, the preferred method (described above) makes the following corrections:
When you open this way, VS Code understands and will use any virtualenv it sees , pipenv or conda environment. 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 a 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 How to write Python code on Windows? Excellent strategy is coming!. For more information, please follow other related articles on the PHP Chinese website!