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.
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 Program
- Connect VS Code to Git and GitHub and share your code with the world
We assume you know Python development and have Some version of Python is installed on the system (such as Python 2.7, Python 3.6/3.7, Anaconda, or other). 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.
- 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:
#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.
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:
# 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:
- Supports both Python 3.4 and higher and Python 2.7 versions
- Complete 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
- Keymaps allow users 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 pack provides localized experience.
- GitLens provides a lot of useful Git features directly in the editing window, including non-responsible comments and repositories Develop features.
- Auto-save is easy 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.
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:
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 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.
Run 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.
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 (possibly containing a new GitHub project)
- Change to New folder
- Create initial Python code using the command code filename.py
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 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:
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:
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
- Monitoring expressions
- Breakpoints
- Call stack inspection
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. WeChat search public account: Linux technology fan, reply: linux receive information.
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.
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 a remote repository (remote repo) and pull changes from it
- 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:
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 Great combination for writing Python code on Windows!. For more information, please follow other related articles on the PHP Chinese website!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.