pip freeze is a command-line tool that displays a list of installed packages and their versions. You can use this command to create a requirements.txt file that captures the dependencies of your Python project. Here's how you can do it:
-
Open a terminal or command prompt.
-
Navigate to your project directory using the
cdcommand. -
Run the following command to generate a
requirements.txtfile:pip freeze > requirements.txtThis command redirects the output of
pip freezeto a file namedrequirements.txt. The file will contain a list of all installed packages and their versions. -
You can view the contents of the
requirements.txtfile using a text editor or by using thecatcommand on Unix-based systems ortypecommand on Windows:cat requirements.txt # Unix-based systems type requirements.txt # Windows
The requirements.txt file can be used to recreate the environment on another machine or share the project's dependencies with others. To install the dependencies listed in requirements.txt, you can use the following command:
pip install -r requirements.txtThis command reads the requirements from the file and installs the specified packages and versions.
Remember to update your requirements.txt file whenever you add or remove dependencies from your project by rerunning the pip freeze command.