🔧 How to use Poetry in Django project
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
First you need to install poetry on your computer. There are many ways to do this, I downloaded it using brew (brew install poetry). You can download it from Poetry's own site in the appropriate way.
But let's first take a brief look at what poetry is and how it differs from virtualenv
Poetry is a tool for managing Python projects and dependencies, making it easier to handle package management and project structure. It allows developers to define and manage dependencies, create virtual environments, and handle project versioning, all in a straightforward way.
Difference from venv:
VirtualEnv: venv is a standard Python module for creating isolated virtual environments, where dependencies are installed separately from the global environment. However, venv doesn’t manage project dependencies by itself; you usually need to use something like pip to install dependencies and manually track them.
Poetry: Poetry, on the other hand, not only creates virtual environments but also automates the management of dependencies. It uses a pyproject.toml file to track dependencies, versions, and project configurations. Poetry also simplifies publishing Python packages by handling versioning and packaging.
In summary, while venv creates isolated environments, Poetry provides a more comprehensive project management solution, including dependency resolution and packaging tools.
After installing Poetry, let's create the Django project:
thehormat@Pawn Desktop % mdkir DjangoPoetry
thehormat@Pawn DjangoPoetry % cd DjangoPoetry
thehormat@Pawn DjangoPoetry % poetry init
This command will guide you through creating your pyproject.toml config.
Package name [djangopoetry]: DjangoPoetry
Version [0.1.0]:
Description []: We learn Poetry
Author [HormatHamidov <[email protected]>, n to skip]: TheHormat
License []:
Compatible Python versions [^3.12]:
Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file
[tool.poetry]
name = "djangopoetry"
version = "0.1.0"
description = "We learn Poetry"
authors = ["TheHormat"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Do you confirm generation? (yes/no) [yes]
Then we will have a file like this:
Great, now let's move on. We need to add Django to our project. But before that we need to activate poetry first, just like virtualenv logic:
thehormat@Pawn DjangoPoetry % poetry shell
Creating virtualenv djangopoetry-5djS955q-py3.12 in /Users/thehormat/Library/Caches/pypoetry/virtualenvs
Spawning shell within /Users/thehormat/Library/Caches/pypoetry/virtualenvs/djangopoetry-5djS955q-py3.12
thehormat@Pawn DjangoPoetry % emulate bash -c '. /Users/thehormat/Library/Caches/pypoetry/virtualenvs/djangopoetry-5djS955q-py3.12/bin/activate'
(djangopoetry-py3.12) thehormat@Pawn DjangoPoetry %
(djangopoetry-py3.12) thehormat@Pawn DjangoPoetry %
⬆️ If you see this result in your terminal, poetry is active.
Then let's add Django to the project:
(djangopoetry-py3.12) hormathamidov@Pawn DjangoPoetry % poetry add django
Using version ^5.1.2 for django
Updating dependencies
Resolving dependencies... (0.3s)
Package operations: 3 installs, 0 updates, 0 removals
- Installing asgiref (3.8.1)
- Installing sqlparse (0.5.1)
- Installing django (5.1.2)
Writing lock file
We will see such a novelty in our toml file. This means we've successfully set up the django:
[tool.poetry.dependencies]
python = "^3.12"
django = "^5.1.2"
And now, finally, we can install and run the django in the normal way:
(djangopoetry-py3.12) thehormat@Pawn DjangoPoetry % django-admin startproject core .
(djangopoetry-py3.12) thehormat@Pawn DjangoPoetry % python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 14, 2024 - 18:22:05
Django version 5.1.2, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I wanted to give a little help for Poetry, and you can learn more from Poetry's own documentation.
Conclusion
🗽 You don’t have to panic about it, you can easily find it and fix it after a detailed search.
🖋️ Before you go… If you have any questions/suggestions/thoughts, do drop me a line below.
And if you enjoyed this, let us know how you felt with a nice emoji(🤯❤️🔥) and don't forget to follow for future updates.
That’s it from me. We will talk soon!
— TheHormat ♟️
...
🔧 How to use Poetry in Django project
📈 39.94 Punkte
🔧 Programmierung
🔧 In-Project Virtualenvs in Poetry
📈 25.95 Punkte
🔧 Programmierung
🔧 Poetry: O Maestro dos Projetos Python 🎩✨
📈 20.78 Punkte
🔧 Programmierung
🔧 Dependency management in Python Using Poetry
📈 20.78 Punkte
🔧 Programmierung
🔧 OOP poetry part III - behavioural patterns
📈 20.78 Punkte
🔧 Programmierung
🔧 OOP poetry Part II: structural patterns
📈 20.78 Punkte
🔧 Programmierung
🔧 OOP poetry Part I: Creational patterns
📈 20.78 Punkte
🔧 Programmierung
🔧 UV как альтернатива Poetry
📈 20.78 Punkte
🔧 Programmierung
🔧 Making Blackout Poetry With Computers!
📈 20.78 Punkte
🔧 Programmierung
📰 Analysis of Palestinian Poetry
📈 20.78 Punkte
📰 IT Security Nachrichten
🔧 Poetry Web Creation
📈 20.78 Punkte
🔧 Programmierung
🎥 OHM2013: OHM Spoken poetry
📈 20.78 Punkte
🎥 IT Security Video