Pip and requirements.txt
pip and requirements.txt in Python
Section titled “pip and requirements.txt in Python”pip
is the official tool for installing and managing packages in Python. Together with the requirements.txt
file, it allows you to easily control your project dependencies and share them with others.
What is pip?
Section titled “What is pip?”- pip is the package manager for Python.
- It allows you to easily install, update, and uninstall libraries from the terminal.
Basic usage example
Section titled “Basic usage example”pip install package_name
What is requirements.txt?
Section titled “What is requirements.txt?”- It is a text file where all the dependencies (packages and versions) necessary for your project are listed.
- It facilitates the installation of all the libraries at once, ideal for sharing projects or working in a team.
Example of requirements.txt
Section titled “Example of requirements.txt”requests==2.31.0numpy>=1.25.0flask
How to create requirements.txt?
Section titled “How to create requirements.txt?”With your virtual environment activated and all packages installed, run:
pip freeze > requirements.txt
This will generate a file with all current dependencies.
How to install dependencies from requirements.txt?
Section titled “How to install dependencies from requirements.txt?”To install all the dependencies listed in the file:
pip install -r requirements.txt
Best practices
Section titled “Best practices”- Use a virtual environment for each project.
- Update the
requirements.txt
file each time you add or remove packages. - Include
requirements.txt
in your repository to facilitate collaboration.
Summary
Section titled “Summary”pip
allows you to install and manage packages easily.requirements.txt
helps to share and reproduce your project environment.- They are essential tools for any modern Python developer.
Ready to manage your dependencies like a pro? Try creating and using a requirements.txt
file in your next project!