Managing Python Dependencies: The Battle between Pip and Poetry

Utkarsh Shukla
4 min readApr 4, 2023

--

Utkarsh Shukla pip vs poetry python

Python has emerged as a popular language for data science, web development, and other applications. While Python comes with many built-in libraries, developers often need to use external packages to add specific functionality to their projects. Two of the most popular package management tools in Python are pip and Poetry. In this blog post, we will compare the two tools and provide a demonstration of when to use each one.

Pip

Pip is the default package manager for Python and has been around for quite some time. Pip is easy to use and provides a simple way to install and manage Python packages. With pip, you can install packages from the Python Package Index (PyPI) or other package indexes. Pip also allows you to specify package dependencies in a requirements.txt file, which is a simple text file that lists the package names and versions.

One of the major benefits of pip is its ease of use. Pip is a command-line tool that can be used on all operating systems. With just a few commands, you can install, update, and uninstall Python packages. Pip also integrates well with virtual environments, which is a powerful tool for isolating project dependencies.

Poetry

Poetry is a newer package management tool that provides more advanced features than pip. Poetry is designed to manage dependencies and packages in Python projects more efficiently. It provides features such as dependency resolution, version control, and package building.

One of the key advantages of Poetry is that it simplifies the package management process. Poetry uses a pyproject.toml file to define the project’s dependencies, and it automatically generates a lock file to ensure that the dependencies are installed consistently. Poetry also provides support for virtual environments and allows you to create isolated environments for each project.

When to use Pip

Pip is an excellent choice for small to medium-sized projects. If you’re working on a simple project with a small number of dependencies, pip is an easy-to-use tool that will get the job done quickly. Pip is also a great choice if you’re new to Python and want to get started quickly.

When to use Poetry

Poetry is an excellent choice for larger, more complex projects. If you’re working on a project with many dependencies or if you need to manage multiple projects with different dependencies, Poetry is a great choice. Poetry makes it easy to manage dependencies and ensures that each project has its own isolated environment.

Demonstration

Let’s look at a demonstration of using pip and Poetry in a Python project. In this example, we will create a simple Flask web application that uses the Flask-SocketIO package to add real-time communication to the app.

Step 1: Installing Flask and Flask-SocketIO using Pip

First, we will use pip to install Flask and Flask-SocketIO:

pip install flask
pip install flask-socketio

Step 2: Creating a Flask app

Next, we will create a simple Flask app that uses Flask-SocketIO. Here is the code for the app:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/')
def index():
return render_template('index.html')

@socketio.on('message')
def handle_message(message):
print('received message: ' + message)
emit('message', message, broadcast=True)

if __name__ == '__main__':
socketio.run(app)

Step 3: Running the Flask app

Finally, we will run the Flask app:

python app.py

Step 4: Installing Flask and Flask-SocketIO using Poetry

Now let’s use Poetry to manage our project dependencies. First, we need to install Poetry:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

This command will download and install the latest version of Poetry.

Next, we will create a new project using Poetry:

poetry new my-project
cd my-project

This will create a new project directory called my-project.

Now, we need to add the dependencies for Flask and Flask-SocketIO to our project. We can do this by editing the pyproject.toml file:

[tool.poetry.dependencies]
flask = "^2.1.0"
flask-socketio = "^5.1.1"

The ^ character in front of the version number means that we want Poetry to install the latest version that is compatible with the specified version.

Next, we need to install the dependencies using Poetry:

poetry install

This will create a virtual environment for our project and install the dependencies in it.

Step 5: Creating a Flask app with Poetry

Now that we have installed the dependencies using Poetry, we can create our Flask app:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/')
def index():
return render_template('index.html')

@socketio.on('message')
def handle_message(message):
print('received message: ' + message)
emit('message', message, broadcast=True)

if __name__ == '__main__':
socketio.run(app)

Step 6: Running the Flask app with Poetry

Finally, we can run the Flask app using Poetry:

poetry run python app.py

This will activate the virtual environment created by Poetry and run the app.

Conclusion

In conclusion, pip and Poetry are two popular package management tools for Python. Pip is simple and easy to use, making it an excellent choice for small to medium-sized projects. Poetry provides more advanced features and is a great choice for larger, more complex projects. When deciding which tool to use, consider the size and complexity of your project and your familiarity with Python package management tools. Ultimately, the choice between pip and Poetry comes down to personal preference and the specific needs of your project.

Still Curious? Visit my website to know more! Github Link

Checkout my Interviews and Podcast at- Professionals Unplugged

For more interesting Blogs Visit- Utkarsh Shukla Author, Utkarsh Shukla Blogs

--

--

Utkarsh Shukla

Host at Professionals Unplugged | Senior Software Engineering Consultant | AWS Certified Cloud Practitioner | Transforming Ideas into Impactful Web Applications