My Python (and Django) Set-Up

My Python (and Django) Set-Up

Tired of installing python versions in the system, creating virtual environments in projects, pip freeze-ing, maintainingrequirements.txt, I tried some tools last month: pipenv, pyenv and poetry

TL;DR

My old set-up: system-installed python versions + python[version] -m pip + virtualenv
My current set-up: pyenv + poetry
Set-Up Django projies using poetry run django-admin.py startproject dj_server .

pyenv

pyenv lets you easily switch between multiple versions of Python.

Installation

The official installation guide is this.
How I installed: Using pyenv-installer

curl https://pyenv.run | bash

poetry

poetry is a tool for dependency management and packaging in Python.

Installation

The official installation guide is this.
How I installed:

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

Installing different Python versions

 pyenv install 3.7.0
 pyenv install 3.8.0
 pyenv install 3.9.0
 pyenv versions
* system (set by /home/vibhu/.pyenv/version)
  3.7.0
  3.8.0
  3.9.0

My Usage

I don't want to mess up with my system-installed python(2.7), but I also want to use a different python version when I use python command. I've got a directory where I place all my projects where I usually work with python-3.7

 pwd
/home/vibhu/.../Projects

 python --version
2.7.17

 pyenv local 3.7.0

 python --version
3.7.0

In different projects, I need python virtual environments. With poetry, I don't have to maintain a file (like requirements.txt) to keep a track of my dependencies. poetry provides it out-of-the-box using pyproject.toml and poetry.lock.

❯ poetry new my_billion_dollar_project
Created package my_billion_dollar_project in my_billion_dollar_project

❯ cd my_billion_dollar_project/
❯ poetry add pandas
❯ tree .
.
├── my_billion_dollar_project
│   └── __init__.py
├── poetry.lock
├── pyproject.toml
├── README.rst
└── tests
    ├── __init__.py
    └── test_my_billion_dollar_project.py

When I add the first package, poetry created a virtual environment (because there wasn't any, already) and added a poetry.lock file.

  • pyproject.toml doesn't contain all of the packages currently installed in the environment, but only the ones which I installed explicitly.
  • poetry.lock is something which is similar to requirements.txt, but way more verbose and contains info about all of the packages installed in the environment with the exact version.
❯ cat pyproject.toml

[tool.poetry.dependencies]
python = "^3.7"
pandas = "^1.1.4"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

To activate the virtual environment, I just do poetry shell
See: The pyproject.toml file
Also see: Tom's Obvious, Minimal Language

Django

Most of my work involves working on Django-based projies and I want to work on the latest python version. poetry new creates a package-like directory structure (as shown above) which I don't usually want, so to create a new Django project I follow these steps:

pwd
/home/vibhu/.../Projects
❯ mkdir new_django_project
❯ ls
my_billion_dollar_project  new_django_project

❯ cd new_django_project/
❯ pyenv local 3.9.0
❯ poetry init

An interactive session will open where I mostly press Enter and continue.

Package name [new_django_project]:  
Version [0.1.0]:  
Description []:  
Author [Vibhu Agarwal <vibhu4agarwal@gmail.com>, n to skip]:  
License []:  
Compatible Python versions [^3.9]:  

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

Now I'd install Django and activate the environment.

❯ poetry add django
❯ poetry shell

Following command will run in the poetry-created python virtual environment.

❯ django-admin.py startproject dj_server .

The final directory structure is fairly simple.

tree .
.
├── dj_server
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── poetry.lock
└── pyproject.toml

Peace.