Reusable Django Packages/Apps: Difference between revisions
No edit summary |
|||
| (3 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
Notes on workflow and best practices for creating and maintaining reusable packages/apps in Django.<ref>[https://docs.djangoproject.com/en/1.8/intro/reusable-apps/ How to write reusable apps] - Django Documentation</ref> | Notes on workflow and best practices for creating and maintaining reusable packages/apps in Django.<ref>[https://docs.djangoproject.com/en/1.8/intro/reusable-apps/ How to write reusable apps] - Django Documentation</ref> | ||
== | == Packaging an app == | ||
The properties of the project are defined in `setup.py` in the project root directory. | |||
This file in turn reads properties such as author and version from `__init__.py` in the root of the app itself. | |||
Version is used to name the package, and is embedded as metadata to allow installation of updates to the pacakge. | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 13: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Installing, uninstalling, and updating apps | == Distribution == | ||
Packages need to be available somewhere public in order for use in deployed web apps. | |||
=== Amazon S3 === | |||
==== Upload through web browser ==== | |||
* [https://s3.console.aws.amazon.com/s3/ Open the Amazon S3 console]. | |||
* Click on the bucket where the packages are stored, then click '''Upload'''. | |||
* Either drag the package files onto the page, or click '''Add Files'''. | |||
==== Upload on command line ==== | |||
Requires [https://docs.aws.amazon.com/cli/ aws cli]. | |||
View contents of a bucket: | |||
<pre> | |||
$ aws s3 ls <bucket-name> | |||
</pre> | |||
Use `s3 mv`, `s3 cp`, `s3 sync`, and `s3 rm` to manage files in the bucket. | |||
For example, to copy a new package to the S3 bucket: | |||
<pre> | |||
$ aws s3 cp ./my-django-pkg-0.0.1.tar.gz s3://my-bucket/path --acl public-read | |||
</pre> | |||
== Installing, uninstalling, and updating apps == | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
| Line 22: | Line 56: | ||
Make sure to take at all the versions stored in the `dist` directory to confirm that the latest version is being installed, i.e. don't assume that there is only one version stored there. | Make sure to take at all the versions stored in the `dist` directory to confirm that the latest version is being installed, i.e. don't assume that there is only one version stored there. | ||
== Using an app in a Django project == | |||
Add the app name under `INSTALLED_APPS` in the project's `settings.py` file. | Add the app name under `INSTALLED_APPS` in the project's `settings.py` file. | ||
| Line 42: | Line 76: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Confirming the version of an installed app == | |||
Use the `pip show` command, e.g.: | Use the `pip show` command, e.g.: | ||
| Line 51: | Line 85: | ||
== Notes == | == Notes == | ||
=== See also === | |||
* [[Refactoring a Django App as a Stand-Alone Module]] | |||
=== References === | |||
<references /> | <references /> | ||
Latest revision as of 17:46, 27 December 2020
Overview[edit]
Notes on workflow and best practices for creating and maintaining reusable packages/apps in Django.[1]
Packaging an app[edit]
The properties of the project are defined in setup.py in the project root directory.
This file in turn reads properties such as author and version from __init__.py in the root of the app itself.
Version is used to name the package, and is embedded as metadata to allow installation of updates to the pacakge.
$ cd /path/to/app/source/dir/ $ python setup.py sdist
Distribution[edit]
Packages need to be available somewhere public in order for use in deployed web apps.
Amazon S3[edit]
Upload through web browser[edit]
- Open the Amazon S3 console.
- Click on the bucket where the packages are stored, then click Upload.
- Either drag the package files onto the page, or click Add Files.
Upload on command line[edit]
Requires aws cli.
View contents of a bucket:
$ aws s3 ls <bucket-name>
Use s3 mv, s3 cp, s3 sync, and s3 rm to manage files in the bucket.
For example, to copy a new package to the S3 bucket:
$ aws s3 cp ./my-django-pkg-0.0.1.tar.gz s3://my-bucket/path --acl public-read
Installing, uninstalling, and updating apps[edit]
$ pip uninstall django-addresses $ pip install ~/develop/labs/shared/django-addresses/dist/django-addresses-1.2.1.tar.gz
Make sure to take at all the versions stored in the dist directory to confirm that the latest version is being installed, i.e. don't assume that there is only one version stored there.
Using an app in a Django project[edit]
Add the app name under INSTALLED_APPS in the project's settings.py file.
# my_project/settings.py
# ...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiels',
'contact_info',
)
Confirming the version of an installed app[edit]
Use the pip show command, e.g.:
$ pip show django-contact_info
Notes[edit]
See also[edit]
References[edit]
- ↑ How to write reusable apps - Django Documentation