43 - Virtual Environment

Creating Environment:

A virtual environment is a tool used to isolate specific Python environments on a single machine, allowing you to work on multiple projects with different dependencies and packages without conflicts. This can be especially useful when working on projects that have conflicting package versions or packages that are not compatible with each other.

To create a virtual environment in Python:

you can use the venv module that comes with Python. Here's an example of how to create a virtual environment and activate it:

# Create a virtual environment
python3 -m venv <your_virtual_environment_name> # this needs python3.10-venv package to be installed

# Activate the virtual environment (Linux/macOS)
source <your_virtual_environment_name>/bin/activate

# Activate the virtual environment (Windows)
<your_virtual_environment_name>\Scripts\activate.bat
Tip
  • Python3 command takes you to python environment
  • exit() command throws you out of Python shell
  • use Pip to install Python modules
    Info
    • All modules in Virtual environments are separated from system & other v-environments
    • Once the virtual environment is activated, any packages that you install using pip will be installed in the virtual environment, rather than in the global Python environment.
    • This allows you to have a separate set of packages for each project, without affecting the packages installed in the global environment.

To deactivate it:

  • you can use the deactivate command:
# Deactivate the virtual environment
deactivate


The "requirements.txt" file

  • In addition to creating and activating a virtual environment,
    it can be useful to create a requirements.txt file, that lists the packages and their versions that your project depends on.
  • This file can be used to easily install all the required packages in a new environment.

To create a requirements.txt file, you can use the pip freeze command, which outputs a list of installed packages and their versions.
For example:

# Output the list of installed packages and their versions to a file
pip freeze > requirements.txt

To install the packages listed in the requirements.txt file, you can use the pip install command with the -r flag:

# Install the packages listed in the requirements.txt file
pip install -r requirements.txt
Info

-r is used to tell PiP that we are giving input & not to install a package.

otherwise pip will take requirments.txt as a package.


Important

Using a virtual environment and a requirements.txt file can help you manage the dependencies for your Python projects and ensure that your projects are portable and can be easily set up on a new machine.