Installing Python Packages For Visual Studio Code On macOS

Earlier, we have seen how to setup Visual Studio Code for Python programming. The next important item you have to know is how to install packages for python on VS Code. For installing the packages, you have to choose the environment whether it’s global or virtual or conda. As a best practice, it is recommended to use the virtual environment for installing packages to be used for python project or workspace in VS Code. Here we will see how to create a virtual environment, install the packages and use them.

Tools & Technologies Used

  • Python (3.7.3)
  • Visual Studio Code (1.36.0)
  • macOS Mojave (10.14.5)

To start with, we will create a virtual environment.

Creating Virtual Environment

  1. In VS Code Editor, open the Command Palette, by pressing Shift, Command and P keys.
  2. Search and start Terminal: Create New Integrated Terminal. The terminal panel will open at the bottom.
  3. In the terminal run these comments.
python3 -m venv env
source env/bin/activate
Creating Virtual Environment

A new virtual environment called env is now created for the workspace. To see this, go to the explorer panel and expand the newly created folder under the workspace.

Python virtual library

Open the Command Palette again and search and select Python: Select Interpreter. Now you will see the newly created virtual environmnet (something similar to Python 3.7.3 64-bit (‘env’, venv)). Select the virtual environment as your interpreter.

Virtual environment is ready. Now we will see how to install a Python package.

Installing & Using Python Packages

Before going to the installation part, let us see what happens if the package is NOT installed. For this demonstration, we will use the package NumPy. NumPy is used for generating multi dimensional array objects used for scientific calculation. Here is a simple program using NumPy package.

import numpy as np
a = np.arange(6)
print(a)

Let us run this program in the debug mode.

  1. Create a break point at the first line of the code.
  2. Open the debug panel and press the green arrow.
  3. Do a step over. You will see a ModuleNotFound error in the editor. This is because we haven’t installed the package in the virtual environment.
ModuleNotFound Error

Now, let us see how to install the package.

  1. Open the Command Palette, by pressing Shift, Command and P keys.
  2. Search and start Terminal: Create New Integrated Terminal. The terminal panel will open at the bottom.
  3. In the terminal run this comment.
python3 -m pip install numpy
Installing Python Packages

Now, run the program again. You will be able to get the result without any error.

Using Python Packages

In the workspace, where virtual environment is already available you can run the comment python3 -m pip install <package-name> for installing more packages as needed.

Next Step

Reference

  • More about getting started with Python here.


3 thoughts on “Installing Python Packages For Visual Studio Code On macOS”

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.