Virtual Environments in Python

Virtual Environments in Python

Before even starting any development, set your environment right

Short description of Virtual Environments

  • Virtual environments allow you to create isolated working copies of Python. Each environment is specific to a project, ensuring that changes made in one environment don’t affect other projects.

  • When you work on multiple projects, each with its own set of dependencies (libraries, packages, etc.), virtual environments keep them separate. This prevents conflicts between different package versions.

Benefits of Virtual Environments

  1. Project-Specific Packages:

    • Imagine you’re working on two projects—one using Streamlit 1.31.0 and another using Streamlit 1.1.0. Or one project using Langchain 0.1.7 and another one using Langchain 0.0.353. Without virtual environments, installing packages system-wide would lead to conflicts.

    • With virtual environments, you can have distinct sets of packages for different projects. This flexibility allows you to satisfy requirements for both projects simultaneously.

  2. Reproducibility:

    • Virtual environments ensure that the correct package/library versions are consistently used every time your software runs.

    • When you share your project with others or deploy it, having a well-defined environment ensures reproducible results.

  3. Easy Dependency Tracking:

    • You can create a requirements.txt file within your virtual environment. This file lists all the packages your project depends on.

    • This makes it straightforward to recreate the same environment elsewhere (e.g., on a server) by installing the packages listed in the requirements file.

  4. Switching Python Interpreters:

    • Sometimes you might need to use an older Python version (e.g., for legacy scripts). Virtual environments allow you to switch to a different installed Python interpreter for a specific project. For example Python 2.7, Python 3.7.0, Python 3.12.2, etc.

In summary, virtual environments provide stability, reproducibility, and flexibility - all essential for managing your Python projects!

Setting up the Python Virtual Environment

pip install virtualenv

1️⃣ Create a virtual environment for a project:

$ cd project_folder
$ virtualenv pm-python-venv

virtualenv pm-python-venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case pm-python-venv) can be anything you like.

This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named pm-python-venv.

💡
If you need to use different Python version for different Virtual Environments, use the following command to point to the interpreter version (like python 2.7 in this case):
virtualenv -p /usr/bin/python2.7 pm-python-venv

2️⃣ To begin using your virtual environment, activate it using the following command:

source pm-python-venv/bin/activate

The name of the current virtual environment will now show to the left of the prompt (e.g., (pm-python-venv) your-machine:project_folder yourUser$) to indicate that it is active. Any package installed using pip will now be stored in the pm-python-venv folder, which is separate from the global Python installation.

3️⃣ Installing Python packages

Having the Virtual Environment running, you can now install the desired Python packages. They will be only installed in the current Virtual Environment and will NOT affect the global Python installation.

Here are two ways you can install packages in your new Virtual Environment:

  • Using pip install directly (to install more than one package, use space as a seperator) - in the example below we are installing the following Python packages in pm-python-venv: requests, pandas and numpy.
pip install requests pandas numpy
  • Another method is using a text document where all packages are listed. The file can have any name you like (it's usually named requirements.txt) and has a content like this:
requests
pandas 
numpy
  • To make it a little bit more precise, you can also point the package versions in your .txt file. This makes it straightforward to recreate the same environment elsewhere (e.g., on a server) by installing the packages listed in the requirements file. More importantly it will ensure your Python code runs the same way you intend it to work, as the right package versions are used.
requests==2.7.0
pandas==2.2.0
numpy==1.21.0

To install these packages, use the following command:

pip install -r requirements.txt
💡
You can export the list of current packages with their versions, used in the current Virtual Environment. This will generate an output, similar to the one above. The command to do that is the following:
python3 -m pip freeze

Deactivating your Virtual Environment

If you are done working in the virtual environment for the moment, you can deactivate it:

deactivate

Deleting your Virtual Environment

To delete a Virtual Environment (yes, you might want to do it if you don't need this environment anymore, as it might take more than 1GB depending on the packages you have installed), you just need to delete the Virtual Environment folder.

Follow me

Did you like this blog post? Follow me! 🔔