Defining Django App Dependencies: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:Django Category:Web Development There are two files that control app dependencies: `setup.py` and `requirements.txt`. == Notes == === See also === * [http...")
 
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Django]] [[Category:Web Development]]
[[Category:Django]] [[Category:Web Development]]
== Overview ==


There are two files that control app dependencies: `setup.py` and `requirements.txt`.
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`:
 
<syntaxhighlight lang="sh">
$ pip freeze > requirements.txt
</syntaxhighlight>
 
<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">`psycopg2` will throw an error if PostgreSQL is not installed on the system first.</p>
 
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:
 
<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`.
 
=== Manually Python package dependencies ===
 
<syntaxhighlight lang="sh">
$ pip install -r requirements.txt
</syntaxhighlight>
 
=== 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. <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 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>
 
To run `manage.py` tasks, create `.ebextensions/02-python.config`:
 
<syntaxhighlight lang="yaml">
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>
 
It's possible in that `python manage.py migrate --noinput` might work in place of `django-admin migrate` in the example above.


== Notes ==
== Notes ==
Line 7: Line 94:
=== See also ===
=== See also ===


* [[Python Project Dependencies]]
* [http://python-packaging-user-guide.readthedocs.org/en/latest/requirements/ install_requires vs Requirements files] - Python Packaging User Guide
* [http://python-packaging-user-guide.readthedocs.org/en/latest/requirements/ install_requires vs Requirements files] - Python Packaging User Guide
* [https://caremad.io/2013/07/setup-vs-requirement/ setup.py vs requirements.txt] Donald Stufft


=== References ===
=== References ===
<references />
<references />

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 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[edit]

See also[edit]

References[edit]