Tools, APIs & Frameworks

Python Developers live in Visual Studio Code

Python Developers – A Growing Breed

Claudia Regio

With over 18 million monthly users, VS Code has become one of the most popular and fastest-growing text editors in the world. To learn more about why over 3.7 million of them find VS Code to be the perfect habitat for Python development and data science work, keep on reading!.

The Python Programming language was created during the late 1980’s by Guido Van Rossum. By 2003, it consistently ranked among the most popular programming languages in the world. According to the PYPL (PopularitY of Programming Language index) [1], which is generated by analyzing the frequency of coding tutorial searches on Google, Python is now the most popular language in the world. This is no surprise after having grown 15.4% in the last 5 years. [Fig. 1]

Fig. 1: Python popularity growth

With hundreds of packages including Pandas, NumPy, Matplotlib and Scikit Learn that provide functionality for tabular data manipulation, numerical computing, data visualizations and machine learning algorithms for predictive data analysis respectively, the Python language has become the go-to for data science work. Powerful frameworks for building apps such as Flask and Django that are lightning-fast, scalable, and flexible make it one of the most compelling options for web development. Python’s growth and coverage in multiple coding use-cases has continued to skyrocket and has no indications of slowing down any time soon.

VS Code – The Perfect Habitat for Python Developers

At Microsoft’s Developer Division, our mission is to enable every developer to achieve more. This year, to continue supporting the quickly growing Python community, we increased our sponsorship of the Python Software Foundation [2] to the top new visionary level. Goals of the PSF include providing grants and resources for further development and adoption of Python as well as expanding Python outreach by funding the Python Ambassador Program.

On top of supporting the Python community at large, we aim to support Python users right here at home in VS Code! With over 18 million monthly users, VS Code has become one of the most popular and fastest-growing text editors in the world. To learn more about why over 3.7 million of them find VS Code to be the perfect habitat for Python development and data science work, keep on reading!

Stay up to date

Learn more about MLCON

 

How to Follow Along

First things first, you will need to install VS Code. Once you have VS Code installed, you can search for and install extensions through the VS Code Extensions Marketplace. The family of extensions you’ll need for the ultimate Python coding experience include the Python, Pylance, Jupyter, and Azure Machine Learning extensions. [Fig. 2]

Fig. 2: VS Code Extension Marketplace

Python Extension

The Python extension builds on top of VS Code’s already powerful code editor. By providing additional support for environment handling, debugging, testing, linting and formatting, the Python extension capabilities are here to supercharge your Python development work. Our recent extension startup changes have also made great strides in performance improvements so you can get coding sooner.

RETHINK YOUR APPROACHES

Business & Strategy

 

Environment Handling

Get started easily with any of your favorite environments such as pyenv, pipenv, Conda, and Poetry. The extension will automatically detect Python interpreters that are installed in standard locations and the environment you choose will power the IntelliSense, auto-completions, linting, formatting, and any other language-related feature other than debugging.

Debugging

Print statements to check states of variables are a thing of the past. Easily debug different types of Python applications (e.g., multi-threaded, web, and remote applications) by setting breakpoints, inspecting data, and utilizing the debug console as you step through your code. On top of that, there is no starting and stopping with the Python debugger. If you make changes to your code after the debugger execution has already hit a breakpoint, you no longer need to restart the debugger as auto-reload exists for Python scripts, Django and Flask! [Fig. 3]

Fig. 3: Debug Python file in terminal

Linting

Linting analyzes your code for potential errors, highlighting areas where problems should be corrected so you don’t have to manually parse the code yourself. The currently supported linters include Pylint, pycodestyle, Flake8, mypy, pydocstyle, prospector, and pylama. Simply make sure the linter of your choice is installed in the active Python interpreter. [Fig. 4]

Fig. 4: Linter support

Pylance

Your default Python editing experience has been upgraded with the bunding of our Pylance extension, Visual Studio Code’s most robust and performant Python language server. Its rich editing features include completions, auto-imports, function signature help, docstrings, contextual highlighting, and more!

With auto-imports you can say goodbye to your workflow being interrupted to import necessary modules. As you are constructing your code, Pylance will provide smart import suggestions and insert them at the top of your file for you. The function signature help provide information on parameters as well as return types so that you no longer have to hunt down external documentation and leave the context of your code editor.

