Packaging Stand-Alone Django Apps: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
Line 51: Line 51:
=== Installing the package ===
=== Installing the package ===


<syntaxhighlight lang="bash">
Push the package distribution to GitHub.
$ pip install --user django-addresses/dist/django-addresses-0.1.tar.gz
 
Add the package and version to `requirements.txt` in the project's root directory:
 
<syntaxhighlight lang="text">
-e git://github.com/account_name/package_repo.git#egg=package_name
</syntaxhighlight>
</syntaxhighlight>


This installs the package on a system as a ''user'' library.
Replacing `account_name`, `package_repo`, and `package_name` in the URL with the appropriate values.


Or, using `littled-addresses` as an example, and installing from within the project directory:<br />''N.B. user site-packages are not visible in virtualenvs. Also, the `.tar.gz` vs`.zip` extension just depends on however it was compressed.''
Install the package with `pip`. (Make sure that the project's virtual environment has been activated first.)


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
$ sudo pip install dist/addresses-0.1.zip
$ pip install -r requirements.txt
</syntaxhighlight>
</syntaxhighlight>
It can now be referenced in other Django projects.
On Windows, when the package is installed for a specific user, the files are placed in `~/AppData/Roaming/Python[version]/site-packages/`.


=== Upgrading the package ===
=== Upgrading the package ===


<syntaxhighlight lang="bash">
Update the version number in `requirements.txt`.
$ pip install --user --upgrade django-addresses/dist/django-addresses-0.2.tar.gz
</syntaxhighlight>


<div class="alert alert-warning">This is essential if the models in the app change. Uninstalling the app will remove the migrations, causing any Django projects that use the app to not be capable of syncing up their database structure with the changes to the models.</div>
Run `pip install -r requirements.txt`


=== Using the package in a project ===
=== Using the package in a project ===
Line 80: Line 78:


<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
|
|- django-addresses
+-- django-addresses
|
      |
|  |- addresses
      +-- addresses
|  |  |
      |   |
|  |  `-- [...]
      |   +- [...]
|   |
      |
|   `-- [...]
      [...]
|
`-- [...]
</syntaxhighlight>
</syntaxhighlight>



Revision as of 17:46, 11 February 2016

Directory structure

The app goes in a directory outside of any Django web project. If the app is addresses, structure it littled-addresses/addresses/.[1]

Try to avoid naming conflicts, of course. (Check resources like PyPI.)

Building the package

Before committing changes to the package, make sure to update the version if needed.
|
 +- [package_root]
      |
      +- [package_dir]
      |    |
      |    +- __init__.py > __version__
      | 
      +- [...]
      |
      +- setup.py > setup.version

Command to build the package:

$ python setup.py sdist
  • Run from the package root directory.
  • Creates a dist directory which contains the (zipped) package.

Using the package

Follow the instructions for upgrading the package if the package has already been installed.

Installing the package

Push the package distribution to GitHub.

Add the package and version to requirements.txt in the project's root directory:

-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.

Install the package with pip. (Make sure that the project's virtual environment has been activated first.)

$ pip install -r requirements.txt

Upgrading the package

Update the version number in requirements.txt.

Run pip install -r requirements.txt

Using the package in a project

Assuming that the package is

|- django-addresses
|   |
|   |- addresses
|   |   |
|   |   `-- [...]
|   |
|   `-- [...]
|
`-- [...]

In the project's settings.py:

INSTALLED_APPS = (
        ...
        'addresses',
    )

Then from the web project root:

$ python manage.py makemigrations addresses
$ python manage.py migrate addresses

To reference the objects in a model within the web project:

class Person(models.Model):
    address = models.ForeignKey('addresses.Address')
    # ...

Resetting migrations

In the case that migrations have been made, and you want to rebuild the database objects from scratch:

$ python manage.py migrate --fake addresses zero
$ python manage.py migrate addresses
  • The first command sets the migration counter to before the initial (0001) migration.
  • The 2nd command migrates the models at the state of the latest migration.[2]

Uninstalling the package

$ pip uninstall django-addresses

Uninstalling a stand-along package will remove the migrations for that package. Without the migrations, Django projects won't be able to sync to any changes to the app's models. It's better to upgrade than to uninstall and install.[3]

Notes

  1. How to Write Reusable Apps (Django documentation)
  2. How to reset migrations in Django 1.7? (Stackoverflow)
  3. Migrations (Django documentation)