
Poetry is an open-source tool for managing and packaging dependencies for Python projects. Poetry maintains a clean view of your dependency tree, streamlines the installation and management of dependencies, and simplifies packaging and distribution to PyPI.
This article explains how to install Poetry and manage dependencies for a Flask application on Ubuntu 24.04. You'll install Poetry with Pipx, create a virtual environment, and run a Flask application using Poetry and Gunicorn.
Before you begin, you need to:
Install Poetry in an isolated environment to prevent conflicts between Poetry’s internal dependencies and system-wide dependencies. Follow these steps to install Pipx and use it to install Poetry.
Update the APT package index.
Install Pipx.
Add the Pipx libraries to your system’s PATH.
Enable tab completions for Pipx commands.
Reload the .bashrc shell configuration to apply the changes in your active session.
Install Poetry using Pipx.
Your output should be similar to the one below.
View the installed Poetry version.
Your output should be similar to the one below:
Initialize Poetry in an existing Python project or create a new project to use Poetry as a dependency manager. Creating a new Poetry project from scratch allows Poetry to create the project structure and configuration files. Follow the steps below to create a new Poetry project to manage dependencies.
Create a new flask-app Poetry project.
The above command creates a flask-app directory with the following files:
flask_app: Contains an __init__.py file that specifies all Flask code, including modules, functions, and other related support components.pyproject.toml: Includes information about the Project, Poetry configuration, and installed dependencies.README.md: Includes basic documentation about the Project.tests: Integrating and organizes the project’s test cases.Your output should be similar to the one below:
Switch to the flask-app project directory.
Follow the steps below to create a virtual environment and install Flask.
Specify the Python interpreter for Poetry and create a new virtual environment.
The above command instructs Poetry to use the default Python3 interpreter on your system. You can also use a specific version such as Python 3.11. For example, poetry env use python3.11 if Python 3.11 is installed on your workstation.
Your output should be similar to the one below:
Activate the virtual environment.
The above command outputs the command to run in order to activate the virtual environment manually.
Your output should be similar to the one below:
Activate the virtual environment using its path.
Your output should be similar to the one below when the virtual environment is activated.
Install Flask using Poetry.
The above command modifies the project directory with the following functionalities:
pyproject.toml file.Poetry.lock file for deterministic builds.Your output should be similar to the one below:
Deactivate the virtual environment.
Uncomplicated firewall (UFW) is active on most Ubuntu 24.04 servers by default. Configure UFW to allow connections to the Flask application port such as 5000 on your server. Follow the steps below to allow port 5000 and reload the firewall configuration.
Allow connections to the Flask application port 5000.
Reload UFW to apply the firewall configuration changes.
View the UFW status and verify that the rule is active.
Your output should be similar to the one below:
Poetry not only helps manage dependencies and create virtual environments but also allows you to run Poetry-managed Python projects. Follow the steps below to set up and run a Flask application with Poetry.
Retrieve the command to reactivate the virtual environment.
Your output should be similar to the one below:
Activate the virtual environment.
Navigate to the flask_app directory.
Create a file app.py using nano.
Add the following code.
Save the file by pressing Ctrl+X, followed by Y and then hit Enter.
This creates a webpage that returns Greetings from Vultr on the home route /.
Run the Flask application.
Your output should be similar to the one below:
Access your Flask app using the following address in a web browser. Replace <SERVER-IP> with your server’s IP address.
To stop the Flask app press Ctrl+C in your terminal session.
Poetry allows you to manage dependencies using groups. Follow the steps below to create a group called dev and install Pytest for testing.
Install Pytest in the dev dependency group.
This creates a new section in the pyroject.toml file - [tool.poetry.group.dev.dependencies], which includes the dependencies such as pytest ="^8.3.4", under the dev group.
Production dependencies should not be added to any group. But instead should be part of the root dependencies - [tool.poetry.dependencies]. This ensures that production dependencies are included in the application without being segregated into specific groups, avoiding any exclusions during deployment or packaging for production.
Navigate to your project’s tests directory.
Create a new directory named unit.
Navigate in the unit directory.
Create an __init__.py file.
__init__.py files allow you to mark a directory as a Python package allowing you to import modules from it. For more information refer to the official Python documentation.
Create a test file test_app.py using nano.
Add the following code in the file.
Save the file by pressing Ctrl+X, followed by Y and then hit Enter.
This checks that the response body from app.py in the flask_app package contains the markup <h1>Greetings from Vultr</h1>.
Run the test.
Your output should be similar to the one below:
A WSGI HTTP server such as Gunicorn allows handling client's requests concurrently and efficiently, making it ideal for production-grade Flask applications. Follow the steps below to add Gunicorn as a dependency with Poetry and configure the Flask app to run with Gunicorn.
Navigate to the flask_app directory.
Add Gunicorn as a root dependency.
Create a wsgi.py file using nano.
Add the following Python code.
Save the file by pressing Ctrl+X, followed by Y and then hit Enter.
Run the Flask app with Gunicorn.
Check that your Flask app is still accessible.
Press Ctrl+C to stop the server.
Poetry virtual environments consume space, though they can be managed and can be recreated. Follow the steps below to remove your active virtual environment and verify that it can be recreated.
Exit the virtual environment.
List all virtual environments for your project.
The environment labeled (Activated) is the one in use.
Your output should be similar to the one below:
Copy the environment name from the previous command output and remove the virtual environment.
Your output should be similar to the one below:
Recreate the virtual environment.
Retrieve the command to reactivate the virtual environment.
Your output should be similar to the one below:
Activate the virtual environment by copying and running the activation command from the previous step:
Install all the dependencies from the pyproject.toml file.
With the pyproject.toml file present, the virtual environment and dependencies can always be recreated and reinstalled.
By default, Poetry creates a virtual environment in /.cache/pypoetry/virtualenvs. However, to keep the virtual environment organized alongside your project files, you can modify its location.
Follow the steps below to set the virtual environment at the root of your project.
Exit the virtual environment.
List the virtual environments for your project.
The environment labeled (Activated) is the one in use.
Your output should be similar to the one below:
Remove the virtual environment.
Configure Poetry to allow the virtual environment to be created at the root of your project.
The above command will overwrite the default configuration set by Poetry and allows the virtual environment to be created in the root of your project.
Verify that the configuration change is applied.
Your output should be similar to the one below:
Create a virtual environment to verify.
Your output should be similar to the one below:
Retrieve the command to activate the virtual environment.
Your output should be similar to the one below:
Activate the virtual environment.
View your virtual environment information.
Your output should be similar to the one below:
Install all the dependencies in this new environment.
Run the Flask app using Gunicorn to verify that everything is running in the new environment.
Check that your Flask app is accessible.
You have installed Poetry on your Ubuntu 24.04 server and used it to manage dependencies for a Flask application. You created a Poetry project, set up a virtual environment, and configured Gunicorn to serve the Flask application. Additionally, you managed the virtual environment, streamlining your development workflow. For more information about Poetry, please visit the official Poetry documentation.
0 Comments
Be the first to comment and share your perspective with the community.