Defining Django App Dependencies: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
Line 2: Line 2:
== Overview ==
== Overview ==


There are two files that control app dependencies: `setup.py` and `requirements.txt`.
3rd party packages are listed in `requirements.txt`.


`setup.py` is a list of "abstract" dependencies without a specification as to their source.
Non-Python libraries and other Django application configuration directives go in `.config` files in `.ebextentions/`.


`requirements.txt` is more specific about source and version number.<ref>[https://caremad.io/2013/07/setup-vs-requirement/ setup.py vs requirements.txt] Donald Stufft</ref>
== requirements.txt ==


== Packaging dependencies ==
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`.


In the project's root directory:
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`:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 17: Line 19:


<p class="alert alert-warning">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`).</p>
<p class="alert alert-warning">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`).</p>
`pip freeze` will list everything installed via `pip` in an environment. Some of these thing will not be requirements of the web app, and should be removed from `requirements.txt`.


<p class="alert alert-warning">`psycopg2` will throw an error if PostgreSQL is not installed on the system first.</p>
<p class="alert alert-warning">`psycopg2` will throw an error if PostgreSQL is not installed on the system first.</p>
Line 24: Line 24:
Lines can be commented out in `requirements.txt` with the pound (`#`) character.
Lines can be commented out in `requirements.txt` with the pound (`#`) character.


=== Deploying local packages ===
=== 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:
 
<syntaxhighlight lang="text">
-e git://github.com/account_name/package_repo.git#egg=package_name
</syntaxhighlight>
 
Replacing `account_name`, `package_repo`, and `package_name` in the URL with the appropriate values.
 
<p class="alert alert-warning">When a package is installed this way, it's necessary to add a directive in `.ebextensions/` to install Git via `yum`.


Upload the package distribution with `scp` (works on Mac terminal and Windows Powershell)
=== Manually Python package dependencies ===


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ scp -i ~/.ssh/app_aws.pem /path/to/package/dist/package-0.1.zip eb_user@ec2-x.x.x.x.us-west-2.compute.amazonaws.com:package-0.1.zip
$ pip install -r requirements.txt
</syntaxhighlight>
</syntaxhighlight>


Replacing
=== Installing Python packages with AWS Elastic Beanstalk ===


* `~/.ssh/app_aws.pem` with path to `.pem` file containing key for the AWS app user.
`eb create` and `eb deploy` automatically attempts to install all the packages found in `requirements.txt`.  
* `/path/to/package/dist/package-0.1.zip` with the path of the actual python package.
* `ec2-x.x.x.x.us-west-2.compute.amazonaws.com` with actual Public DNS of the app, from '''AWS Management Console''' > '''Services''' > '''EC2''' > '''Intances''' > '''Instances''' > ''instance-name'' > '''Public DNS'''
* `:package-0.1.zip` with the name given to the file on the server (required)


Then from the Django project directory:
== `.ebextensions/` ==


<syntaxhighlight lang="bash">
`.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.
$ pip install ../package-0.1.zip
 
* It's necessary to install PostgreSQL if `psycopg2` is present in `requirements.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`:
 
<syntaxhighlight lang="yaml">
packages:
  yum:
    git: []
    postgresql93-devel: []
</syntaxhighlight>
</syntaxhighlight>


== Installing dependences ==
To run `manage.py` tasks, create `.ebextensions/02-python.config`:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="yaml">
$ pip install -r requirements.txt
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/"
</syntaxhighlight>
</syntaxhighlight>
It's possible in that `python manage.py migrate --noinput` might work in place of `django-admin migrate` in the example above.


== Notes ==
== Notes ==

Revision as of 17:58, 11 February 2016

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 psycopg2 is present in requirements.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

References