2.1 Environments and Libraries

Questions we’ll cover

  • How do I create a virtual environment?
  • How do I install the Python libraries that I want to work with?

Setup

  1. Create a project folder where you’ll store data and Python scripts.
  2. Download this file and put it in the folder you just created.
  3. Open your project in VS Code Open VS code, and then select File > Open Folder, and open the folder you created.

Create a local Python environment

Best practice in Python is to create a virtual environment for each project, which contains all the specific libraries (and specific versions) that you need for that project.

We will use the venv tool to create an environment with the following steps:

  1. Open the Command Palette with Ctrl+Shift+P
  2. Type in Python: Create Environment and click on it
  3. Select venv as the tool we want to use to create the environment
  4. Select the Python interpreter that you want to use (probably the one with the highest numbers, starting with something like Python 3.10)

After following these steps, VS Code will create a .venv folder in your project folder.

Activate the environment

VS Code may give you a pop-up asking if you want to activate the virtual environment that you just created. If it does, select Yes.

If not, you can run a command in the terminal (in VS Code) to activate it.

For Windows, you should run .venv\Scripts\activate.

For Mac or Linux, you should run source .venv/bin/activate

Install Python libraries

In the terminal in VS Code, you can install the libraries that we will need for this workshop by running the following commands.

First, we will upgrade pip, which is the tool that we use to install Python libraries.

pip install --upgrade pip

Now we can install the libraries numpy and matplotlib.

pip install numpy matplotlib

View in GitHub

Loading last updated date...