1 可重用性很重要
可重用性是 Python 的根本。 The Python Package Index (PyPI) 有大量的包(即模块), Django Packages 也有大量可重用的应用,它们都可以并入你的项目。
模块通过 import foo.bar 或 from foo import bar 的形式导入。一个目录要想成为一个模块,它必须包含特定的文件
__init__.py
,即便文件为空。 Django 应用是专用于 Django 项目的 Python 模块,包含 models 、 tests 、 urls 、 views 等子模块。
2 你的项目和可复用应用
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
0001_initial.py
models.py
static/
polls/
images/
background.gif
style.css
templates/
polls/
detail.html
index.html
results.html
tests.py
urls.py
views.py
templates/
admin/
base_site.html
3 安装必须环境
pip install setuptools
4 打包你的应用
首先,在你的 Django 项目目录外创建一个名为 django-polls 的文件夹,将 polls 目录移入 django-polls 目录。
为你的包起名时,最好加上
django-
前缀避免冲突,这样也有助于他人在寻找 Django 应用时判断你的应用是否适用。避免使用任何与 Django contrib packages 相同的标签名,比如 auth 、 admin 、 messages 等。
创建 django-polls/README.rst
。
=====
Polls
=====
Polls is a Django app to conduct Web-based polls. For each question,
visitors can choose between a fixed number of answers.
Detailed documentation is in the "docs" directory.
Quick start
-----------
1. Add "polls" to your INSTALLED_APPS setting like this::
INSTALLED_APPS = [
...
'polls',
]
2. Include the polls URLconf in your project urls.py like this::
path('polls/', include('polls.urls')),
3. Run ``python manage.py migrate`` to create the polls models.
4. Start the development server and visit http://127.0.0.1:8000/admin/
to create a poll (you'll need the Admin app enabled).
5. Visit http://127.0.0.1:8000/polls/ to participate in the poll.
选择一种许可证,如 BSD ,放在 django-polls/LICENSE
。
下一步,创建 setup.cfg 和 setup.py 用于说明构建和安装的细节。
# django-polls/setup.cfg
[metadata]
name = django-polls
version = 0.1
description = A Django app to conduct Web-based polls.
long_description = file: README.rst
url = https://www.example.com/
author = Your Name
author_email = yourname@example.com
license = BSD-3-Clause # Example license
classifiers =
Environment :: Web Environment
Framework :: Django
Framework :: Django :: X.Y # Replace "X.Y" as appropriate
Intended Audience :: Developers
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Topic :: Internet :: WWW/HTTP
Topic :: Internet :: WWW/HTTP :: Dynamic Content
[options]
include_package_data = true
packages = find:
python_requires = >=3.6
install_requires =
Django >= X.Y # Replace "X.Y" as appropriate
# django-polls/setup.py
from setuptools import setup
setup()
建议创建空目录 django-polls/docs
留给未来的文档。除非你打算提供在线文档 readthedocs.org 这样的网站。
创建 jango-polls/MANIFEST.in
来包含额外的文件。
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
recursive-include docs *
在 django-polls 目录内执行 python setup.py sdist
生成 dist/django-polls-0.1.tar.gz
。
5 使用你自己的包名
先把 polls 目录移出项目,使它无法工作,再安装 django-polls 修复它。
以下步骤将 django-polls 以用户库的形式安装。与安装整个系统的软件包相比,用户安装具有许多优点,例如可在没有管理员访问权的系统上使用,以及防止应用包影响系统服务和其他用户。
python -m pip install --user django-polls/dist/django-polls-0.1.tar.gz
python -m pip uninstall django-polls
6 发布你的应用
现在,你已经对 django-polls 完成了打包和测试,准备好向世界分享它!
7 通过虚拟环境安装 Python 包
以用户库的形式安装了投票应用,会影响你系统上的其他 Python 软件,而且不能运行此包的多个版本(或者其它用有相同包名的包)。最好的解决方法是使用 venv ,维护多个相互隔离的 Python 环境。