How to create a Virtual Environment in Python

Creating a Python virtual environment is a methodology that enables the isolation of dependencies and packages specific to a particular project. This isolation prevents conflicts with other projects and ensures a clean and organized package installation. Virtual environments are particularly valuable when managing multiple projects with different Python library versions or when avoiding interference with the system-wide Python installation.



Creating Python Virtual Environments for Project Isolation

Starting from Python 3.3 and above, virtual environments are already integrated into the standard library, and the venv module is used for their creation. Here's how to create and activate a Python virtual environment:

  • Launch the command prompt (on Windows) or terminal (on macOS/Linux)
  • Create a new directory for your project (if it doesn't exist) and navigate to it:
    mkdir my_project
    cd my_project
  • Create a virtual environment using the venv module. Specify a name for your virtual environment (e.g., "venv_name"):
    python -m venv venv_name
  • Activate the virtual environment:
    • on Windows:
      venv_name\Scripts\activate
    • on macOS/Linux:
      source venv_name/bin/activate

After activation, your terminal or command prompt should indicate that you are inside the virtual environment. For instance, you may see a prefix (venv_name) or the name of the virtual environment at the beginning of the command prompt.

Now, you can install packages in your virtual environment using pip, and they will be available only within this environment. For example, to install the package requests, execute the following command:

pip install requests

When you're done working in the virtual environment, you can deactivate it using:

deactivate

By deactivating the virtual environment, you will return to your system's default Python environment.

That's it! With virtual environments, you can manage dependencies for different projects independently, and it helps to keep your project isolated from the system-wide Python installation.



requirements.txt

The requirements.txt file is a plain text file used in Python projects to manage dependencies. It contains the names and versions of packages required for your project. The requirements.txt file is typically used in conjunction with virtual environments to install and update packages in the project.

Generating the requirements.txt file:

If you already have installed packages in your virtual environment, you can generate a requirements.txt file that contains a list of the installed packages and their versions. Execute the following command:

pip freeze > requirements.txt

OR (in more general way)

venv_name/bin/python -m pip freeze > requirements.txt

This will save a list of all installed packages in the requirements.txt file.

Installing dependencies from requirements.txt:

To install all the dependencies listed in the requirements.txt file, use the following command in the activated virtual environment:

pip install -r requirements.txt

This will read the requirements.txt file and install all the specified packages with their specified versions.

Updating dependencies

If you want to update packages to their latest versions, you can use the following command:

pip install --upgrade -r requirements.txt

This will upgrade the packages listed in the requirements.txt file to their latest available versions.

It is essential to keep the requirements.txt file up to date and periodically update dependencies to ensure that your project utilizes the latest package versions and operates reliably.

Please note that using virtual environments and the requirements.txt file is highly recommended for all Python projects, especially when dealing with multiple projects or collaborating with other developers. This practice helps avoid version conflicts and simplifies dependency management.