Python
Virtual environments & Django shortcuts
Python development is better with proper environment isolation. These tools make it painless.
Virtual Environments
venv — Create & Activate
Create and activate a virtual environment in one command:
venv # Creates ./venv and activates it
venv myenv # Creates ./myenv and activates itNo more separate python -m venv and source activate steps.
va — Auto-Activate
Smart virtualenv activation that searches up the directory tree:
va
# Searches for: venv/, .venv/, env/, .env/
# Activates the first one found
# Checks parent directories tooPerfect for jumping into projects. No need to remember where the venv is.
vd — Deactivate
Exit the current virtualenv:
vd
# Simple alias for deactivateDjango Utilities
Quick shortcuts for Django development. All require being in a Django project.
Development Server
drs # python manage.py runserver (port 8000)
drs 3000 # python manage.py runserver 3000Shell
dsh
# Uses shell_plus if django-extensions is installed
# Falls back to regular Django shellshell_plus auto-imports all models. Install it: pip install django-extensions
Migrations
dmm # python manage.py makemigrations
dm # python manage.py migrateThe two most-used Django commands, now 3 keystrokes each.
Admin
dsu # python manage.py createsuperuserCreate admin users quickly.
Tests
dt # Run all tests
dt myapp # Run specific app's tests
dt myapp.tests.test_views # Specific test moduleCode Quality
Formatting
black . # Format all Python files with Black
isort . # Sort importsOr use a combined workflow (see below).
Linting
flake . # Run flake8 on current directoryCatches style issues and potential bugs.
Testing
pytest
pt # Run pytest (verbose, short traceback)
ptd # Run with debug logging enabledpytest is better than unittest. Fight me.
Workflows
New Django Project
# Create project directory
mkdir myproject && cd myproject
# Create and activate venv
venv
# Install Django
pip install django
# Start project
django-admin startproject config .
# Run dev server
drsExisting Project
# Enter project
cd ~/projects/django-app
# Auto-activate venv
va
# Install dependencies
pip install -r requirements.txt
# Run migrations (if needed)
dm
# Start server
drsBefore Commit
# Format code
black .
isort .
# Lint
flake .
# Run tests
pt
# If all green, commit
gigDjango Development Loop
# Make model changes
vim myapp/models.py
# Create migrations
dmm
# Apply migrations
dm
# Test in shell
dsh
# >>> from myapp.models import MyModel
# >>> MyModel.objects.all()
# Run tests
dt myapp
# Start server
drsManaging Dependencies
# Activate venv
va
# Install new package
pip install requests
# Freeze dependencies
pip freeze > requirements.txt
# Or use pip-tools for better management:
pip install pip-tools
pip-compile requirements.in
pip-sync requirements.txtPro Tips
Always use virtualenvs: Never install packages globally (except pip, virtualenv itself). venv makes it trivial.
Use .env for secrets: Store database passwords, API keys in .env files. Load with python-dotenv.
Format before committing: Black is opinionated. Just accept it and run black . before every commit.
django-extensions is essential: Get shell_plus, runserver_plus, and other useful management commands. pip install django-extensions
pytest > unittest: More concise, better fixtures, better plugins. Install with pip install pytest pytest-django.
Use requirements.in: Keep high-level deps in requirements.in, generate requirements.txt with pip-compile. Lock exact versions.
Virtual environment in project: Use venv or .venv as the directory name. Editors auto-detect these.
Activate on cd: Consider adding auto-activation to your shell config (check if in git repo, look for venv, activate). The va command is a manual version of this.