Start a Virtualenv Django Shell From the Linux Desktop

If you are tired to fire a terminal window, cd to your project directory and activate your python virtualenv to get to your Django project, you will find here some tips to improve things a little bit.

This tip is divided in two parts :

  1. First we create a shell startup script that activates the virtualenv, bash completion and cd in the project directory.
  2. Then we create a Linux Desktop Entry file That spawns a console in our environment.

Here you have the startup script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
#
# The layout of the development environment is assumed to be:
#
# <pyton virtual env>/
#   src/
#     <project name>/
#       .consolerc (this file)
#       setup.py
#       ...
#       <project name>/
#         manage.py
#         settings.py
#         ...
#

# Run the standard bash rc file
source ~/.bashrc

# Get the current source file name
current="${BASH_SOURCE[0]}"

# Retrieve the source directory
DJANGO_SOURCE_DIR="$(dirname "$(readlink -f "$current")")"

# Get the Django related directories
DJANGO_PROJECT_NAME="$(basename "$DJANGO_SOURCE_DIR")"
DJANGO_ENV_DIR=$(readlink -f "${DJANGO_SOURCE_DIR}/../../")
DJANGO_PROJECT_DIR="${DJANGO_SOURCE_DIR}/${DJANGO_PROJECT_NAME}"

# Activate the environment
source "${DJANGO_ENV_DIR}/bin/activate"
cd "$DJANGO_PROJECT_DIR"
export PATH="$PATH:$(pwd)"

# Retrieve the Django bash completion file (only once) and execute it.
# This is potentially insecure.
DJANGO_BASH_COMPLETION="${DJANGO_SOURCE_DIR}/.django_bash_completion"
if [ ! -f "$DJANGO_BASH_COMPLETION" ]; then
  curl http://code.djangoproject.com/svn/django/trunk/extras/django_bash_completion -o "$DJANGO_BASH_COMPLETION" 2>/dev/null
fi
source "$DJANGO_BASH_COMPLETION"

# Miscellaneous
alias runserver='cd $DJANGO_PROJECT_DIR;manage.py runserver 0.0.0.0:8000'

The comment at the beginning explains how the project directory layout is assumed to be. That is the only assumption that makes the script. In consequence, it is reusable as is in any other project.

Here is the .desktop file that runs a terminal console with our script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[Desktop Entry]
Exec=/bin/bash --rcfile .consolerc
GenericName[fr]=MyProject Django
GenericName=MyProject Django
Icon=/home/antoine/images/django-icon_0.png
MimeType=
Name[fr]=MyProject Django
Name=MyProject Django
Path=/home/antoine/src/django/my_project/src/my_project/
StartupNotify=true
Terminal=true
TerminalOptions=
Type=Application
Categories=Development

The command runs in a terminal because of Terminal=true. You can see that apart from Name and GenericName, the only line specific to the project is

1
Path=/home/antoine/src/django/my_project/src/my_project/

It defines the project path, making it easy to reuse. The execution of our init script is done through:

1
Exec=/bin/bash --rcfile .consolerc

The Icon is the familiar Django icon :

/images/django-icon_0.png

I personally put the .desktop file in $HOME/Desktop, but it also can reside in $HOME/.local/share/applications. In that case, the entry will be available in the menu. I’ve tested this under KDE, but it should work also with Gnome.