Visual Studio Code for Python Programmers
()
About this ebook
Become proficient and efficient with Visual Studio Code and learn how to integrate all your external tools!
Visual Studio Code for Python Programmers helps Python developers become not just familiar, but productive in Visual Studio Code. To start, you’ll find the steps for installing Visual Studio Code on Windows, Mac and Linux platforms, along with an introduction to the editing features of the workspace. Coverage of more advanced functionality includes managing source code, debugging, unit testing, and Jupyter Notebook support. The book finishes with a walk-through of real-world projects which utilize Visual Studio Code features introduced in the book.
For developers, the choice of an editor is a very personal one. You have idiosyncratic needs and wants that are unique to you as a developer. This book will help you learn how to customize Visual Studio Code to meet your needs and Python development workflow.
- Introduces you to the features of the Visual Studio Code workspace and how those features can be customized
- Demonstrates how Visual Studio Code allows you to choose your structure according to your needs
- Covers editing code in Python, including syntax highlighting, code completion, object definition, refactoring, and code navigation
- Describes Git integration and how to perform common Git functions (commits, checkouts, branches, and merges) from within Visual Studio Code
- Highlights debugging features for Python developers
Related to Visual Studio Code for Python Programmers
Related ebooks
Python and SQLite Development Rating: 0 out of 5 stars0 ratingsPyqt6 101: A Beginner’s Guide to PyQt6 Rating: 0 out of 5 stars0 ratingsVisual Studio Code: End-to-End Editing and Debugging Tools for Web Developers Rating: 0 out of 5 stars0 ratingsArtificial Intelligence Programming with Python: From Zero to Hero Rating: 0 out of 5 stars0 ratingsPython Real-World Projects: Craft your Python portfolio with deployable applications Rating: 0 out of 5 stars0 ratingsMastering PyCharm Rating: 5 out of 5 stars5/5C# Programming Cookbook Rating: 0 out of 5 stars0 ratingsFlask By Example Rating: 0 out of 5 stars0 ratingsFlask Framework Cookbook Rating: 5 out of 5 stars5/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Mastering IPython 4.0 Rating: 0 out of 5 stars0 ratingsMastering Git Rating: 0 out of 5 stars0 ratingsPython: Best Practices to Programming Code with Python Rating: 0 out of 5 stars0 ratingsGit Essentials Rating: 4 out of 5 stars4/5Enterprise Automation with Python: Automate Excel, Web, Documents, Emails, and Various Workloads with Easy-to-code Python Scripts Rating: 0 out of 5 stars0 ratingsLearning Flask Framework Rating: 4 out of 5 stars4/5PySide GUI Application Development - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Pandas 2.0: A Comprehensive Guide to Data Manipulation and Analysis for Data Scientists and Machine Learning Professionals Rating: 0 out of 5 stars0 ratingsStart-to-Finish Visual C# 2015 Rating: 5 out of 5 stars5/5Get Programming: Learn to code with Python Rating: 0 out of 5 stars0 ratingsModular Programming with Python Rating: 0 out of 5 stars0 ratingsLearning pandas - Second Edition Rating: 4 out of 5 stars4/5Building Python Real-Time Applications with Storm Rating: 0 out of 5 stars0 ratingsNumPy Cookbook Rating: 5 out of 5 stars5/5Clojure Programming Cookbook Rating: 0 out of 5 stars0 ratingsProgramming Problems in Ruby Rating: 0 out of 5 stars0 ratingsProfound Python Libraries Rating: 0 out of 5 stars0 ratingsThe SQL Workshop: Learn to create, manipulate and secure data and manage relational databases with SQL Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsSQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5C# 7.0 All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsPython: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Narrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5
Reviews for Visual Studio Code for Python Programmers
0 ratings0 reviews
Book preview
Visual Studio Code for Python Programmers - April Speight
Introduction
What started as an announcement at Microsoft Build 2015 has evolved into the Most Popular Development Environment, as ranked in the 2019 Stack Overflow Developer Survey. Visual Studio Code is a free, open-source, cross-platform code editor developed by Microsoft as part of the Visual Studio family. In comparison to its Visual Studio counterpart, Visual Studio Code is a streamlined code editor for a quick code-build-debug cycle. This feature-rich editor includes support for code completion, refactoring, formatting, managing source code, collaboration, debugging, unit testing, and more.
This book introduces Visual Studio Code through the lens of a Python developer. Editor features are introduced and explored with examples applicable to Python development. The goal of this book is to help acclimate you to Visual Studio Code features and to help you develop an efficient development workflow.
Stay up-to-date with new Visual Studio Code and Python features by visiting the Microsoft Developer Blog at devblogs.microsoft.com/python.
Who Will Benefit Most from This Book
Those who are in search of a comprehensive introduction to Visual Studio Code for Python development will benefit most from reading this book. It was written for developers with a working knowledge of Python. Although this book does not provide instruction for learning Python development, it includes general programming concepts, such as managing source control, unit testing, and debugging to name a few, which are explored as they relate to Visual Studio Code features. Python tools and libraries that fall outside of the Python Standard Library are also explored and provided with a foundational understanding to complete the exercises throughout this book.
Looking Ahead in This Book
Here's the book at a glance:
Chapter 1: Getting Started introduces the Visual Studio Code interface and the Extension Marketplace. Instructions on how to customize the editor are provided, alongside keyboard shortcuts for quickly executing commands. The keyboard shortcuts are also provided, following the convention of iOS/Windows and Linux (e.g., Cmd+C/Ctrl+C is the shortcut to Copy).
Chapter 2: Hello World for Python prepares your workspace for Python development. After installing a Python interpreter and the Python extension for Visual Studio Code, you are tasked to create and run your first Hello World program in the editor.
Chapter 3: Editing Code explores standard Visual Studio Code editing features in addition to features provided by the Python extension.
Chapter 4: Managing Projects and Collaboration discusses how to open and navigate files in addition to collaborating with others in Visual Studio Code. An introduction to managing Python environments is also provided as it relates to global, virtual, and conda environments.
Chapter 5: Debugging takes you beyond print statements and instead shows you how to use the built-in debugger. Instructions for configuring the debugger are provided as well.
Chapter 6: Unit Testing explains how to create, run, and debug unit tests within the Test Explorer. Examples are provided for both the unittest and pytest frameworks.
Chapter 7: Jupyter Notebook introduces Jupyter Notebook support in Visual Studio Code. Learn how to create, edit, and run cells within the editor. An overview of how to debug a notebook and connect to a remote are provided as well.
Chapter 8: Using Git and GitHub with Visual Studio Code explains how to extend your GitHub workflow in the editor without navigating to the browser. The GitHub Pull Requests and Issues extension is installed and used to maintain source code within a GitHub repository.
Chapter 9: Deploy a Django App to Azure App Service using the Azure App Service Extension takes you through the basic workflow of creating a Django app and how to deploy to Azure within the editor.
Chapter 10: Create and Debug a Flask App provides instruction for how to debug a website created with Flask in Visual Studio Code.
Chapter 11: Create and Deploy a Container with Azure Container Registry and Azure App Service takes you through the basic workflow of containerizing a project with the Visual Studio Code Docker extension.
Chapter 12: Deploy an Azure Function Trigger by a Timer explains how to create a daily RSS feed summary using a function created with Azure Functions that is deployed to Azure.
Special Features
The project files for this book are found on the book page at www.wiley.com. Each chapter introduction states which folder to refer to for the project file(s) required to complete the exercises.
NOTE Boxes like this are used to expand on some aspect of the topic, without interrupting the flow of the narrative.
How to Contact Wiley
Wiley strives to keep you supplied with the latest tools and information you need for your work. If you believe you have found an error in this book, and it is not listed on the book's web page, you can report the issue to Wiley Customer Technical Support at wileysupport@wiley.com.
Part I
Welcome to Visual Studio Code
In This Part
Chapter 1: Getting Started
Chapter 2: Hello World for Python
Chapter 3: Editing Code
CHAPTER 1
Getting Started
When you began your Python development journey, you were most likely introduced to Python's Integrated Development and Learning Environment (IDLE). IDLE's simplicity is ideal for newcomers but leaves much to be desired by those who are more comfortable with the language and are in need of an efficient and productive workflow. A range of code editors and integrated development environments (IDEs) are available for Python development—some for general development with multilanguage support (such as Atom or Sublime) and others built exclusively for Python (such as PyCharm). Selecting a development environment is a matter of personal preference. As an experienced programmer, you might have already tried a few editors and thus are aware of what features you most desire. If you're in need of an extensible code editor that provides ample flexibility, efficiency, and productivity for managing Python source code, then Visual Studio Code is well worth your consideration.
Visual Studio Code (also referred to as VS Code) is a free, open-source, and cross-platform code editor developed by Microsoft. Ranked as the Most Popular Development Environment in the 2019 Stack Overflow Developer Survey, Visual Studio Code is a feature-rich highly customizable code editor that not only is great for editing source code but has built-in support for collaboration and cloud-hosted environments. Visual Studio Code's source code is available in the product's GitHub repository at github.com/microsoft/vscode. You're welcome to contribute to the project and can also view the product roadmap within the repository. Visual Studio Code is updated monthly with new features and bug fixes. For early adopters, the VS Code Insiders build provides a new build at least every day with features and bug fixes.
Visual Studio Code has built-in support only for JavaScript, TypeScript, HTML, and CSS, but it supports many additional languages, such as Python, through extensions. Before you begin programming in Python, you must install the extension. You can then begin to familiarize yourself with the editor's interface within the context of Python.
Installing Visual Studio Code
As a free, cross-platform code editor, Visual Studio Code runs on macOS, Linux, and Windows. Download Visual Studio Code from code.visualstudio.com. If the browser doesn't detect your operating system, visit code.visualstudio.com/#alt-downloads for more options. Platform-specific installation steps are available at code.visualstudio.com/docs/setup/setup-overview. Both macOS and Windows provide the option to add Visual Studio Code to your PATH environment variable. Adding Visual Studio Code to your PATH environment variable provides the convenience of opening a folder directly from the console using the command code
As mentioned, Microsoft releases a new version of Visual Studio Code often with new features and important bug fixes. If your platform supports auto-updating, Visual Studio Code prompts you to install the new release when it becomes available. As an alternative, you can manually check for updates by running Help ➪ Check For Updates on Linux and Windows or by running Code ➪ Check For Updates on macOS.
NOTE If you're interested in trying the VS Code Insiders build, you can download a copy from code.visualstudio.com/insiders/. You can install the Insiders build side by side with the latest monthly build, which enables you to use both versions of the code editor independently.
The Visual Studio Code User Interface
Visual Studio Code's user interface (UI) provides a simple minimal layout that keeps your source code as the focus of the development environment. When you first start Visual Studio Code, it displays a default layout. Each time you start Visual Studio Code going forward, the editor opens in the same state it was in when last closed.
You can make yourself at home by customizing the layout to your liking. However, before you start moving things around, you should get to know the main areas of the UI and their respective function (see Figure 1.1).
Snapshot of the Visual Studio Code user interface.Figure 1.1: The Visual Studio Code user interface.
Activity Bar
The Activity Bar, located on the far-left side, lets you switch between views. Views provide quick access to common tasks such as the following:
Explorer—File and folder management
Search—Global search and replace across open folders using plain text or regular expressions
Source Control—Git source control for maintaining code repositories
Run—Features used during debugging, such as variables, call stacks, and breakpoints
Extensions—Browsing, installation, and management of extensions from the Extension Marketplace
In addition to the default views, the Activity Bar can also include custom views provided by extensions that you install from the Extension Marketplace. Each view has an icon that reflects its respective function.
You can reorder views by dragging and dropping them in the Activity Bar. Views can also be hidden if you right-click the view and select Hide From Activity Bar. Views are part of your custom layout that is preserved each time you run Visual Studio Code.
Side Bar
The Side Bar, located to the right of the Activity Bar, displays the active view. If no view is selected, the Side Bar is collapsed. You can resize the Side Bar by clicking and dragging the edge that it shares with the editor. The default views for the Side Bar are Explorer, Search, Source Control, Run, and Extensions (see Figures 1.2 through 1.6, respectively).
Snapshot of explorer view.Figure 1.2: Explorer view.
Snapshot of search view.Figure 1.3: Search view.
Snapshot of source Control viewFigure 1.4: Source Control view.
Snapshot of run viewFigure 1.5: Run view.
Snapshot of extensions viewFigure 1.6: Extensions view.
Editor
The editor, which fills most of the screen, is where you edit files. You can resize the editor by clicking and dragging the edges that it shares with the Side Bar and the panels.
The top editor region can change depending on the type of file that's active in the editor. For example, if you edit a Markdown file, a Preview icon appears, thus enabling Visual Studio Code's Markdown Preview (see Figure 1.7).
Snapshot of in the top image, the Preview icon appears in the top editor region since a Markdown file is opened. Clicking the icon displays a preview of the Markdown file, as shown in the bottom image.Figure 1.7: In the top image, the Preview icon appears in the top editor region since a Markdown file is opened. Clicking the icon displays a preview of the Markdown file, as shown in the bottom image.
When you open a Python file, you instead see a Run Python File In Terminal icon (displayed as a Play button) in the top editor region. (The Run Python File In Terminal icon is a quick way for you to run a Python program.) When selected, a terminal opens, and the Python file is run (see Figure