How to do Python development on Windows? Do you use a plain text editor like Yamami, or do you use a more sophisticated IDE? Do you want to use the built-in command line tool or install a new Terminal? This article will show you how to take advantage of Microsoft’s official maintenance of MS Terminal and VS Code to protect Python development. This article is written by Jon Fincher, author of The Heart of the Machine.

One of the great things about using Windows is that there are so many applications available that even a powerful GPU can do some other “work” in its spare time. Unlike Linux or macOS, however, there are always challenges to developing on Windows. Whether it’s file coding, environment control, or project compilation, there are always some magical things to be learned.

This is especially true for beginners: we can get all kinds of dependency errors when we install a library, we can get all kinds of coding errors when we read or write text, and so on.

So how do you develop Python on Windows? I’m sure the gods will have their own solutions, but this article will introduce Microsoft’s official Release of Terminal and Visual Studio Code to help build a smoother Windows development experience.

image

Visual Studio Code is one of the coolest Code editors available to programmers. It is an open source, extensible, and lightweight editor available on all platforms. It is these qualities that have made Microsoft’s VS Code so popular and a great platform for Python development. Most readers are probably familiar with common Python ides like PyCharm and Jupyter Notebook, but VS Code won’t disappoint you either.

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

  • What is Microsoft Terminal

  • How about Microsoft Terminal

  • Install Visual Studio Code

  • Discover and install the Python extension

  • Write simple Python applications

  • Know how to run and debug existing Python programs in VS Code

  • Connect VS Code to Git and GitHub and share your Code with the world

We assume that you know Python development and have some version of Python installed on your system (such as Python 2.7, Python 3.6/3.7, Anaconda, or others). Since VS Code is compatible with all major platforms, you may see slightly different UI elements, and some commands may need to be modified.

The emerging Microsoft Terminal

Windows Terminal is an open-source Terminal application that Microsoft unveiled at its Build developer conference in May. MS Terminal supports all the advantages of Command Prompt and PowerShell. Basically, the Command line can be integrated with Linux. Besides, running Command Prompt is no problem.

After MS Terminal opened source, GitHub’s Star volume has grown very fast and now exceeds 50,000. That’s enough to say that the project is getting a lot of attention, and with the community’s open source improvements, the tool will work well.

image

MS Terminal open source address: https://github.com/microsoft/terminal

Of course, now MS Terminal can be downloaded directly to install the program, the community experience is very good. So if we do Python development on Windows, the command line tool can use MS Terminal, which can solve a large part of the package installation, environment control, etc.

How does MS Terminal work

The core function of MS Terminal is to support multiple tabs, and each TAB can be connected to Command line shell or application, such as Command Prompt or accessing raspberry PI through SSH. The following figure shows the multi-tab support:

image

Also, more important than functionality is the level of appearance, as we often use ZSH to provide a more aesthetically pleasing command line. Although ZSH currently has a GitHub collection of 94,000, the ReadMe documentation clearly states that it is best used on macOS or Linux. The newly released MS Terminal is much stronger in both interface and text style.

Background transparency and text highlighting can be customized, as can emoji and other symbols. The following is a basic display, and we can adjust the entire interface to suit our needs.

image

The whole project is still under active development, and many functions are being improved and added. However, since it is officially an open source project maintained by Microsoft, we are confident that at least on the command line we can reduce the number of errors reported during development. Of course, if readers have better command-line tool recommendations on Windows, they can also leave a comment at the end of this article.

Install and configure VS Code

Now that we’ve introduced the essential command-line tools for development, it’s time to talk about VS Code, the core tool that supports Python development. Here’s 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 website provides complete installation instructions for Windows, Mac, and Linux, and the editor is updated monthly with new features and bug fixes. You can find all the installation content on the Visual Studio Code website:

image

In addition, Visual Studio Code (VS Code for short) has little in common with the larger, Windows-based Visual Studio except for its name.

Visual Studio Code itself supports multiple languages, and one of its extended models has a rich ecosystem that supports other components. VS Code is updated monthly, and you can read about the updates on the Microsoft Python blog. Any user can clone Microsoft’s VS Code Github repository and contribute their own Code.

VS Code UI is well documented and will not be covered here:

image

Python extension

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

  • Both Python 3.4 and later and Python 2.7 are supported

  • Code completion is done using IntelliSense

  • Linting

  • Debugging support

  • Snippet support

  • Unit test support

  • Automatic use of Conda and virtual environments

  • Code editing in Jupyter environment and Jupyter notebook

image

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

  • Keymaps allows users who are already familiar with Atom, Sublime Text, Emacs, Vim, PyCharm, or other environments to get started easily.

  • Theme custom UI, whether you like to code in bright, dark, or more colorful places.

  • The language pack provides a localized experience.


Here are some other extensions and Settings that might be useful:

  • GitLens provides a number of useful Git features directly in the edit window, including non-responsible comments and repository development capabilities.

  • Automatic saving can be easily done by selecting File, Auto Save from the menu. The default delay is 1000 milliseconds, which can be reconfigured.

  • Settings Sync allows users to synchronize their VS Code Settings across devices using GitHub. If users are working on different computers, this helps to keep the operating environment consistent.

  • Docker allows users to use Docker quickly and easily, helping to compose Dockerfiles and docker-comemater.yml, package and deploy projects, and even generate the appropriate Docker files for projects.

