Pawel Zadrozny


About confusing or poorly documented features of tools and libraries I use.

Tired of typing 'python'?

26 Feb 2018 tools | #django #shell

Using lots of Django commands in a row can be tiring. Imagine creating new migrations, and then sync some data and then run dev server to verify whole process. It may looks like this:

(venv) $ python makemigrations
(venv) $ python migrate
(venv) $ python make_something
(venv) $ python sync_service --with=args
(venv) $ python runserver

It’s a lot of typing!

Solution 1: shortcut shell script

The easiest way to merge two keywords into one is to create an alias script.

$ sudo vim /bin/django

And this is it:

python "[email protected]"

Make it executable:

$ sudo chmod a+x /bin/django

Now use it like this:

(venv) $ django makemigrations
(venv) $ django migrate
(venv) $ django make_something
(venv) $ django sync_service --with=args
(venv) $ django runserver

Much better. It works good on one machine, but when you have to manage many machines… Copying and pasting the same script to every single machine isn’t a good idea.

Solution 2: add to virtualenv path

Every project has or should have file. All you need is to add to setup and voilà.

Minimal looks like:

from setuptools import find_packages, setup

    packages=find_packages(exclude=('docs', 'media', 'static')),
    package_dir={'mysite': 'mysite'},
    scripts=[''],  # <-- relative path to

And now project can be installed using pip as always

(venv) $ pip install /path/to/project

Setup will take care of adding to environment path. Now it is available directly like any other executable script.

(venv) $ runserver