Defining Django App Dependencies
Overview
3rd party packages are listed in requirements.txt.
Non-Python libraries and other Django application configuration directives go in .config files in .ebextentions/.
requirements.txt
It's important to always work in a virtual Python environment when working on Django projects. This way the list of active Python packages that have been installed to support the project will be tracked by pip.
It's possible at any time to list the currently installed Python packages with pip freeze.
To deploy the project's dependencies on another installation, first save the environment in requirements.txt:
$ pip freeze > requirements.txt
Creating this file in Windows, even after changing the line endings to Unix-style line endings, caused an error when running pip install on the the production Linux server (in codec.py).
psycopg2 will throw an error if PostgreSQL is not installed on the system first.
Lines can be commented out in requirements.txt with the pound (#) character.
Deploying local stand-alone packages
See Packaging Stand-Alone Django Apps
Push the package's distribution to GitHub.
Add a line to requirements.txt to pull the package distribution from GitHub:
-e git://github.com/account_name/package_repo.git#egg=package_name
Replacing account_name, package_repo, and package_name in the URL with the appropriate values.
When a package is installed this way, it's necessary to add a directive in .ebextensions/ to install Git via yum.
Manually Python package dependencies
$ pip install -r requirements.txt
Installing Python packages with AWS Elastic Beanstalk
eb create and eb deploy automatically attempts to install all the packages found in requirements.txt.
.ebextensions/
.config files placed in the .ebextensions/ directory will be executed in alphabetical order. The convention is to name them 01-xxx.config, 02-xxx.config, etc. With the lower-numbered files being executed first in the sequence.
- It's necessary to install PostgreSQL if
psycopg2is present inrequirements.txt. - It's necessary to install Git if any Python packages are pulled from GitHub.
To install non-Python applications using yum, create .ebextensions/01-packages.config:
packages:
yum:
git: []
postgresql93-devel: []
To run manage.py tasks, create .ebextensions/02-python.config:
container_commands:
01_migrate:
command: "django-admin migrate"
leader_only: true
02_collectstatic:
command: "python manage.py collectstatic --noinput"
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "northrose.settings"
PYTHONPATH: "/opt/python/current/app/northrose:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: "northrose/wsgi.py"
numProcesses: 3
numThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
It's possible in that python manage.py migrate --noinput might work in place of django-admin migrate in the example above.
Notes
See also
- Python Project Dependencies
- install_requires vs Requirements files - Python Packaging User Guide