Of course, you may find other useful extensions while working with VS Code. Please share your findings and Settings in the comments!

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

Win platform to do Python

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

Visual Studio Code configuration file

It is worth noting that Visual Studio Code implements height configuration through User and Workspace Settings.

User Settings are global in all Instances of Visual Studio Code, while Workspace Settings are local to a particular folder or project Workspace. Workspace Settings provide great flexibility for VS Code and will be covered throughout this article. Workspace Settings are stored as a. Json file in a project workspace local folder 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 choose “File” – “New” from the menu.)

No matter how you do it, you should see a VS Code window that looks something like this:

image

Once you open 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 all primes smaller than any known number). In the new TAB that just opened, type the following code:

image

Wait, what the hell is going on here? Why isn’t Visual Studio Code doing any keyword highlighting, auto-formatting, or anything really useful? What does it offer?

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

To enable the Python extension, Save the File (select File-Save from the menu or file-Save File from the command panel or just use Ctrl + S) as Sieve.py. VS Code will see the.py extension and correctly convert the file to Python Code.

Your window view should now look like this:

image

That’s better! VS Code automatically reformats files into Python Code, which you can verify by checking the language patterns in the bottom left corner.

If you have multiple Python installations (such as Python 2.7, Python 3.x, or Anaconda), you can select Python by clicking on the language mode indicator or from the command panel: Select Interpreter to change the Python Interpreter VS Code will use. VS Code supports pep8 by default, but you can also choose black or YAPF.

image

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

When you type Code, VS Code automatically indents the lines below the for and if statements appropriately, adds a close bracket, and gives you a hint for the content.

image

Running Python code

Now that the code is complete, you can run it. There is no need for the editor to do this: Visual Studio Code can run the 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’ll see the terminal pane at the bottom of the window, showing the code output.

Edit existing Python projects

In the Sieve of Eratosthenes example, you created a Python file. That’s good as an example, but a lot of times, you need to create bigger projects and develop on them over longer periods of time.


A typical new project workflow might look like this:

  • Create a folder to hold projects (possibly including a new GitHub project)

  • Change to a new folder

  • Create the initial Python code using the command code filename.py

Using Visual Studio Code on Python projects (rather than individual Python files) opens up more functionality to make VS Code really work. Let’s see how it works in a larger project.

Let’s say we write a calculator program that uses a variant of Edsger Dijkstra’s scheduling field algorithm to parse the equations written by infix notation.

To illustrate the project-centric nature of Visual Studio Code, we will now start recreating the scheduling field algorithm in Python as an equation evaluation library. Making corresponding address: https://github.com/JFincher42/PyEval.

Once the local folder is created, you can quickly open the entire folder in VS Code. Since we have created folders and base files, the preferred method (as described above) is modified as follows:

cd /path/to/project
code .
Copy the code

When you open this way, VS Code knows 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 folders on the USER interface (UI) by using File, Open Folder in the menu, Ctrl+K, Ctrl+O on the keyboard, or File, Open Folder in the command panel.

Here is the equation Eval library project created:

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

image

Support for code testing

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

To Run existing Unit tests for any Python files in the project, right-click and select Run Current Unit Test File. You will be prompted to specify the test framework, search the project for the location of the test, and the file name pattern used by the test.

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 select Unittest, the current folder, and the schema *_test.py.

After the test framework is set up and the Tests are displayed, you can Run all the Tests by clicking Run Tests on the Status Bar and selecting an option from the command panel:

image

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 any other specific Tests to Run. This makes it easy to resolve individual test failures and rerun failed tests, which can save a lot of time. The Test results are displayed in the Output pane under Python Test Log.

Debugging support

Even though VS Code is a Code editor, it is possible to debug Python directly in VS Code. VS Code offers many features comparable to a good Code debugger, including:

  • Automatic variable tracking

  • Monitor expression

  • The breakpoint

  • Call stack check

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

image

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

Debugging code in a single Python file is as easy as pressing F5 to start the debugger. You can skip and enter functions by pressing F10 and F11 respectively, and exit the debugger by pressing Shift + F5. Press F9 to set a breakpoint, or by clicking on the left lift Margin in the editor window.

Before you can start debugging more complex projects, including Django or Flask applications, you first need to set up and select debug configurations. Setting up the debug configuration is relatively simple. Select the Configuration dropdown from the Debug view, and then select Add Configuration and Python:

image

Visual Studio Code will create a debug configuration file in a folder currently named.vscode/launch.json, which allows the user to set up specific Python configurations and debug specific application 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 the application from the Configuration drop-down list.

Git integration

VS Code has built-in support for source control management as well as 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:

image

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

  • Commit the file to Git

  • Push changes to and retrieve them from a remote repository (remote Repo)

  • Check-out Existing or creating new branches and tags

  • View and resolve merge conflicts

  • View Diffs

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

image

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

Committing your latest changes in VS Code is fairly simple. The modified file is displayed in the Source Control view with the M tag, while the new untraced file uses the U tag. Hover over the file and click the plus sign (+) to temporarily save the changes. Add a commit message at the top of the view, then click the check mark to commit the changes:

image

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 out there and the best candidate for Python development. Hopefully, you can also try using the Visual Studio Code editor in Python development, and you won’t be disappointed.

Refer to the article