You are currently viewing How to Set up a Virtual Environment in Python

How to Set up a Virtual Environment in Python

One of the best features of Python is its remarkable flexibility, which allows you to work with different types of packages and libraries depending on your preferences and the requirements of your project. This guide will walk you through to set up a virtual environment in python, using venv package.

One of the best features of Python is its remarkable flexibility, which allows you to work with different types of packages and libraries depending on your preferences and the requirements of your project. This guide will walk you through to set up a virtual environment in python, using venv package.

What is Virtual Environment in Python?

Python virtual environments provide an isolated, self-contained environment in which you can install and run your Python packages.

Why you should use a Virtual Environment?

Python projects come with their own independent set of installed Python packages, as well as their own configuration files, directories, and environment variables. Virtual environment is useful if you have multiple projects that require different versions of the same Python packages, or if you just want to keep your global site-packages directory clean and tidy.

Step 1: Install Multiple Python Versions in Linux

Multiple python versions can be easily installed using the apt install. We first need to update our system and then install software-properties-common to easily manage our Linux distribution using the following commands.

In the third command, you must specify the version of Python you wish to install.For example, in the below code I specify python3.9 version. To install a specific version of Python, simply change the version code to something other than 3.9.

sudo apt update
sudo apt install software-properties-common
sudo apt install python3.9 python3-pip

You can verify the python version using the below command.

python3.9 -V

That’s it! Now you can enjoy multiple versions of python running on your machine at the same time.

Step 2: Install venv library in Python

Python has its own venv library which makes it very easy to create and activate new virtual environments. First, install the library by running sudo apt install python3-venv from the terminal.

sudo apt install python3.9-venv

Step 3: Create Virtual Environment using venv in Linux/Windows

You can use any name you like when you make a new virtual environment. For example, I used the name segmentation_env when I created my medical image segmentation project.

python3.9 -m venv segmentation_env

Initially, it will create only seven folders in the segmentation_env/lib/python3.9/site-packages/

Step 4: Activate venv in Linux

The next step is to switch to your working directory using cd ~/segmentation_env and then activating the venv with . ./bin/activate. Follow the below code.

source segmentation_env/bin/activate

Activate venv in Windows

envionment_name\\Scripts\\activate.bat

Step 5: Install Python Packages in venv using PIP

In order to install python packages inside a virtual environment you need to first activate the venv (see above). Once activated, open terminal and execute the following commands: pip install package_name.

As an example, the following code is used to install NumPy library in a virtual environment.

pip install numpy

Now, you can successfully import any installed package.

Step 6: Uninstall Python Package in venv using PIP

To uninstall a package that was installed in the venv virtual environment, use the following command: pip uninstall packagename.

pip uninstall numpy

Step 7: Export requirements.txt in venv

You can export the requirements.txt in venv folder so that the same packages are available outside this folder as well. Pip will generate a text file called requirements.txt listing all of the necessary Python packages that are required to run the application.

pip freeze>requirements.txt

Step 8: Install requirements.txt in venv

You can easily install the packages of requirements.txt using the below command.

pip install -r requirements.txt

Step 9: Add venv to Jupyter Notebook

To add the python language kernel of virtual environment in Jupyter Notebook, enter the following into your terminal:

pip install ipykernel
python -m ipykernel install --user --name=segmentation_env

After successfully installation, you can start it by writing the code jupyter notebook in your terminal.

Step 10: Deactivate venv Virtual Environment

Finally, you can deactivate a venv virtual environment by running the deactivate command from the same directory as where you activated it.

deactivate

Reference:

venv

Leave a Reply