Python in Visual Studio tutorial Step 5, install packages (original) (raw)

This article presents Step 5 in the tutorial series Work with Python in Visual Studio.

All code in a Python project runs within the context of a specific environment. Examples of environments include a global environment, a virtual environment, a conda environment, and so on. The environment is used for any tasks that require language services that are specific to the Python version and a set of installed packages. Each environment requires certain installed packages based on the project code.

The Python developer community produces thousands of useful packages that you can incorporate into your own projects. In Step 5 of this tutorial series, you use the Python Environments window in Visual Studio to manage packages in your Python environments.

In Step 5 of the tutorial, you learn how to:

Prerequisites

View Python environments in Visual Studio

Visual Studio provides two places to view information about Python environments:

Use one of the following methods to open the Python Environments window:

Prepare the Python file

To prepare for the exercise, follow these steps to update your Python project file to include the necessary code:

  1. Open your Python project file (.py) in the editor. (Tutorial Step 2 explains how to create this file, where the default file name is PythonApplication1.py.)
  2. Replace the code in the file with the following code. This version of the code creates a cosine wave similar to the output from tutorial Step 4, but it plots the output graphically.
from math import radians  
import numpy as np                # installed with matplotlib  
import matplotlib.pyplot as plt  
def main():  
   x = np.arange(0, radians(1800), radians(12))  
   plt.plot(x, np.cos(x), 'b')  
   plt.show()  
main()  
  1. In the editor, hover over the matplotlib import statement. Visual Studio uses special formatting to indicate the statement is unresolved.
    Screenshot that shows how Visual Studio 2022 uses special formatting to indicate that the matplotlib import statement is unresolved.
    Screenshot that shows how Visual Studio 2019 uses special formatting to indicate that the matplotlib import statement is unresolved.
    The matplotlib import statement is unresolved because the necessary packages aren't installed to the default global environment. You learn how to install the required packages in the next section.

Install packages for the Python environment

To resolve the matplotlib import statement, you need to install the necessary packages to the default global environment:

  1. In the Python Environments window, select the default environment for new Python projects, then select Packages (PyPI) in the dropdown menu. (PyPI is the acronym for the Python Package Index.)
    Screenshot that shows how to select the Packages PyPI option for the selected environment to see the installed packages.
    Visual Studio shows the list of packages currently installed in the default environment.
  2. In the Search field, enter matplotlib. In the results list, select the Run command: pip install matplotlib option.
    Screenshot that shows how to install the matplotlib library for the selected environment in the Python Environments window in Visual Studio 2022.
    Screenshot that shows how to install the matplotlib library for the selected environment in the Python Environments window in Visual Studio 2019.
    The command installs the matplotlib library, and also any packages it depends on. In this case, the dependent packages include the numpy library.
    You can open the View > Output window to monitor the progress of the installation.
  3. After the packages install, the Python Environments window refreshes to show the packages for the selected environment:
    Screenshot that shows the newly installed matplotlib and numpy packages for the selected environment in the Python Environments window in Visual Studio 2022.
    Screenshot that shows the newly installed matplotlib and numpy packages for the selected environment in the Python Environments window in Visual Studio 2019.
    The X to the right of the package uninstalls it.

Run the program

Now that the matplotlib library is installed, you're ready to test your program.

  1. Run your program with debugging (F5) or without the Debugger (Ctrl+F5).
  2. Check the program output. You should see a plot graph of the cosine data:
    Screenshot that shows the program output, which uses code in the Python matplotlib library package.

Next step