You can even refactor your code at the speed of light by tapping into Pylance’s extraction features. You can highlight lines of code and pick either “Extract Method” [Fig 5] or “Extract Variable” [Fig. 6] to have Pylance do the heavy lifting to turn them into new variables or functions.

Fig. 5: Pylance’s Extract Method

Fig. 6: Pylance’s Extract Variable

Don’t forget about contextual document highlighting! Double-clicking on variables will present other instances of the variable to you such that none can slip by you. [Fig. 7]

Fig. 7: Pylance’s Contextual Document Highlighting

Jupyter Notebooks

A Jupyter Notebook is an interactive programming and computational document that supports mixing executable code, equations, visualizations, and narrative text. Jupyter Notebooks [Fig. 8] can contain markdown and code cells, where code cells have two major components: input and output. You can write code in the input area of a cell, and after running the cell the result will show up in the output area just below. [Fig. 9]

Fig. 8: VS Code Notebook

Fig. 9: Histogram in VS Code Notebooks

Jupyter Notebooks have quickly become the de facto tool for data science. The ability to run chunks of code at a time and out of order makes them very exploratory in nature which is incredibly conducive to data exploration. The ability to see outputs and visualizations in a hassle-free manner paired with narrative text, makes Jupyter Notebooks the perfect location to tell a story with data. Outside of data science though, they are also a great tool for teaching or learning new languages, general code experimentation, and building quick prototypes.

This past year, our very own implementation of Jupyter Notebooks got a major overhaul by being fully integrated with Visual Studio Code. On top of a new modern design, you can now benefit from faster load times, innate source control and diffing capabilities, full notebook debugging, customizable theming, and more!

Variable Explorer and Data Viewer

Our additional features such as our Variable Explorer [Fig. 10] and Data Viewer will help you keep track of the state of your variables and take a deeper look at the tabular data you might be working with. To access the Variable Explorer, simply click on the Variables Icon in your notebook toolbar.[Fig. 11] To access the Data Viewer, click on the icon to the left of the tabular variable you would like to inspect.

Fig. 10: Variable Explorer in Notebook Toolbar

Fig. 11: Variable Explorer

The Data Viewer [Fig. 12] provides a spreadsheet-like view of your data and the filtering capabilities allow you to make quick checks on your data. It facilitates and speeds up identifying data quality issues and the next steps that must be taken in order to properly clean the data.

Fig. 12: Data Viewer

Debugging

VS Code allows you to debug your notebook in multiple ways. For a “debugging-lite” experience, you can opt for Run by Line. As you’ve likely already guessed based on the name, Run by Line [Fig. 13] allows you to run through your cell, one line at a time. When Run by Line is enabled, the Variable Explorer will open alongside with it so you can keep track of the state of your variables as you iterate and quickly resolve small code issues.

Fig. 13: Run by Line

With our most recent revamp, you can now take advantage of the same full debugging experience [Fig. 14] enabled by the Python extension in notebooks. It’s important to note that if you’d like to use these debugging features in VS Code today, you’ll need at least version 6.0 or higher of ipykernel [3] in the environment you select to run your notebooks.

Fig. 14: Debugging in Jupyter Notebooks

Custom Notebook Diffing

Under the hood, Jupyter Notebooks are JSON files. The segments in a JSON file are rendered as cells that are comprised of three components: input, output, and metadata. Comparing changes made in a notebook using lined-based diffing is difficult and hard to parse. The rich diffing editor for notebooks allows you to easily see changes for each component of a cell.

You can even customize what types of changes you want displayed within your diffing view. In the top right, select the overflow menu item in the toolbar to customize what cell components you want included, but don’t worry about input changes as those will always be shown. [Fig. 15]

Fig. 15: Customized Diffing View

Interactive Window

If you like the idea of notebooks but are used to working with scripts, we have the feature just for you! The Interactive Window is a hybrid between a notebook and a script. When working in a Python file, you can create cell-like code segments by using the ‘#%%’ delimiter. Running these faux cells in the Interactive Window [Fig.16] allows you to break down your longer Python script into smaller and more comprehensible chunks and see their results to the right as opposed to inline like a notebook would. You can also run code directly in the Interactive Window itself, that way you can use it as a scratch pad where you can try out slightly tweaked code before inserting it into your more finalized Python script.

Fig. 16: Interactive Window

TensorBoard and PyTorch Profiler

