When working on a Python project, it's essential to manage dependencies efficiently. One of the best practices is to use a virtual environment. This guide will walk you through creating a virtual environment and generating a
requirements.txt
file to manage your project's dependencies.
A virtual environment is an isolated environment that allows you to manage dependencies for your project without affecting the global Python installation. To create a virtual environment, follow these steps:
.venv
:
python3 -m venv .venv
This command creates a directory named
.venv
that contains a copy of the Python interpreter and a site-packages directory where dependencies will be installed.
Before installing any dependencies, you need to activate the virtual environment. Use the following command:
source .venv/bin/activate
Once activated, your terminal prompt will change to indicate that you are now working within the virtual environment.
Assuming you have a list of dependencies, you can install them using pip. If you have a