back to tutorials

Managing a Django Project with Poetry

Poetry is relatively new packaging and dependency manager. It makes it very easy to upload libraries to PyPI, manage dependencies visually, and has a couple of handy features. Today, I'm not going to do a deep dive into how Poetry works and all its features. Today I just want to focus on configuring a Django project.

1. Install Poetry

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

2. Create a Directory for you Django Project

mkdir django_poetry_example && ls django_poetry_example

3. Initiate a Poetry Project

poetry init

You will be asked to confirm the information about your project. You can skip through most of it.

Poetry Init Output.png

4. Add the Necessary Dependencies

Run poetry add django. Poetry will add django to the pyproject.toml file under the dependencies section. A virtual environment will also be created for you.

5. Start you Django Project

django-admin startproject django_poetry_example .

6. Working on your Django Project

When you need to run any python function (for example, python manage.py createsuperuser) you have two options.

  1. You can leverage poetry run, which will run against the current project's dependencies. The command will be this: poetry run python manage.py createsuperuser.
  2. You can activate the virtual environment with a poetry shell command. Now you can run python commands, as is. They will be run with dependencies you have installed.

Bonus. Export dependencies to a requirements.txt

If you need to have the requirements.txt file with all the dependencies, you can run poetry export -f requirements.txt --output requirements.txt. If you have configured a CI/CD job that auto deploys your project, you can add this function as a step, which will generate the updated version on each update.

Bonus II. Video

If you prefer a more visual approach, I have made a video that shows how to start a Django project with Poetry.

If you have any feedback, please let me know on Twitter. Your likes, retweets, and replies will show up here.

Share

If you enjoyed this post, or found it useful, please consider signing up to my newsletter. Every week, I share my thoughts on cool stuff I found around the internet. No spam, ever. Unsubscribe any time.

29 Webmentions

Want to respond? Reply, like, reply or bookmark on Twitter :)

15 Likes

3 Retweets

7 Replies

Rasul Kireev

So Django News doesn't have a Twitter handle? - (Link)

William Vincent

We're not DjangoWeekly btw! But I know you meant DjangoNews :) - (Link)

Jeff Triplett 😷

It's @djangonewsbot but someone (*clears throat me*) needs to set a cronjob to get links flowing here. - (Link)

William Vincent

Jeff is the automator in our partnership. I'm neutral at best about Twitter but he's a fanboy so I'll happily let him take ownership of Twitter-game for our newsletter :) - (Link)

Rasul Kireev

🤣 Nice - (Link)