If you are using PyTorch or TensorFlow you can look forward to our TensorBoard dashboard integration helping you visualize datasets, train models, spot check model predictions, view model architecture, analyze model’s loss and accuracy over time, as well as profile your code to understand where it is slowest. In addition to TensorBoard integration, we’ve also embedded the PyTorch Profiler in VS Code such that you can monitor your PyTorch models all in one convenient location. In addition, VS Code is exclusively the only tool today that allows you to jump directly to your source code file from the PyTorch Profiler!

If working in notebooks, our Variable Explorer allows you to inspect PyTorch and TensorFlow data types and our Data Viewer allows you to slice data so you can get a robust understanding of any 3D or higher dimensional data. As a reminder, you can access the Data Viewer through the Variable Explorer or during a Python debugging session. When a debugging session has started, you can right click on the Tensor you would like to do a deeper dive on and select “View Value in Data Viewer”. [Fig. 17]

Fig. 17: Data viewer

Azure Machine Learning

While many data science and machine learning tasks can be completed successfully on your local machine, sometimes you just need a bit more power! If you’re interested in scaling your training and inferencing workloads, the Azure Machine Learning extension [4] has you covered.

You can search for the Azure Machine Learning extension in the Visual Studio Code marketplace, sign into your Azure Account, [5]  and create a machine learning workspace [6] to organize and manage your resources. Through the Azure Machine Learning extension, you can create a compute resource and seamlessly connect to it without requiring SSH or additional network configuration. When connected to the compute you can continue using your favorite VS Code capabilities (notebooks, debugger, terminal), import your local project, and scale your model training while leveraging GPU resources.

The Azure Machine Learning extension [Fig. 18] also provides enhanced language support (completions based on your Azure resources) and generated templates that you can use to author and check-in reproducible, shareable configuration files. Once created and deployed, the results of your work (e.g., creating an environment, training a model) can be viewed from directly within Visual Studio Code through the extension tree view; you no longer have to context-switch between the editor and the browser to manage your machine learning resources.

NEW & PRACTICAL ENDEAVORS FOR ML

Machine Learning Principles

Fig. 18: Azure Machine Learning

Additional Notable Mentions

While the next two extensions are not exclusive to Python itself, they are incredibly powerful aids in your development experience.

Remote – SSH

The Remote – SSH extension lets you use any remote machine with a SSH server as your development environment. It effectively runs VS Code on the remote machine, so you have access to any extensions and files on that same remote machine. With this extension you can develop on the same operating system you deploy to or use larger, faster, or more specialized hardware than your local machine as well as switch between remote development environments without altering anything on your local machine.

Live Share

The Live Share extension allows you to collaboratively edit and debug with others in real time, regardless of what programming languages are being used. You can forget the archaic method of sending files back and forth between you and your coworkers. Live Share allows you to instantly (and securely) share your current project, and then as needed, share debugging sessions, terminal instances, localhost web apps, voice calls, and more! Guests invited to your sessions will have your editor context mirrored on their machine so you can start collaborating productively immediately without needing to clone any repos or install any SDKs.

As the guest joining a Live Share session however, you will still have all your personal editor preferences (e.g. keybindings, theme) honored and your own cursor so you can seamlessly jump into a session and work together or independently in the same file.

Welcome Home Pythonistas

Regardless of what you are trying to achieve with Python, VS Code is the place for you! We hope you come try out the Python experience in VS Code and that it becomes your new home for Python development and data science work! Let us know what you think!

For a much more detailed and in-depth walkthrough of the mentioned extensions and features, please visit our Visual Studio Code Documentation.

How to Contact Us

Follow our Twitter handles @code for any Visual Studio Code product updates and @pythonvscode for Python and Jupyter product announcements! For any feature suggestions or issues don’t hesitate to file issues on our VS Code, VS Code Python, VS Code Jupyter, or VS Code Pylance GitHub Repositories! As always, we encourage and welcome the community to participate and contribute to our open-source tools!

Links & Literature

[1] https://pypl.github.io/PYPL.html

[2] https://www.python.org/psf/ 

[3] https://pypi.org/project/ipykernel/ 

[4] https://docs.microsoft.com/en-us/Azure/machine-learning/how-to-setup-vs-code 

[5] https://docs.microsoft.com/en-us/dotnet/azure/create-azure-account

[6] https://docs.microsoft.com/en-us/Azure/machine-learning/how-to-setup-vs-code 

Top Articles About Tools, APIs & Frameworks

Behind the Tracks

DON'T MISS ANY ML CONFERENCE NEWS!