Introduction

Python is a popular programming language for data science tasks, but managing dependencies can be challenging. One way to simplify this process is by using a virtual environment. In this tutorial, we will cover how to set up a Python virtual environment using the venv module on Linux with a Bash shell. We will also show how to install packages suitable for data science tasks using NumPy and Pandas.

Prerequisites

Before we get started, you will need to ensure that you have Python and the venv module installed on your Linux system. You can check if you have Python installed by running the following command in your terminal:

python --version

You should see the version number of Python installed on your system. If you do not have Python installed, you can install it using your system's package manager.

sudo apt-get install python3

You can check if you have the venv module installed by running the following command:

python -m venv --help

If you see the help text for the venv module, you have it installed. If not, you can install it by running the following command:

sudo apt-get install python3-venv

Creating a Virtual Environment

Now that we have Python and the venv module installed, we can create a virtual environment. To create a virtual environment, navigate to the directory where you want to create it and run the following command:

python -m venv myenv

This command will create a directory named myenv in the current directory and populate it with a minimal Python environment. You can choose any name you like for your virtual environment.

Activating the Virtual Environment

To activate the virtual environment, run the following command:

source myenv/bin/activate

This command will modify your shell's environment to use the Python executable and packages installed in the virtual environment.

You should see the name of your virtual environment displayed in your shell prompt. For example:

(myenv) user@hostname:~/project$

This indicates that you are now using the Python environment of the virtual environment.

Installing Packages

To ensure that our virtual environment is working correctly, we will install packages suitable for data science tasks using NumPy and Pandas. To do this, we first need to update pip, the package installer for Python. Run the following command:

pip install --upgrade pip

Now we can install NumPy and Pandas by running the following command:

pip install numpy pandas

This will install the latest versions of NumPy and Pandas in our virtual environment.

Deactivating the Virtual Environment

When you are finished using the virtual environment, you can deactivate it by running the following command:

deactivate

This command will restore your shell's environment to its previous state, where Python and packages are installed globally on your system.


Published

Last Updated

Category

Tutorials

Tags

Contact