Python, a versatile and powerful programming language, offers different tools and methods to manage project dependencies. Whether you're working on a small script or a large-scale application, the ability to control the libraries and packages your project relies on is crucial. In this blog post, we'll explore various methods for managing Python project dependencies, each with its own strengths and use cases.
Using Pip and a Requirements File
A requirements file is a straightforward yet effective method for specifying and managing your project's dependencies. It's particularly useful when sharing your project with others or deploying it to different environments. Here's how to create and use a requirements file:
- Activate your Python virtual environment, where you've already configured all the essential Python packages for your project.
pip freezecommand to list out packages and store these packages in requirements file (requirements.txt)
pip freeze > requirements.txt
- You can utilize the
requirements.txtfile to install all the packages along with their project dependencies by using
pip install -r requirements.txt
This way makes it easier to copy your setup to other environments or share the tools your project needs. PIP is a handy tool for any developer to have in their toolbox. You can check out more on PIP commands here.
Poetry is a new and user-friendly tool for Python. It helps manage the things your project relies on and package your work. It uses a
pyproject.toml file to keep track of what your project needs. Here's how you can use Poetry:
- Install Poetry globally on your machine using
pip install poetry
- Create new project using
poetry new myproject
- Edit the
pyproject.tomlfile to list your project's dependencies:
python = "^3.8"
requests = "^2.25.1"
numpy = "^1.21.0"
- Install dependencies using
You have the option to upload these files to your Git repositories and manage dependencies during the build process.