How to Install Python Libraries in Visual Studio Code (Mac)

Before we begin, ensure that you have at least Python 3 installed. I’m using Python 3.9

First, create a new folder. This will be your Python project folder.

Next, create a Python file inside your folder. It doesn’t matter what you call it. I’ll call mine main.py

In order to import libraries inside our folder, we need to first create a Python virtual environment. To do this run the following command in Terminal inside your project directory:

python3.9 -m venv projenv 

I typed python3.9 because I’m using Python 3.9, but you would type whichever version you’re using.

I called my virtual environment projenv, but you can call yours whatever you want.

You should see a new folder for your virtual environment was created within your project folder.

Note: Check to make sure that the VS Code Python interpreter is the correct version.

Next, we will activate the virtual environment so that Terminal knows we only want to work within the virtual environment.

source projenv/bin/activate

Now, you should see the virtual environment name in parentheses at the left of your new Terminal line.

It’s good practice to make sure you have installed the latest version of pip. Run this command to update to latest:

pip3 install --upgrade pip

Now, we can start installing pip dependencies to our project.

To install a dependency, run the following command:

pip3 install matplotlib

I’m using matplotlib in this example.

Now, we will check to to see if the install was successful. In the Python file you created earlier, import the matplotlib package:

import matplotlib.pyplot

Run the main.py file by typing python main.py in Terminal.

If you don’t get an error, then you successfully installed a package!

Leave a comment