Defining Django App Dependencies: Difference between revisions
| (One intermediate revision by the same user not shown) | |||
| Line 14: | Line 14: | ||
To deploy the project's dependencies on another installation, first save the environment in `requirements.txt`: | To deploy the project's dependencies on another installation, first save the environment in `requirements.txt`: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="sh"> | ||
$ pip freeze > requirements.txt | $ pip freeze > requirements.txt | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 42: | Line 42: | ||
=== Manually Python package dependencies === | === Manually Python package dependencies === | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="sh"> | ||
$ pip install -r requirements.txt | $ pip install -r requirements.txt | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 52: | Line 52: | ||
== `.ebextensions/` == | == `.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. | `.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. <ref>[https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html Customizing Software on Linux Servers] - AWS documentation</ref> | ||
* It's necessary to install PostgreSQL if `psycopg2` is present in `requirements.txt`. | * It's necessary to install PostgreSQL if `psycopg2` is present in `requirements.txt`. | ||
Latest revision as of 12:27, 24 July 2018
Overview[edit]
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[edit]
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[edit]
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[edit]
$ pip install -r requirements.txt
Installing Python packages with AWS Elastic Beanstalk[edit]
eb create and eb deploy automatically attempts to install all the packages found in requirements.txt.
.ebextensions/[edit]
.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. [1]
- 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[edit]
See also[edit]
- Python Project Dependencies
- install_requires vs Requirements files - Python Packaging User Guide
References[edit]
- ↑ Customizing Software on Linux Servers - AWS documentation