I've been working on a few Python projects recently, and I've been thinking about how I can make the setup process easier. I've been using a few tools and libraries that have made my life easier, and I thought I'd share them with you.

TLDR🔗

  • use asdf to install poetry and python
  • run a specific version of poetry to create the project directory
  • inside the project directory pin poetry and python with asdf local
  • use poetry to add dependencies (like django)
  • poetry also has a run command that can be used to run the django server (or any other commands you might want)

Lets see it in action🔗

Add the asdf plugins.

> asdf plugin add python
> asdf plugin add poetry

Install python.

> asdf install python 3.10.10

Install poetry (the poetry plugin doesn't know how to translate latest into a version number, so we have to look it up).

> asdf list all poetry | tail -n 1
  1.8.1
 
> asdf install poetry 1.8.1

Ok. lets see what we got here.

> asdf list python
 *3.10.10
  3.11.7
  3.7.4
  3.9.18
 
> asdf list poetry
  1.7.1

What about when we use the tools directly?

> poetry --version
Poetry (version 1.8.1)
 
> python --version
Python 3.10.10

👍 sterling.

Lets setup a new project.

> env ASDF_POETRY_VERSION=1.8.1 poetry new my-project
Created package my_project in my-project
 
> cd ./my-project
 
> asdf looal python 3.10.10
> asdf local poetry 1.8.1

Lets add Django.

> poetry add django
Creating virtualenv my-project-6y3EQ0-w-py3.10 in /home/zenobius/.cache/pypoetry/virtualenvs
Using version ^5.0.2 for django
 
Updating dependencies
Resolving dependencies... (1.0s)
 
Package operations: 4 installs, 0 updates, 0 removals
 
  - Installing typing-extensions (4.10.0)
  - Installing asgiref (3.7.2)
  - Installing sqlparse (0.4.4)
  - Installing django (5.0.2)
 
Writing lock file

Now that we have django install, it's time to create our app.

> python -m django --version
5.0.2
 
> poetry run django-admin startproject myproject ./
 
> ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
 
> ./manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
 
System check identified no issues (0 silenced).
February 28, 2024 - 07:22:45
Django version 5.0.2, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Screenshot from 2024-02-28 18-21-13.png

Poetry🔗

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on, and it will manage the installation and updating of these libraries for you. In a lot of ways these feels just like NPM/Yarn/Pnpm... for Python.

ASDF🔗

ASDF is a tool for managing multiple runtime versions. It's like NVM, but for more than just Node. It supports Python, Node, Ruby, Java, and pretty much anything you can write a simple bash plugin for.

~