1 可重用性
一个WEB应用的工作包含了诸多内容, 如设计, 构建, 测试和维护
如果能够避免做一些重复的事情, 那将省时省力
因此我们在完成一个项目之后, 可以将那些以后可以用到的内容做成一个python包, 以后遇到此类问题就可以直接使用该包
那么我们将有更好的工作效率, 对于Python包这样的内容, 就是可重用的
那么如果将那些可重用的内容分离出来, 就是我们要做的内容了
经过之前的内容, 现在我们的mysite的目录结构Wie
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
__init__.py
admin.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
可见polls应用几乎是个独立的了, 现在都可以直接复制给别的项目并且能够立即使用(模板等文件都在应用内, 只需将配置好URLs)
但是直接复制不够专业, 我们可以将polls打包
我们推荐使用setuptools来打包
2 打包程序
1) 在项目外创建一个文件夹django-polls
关于命名: 需要先检查PyPI这样的资源, 避免同名
同时用分隔符分割这样也便于识别内容内部
应用程标签(Application labels)也需要注意不要和INSTALLED_APPS内使用相同的标签
2) 将polls移动到django-polls文件夹
3) 创建 django-polls/README.rst
django-polls/README.rst
=====
Polls
=====
Polls is a simple 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::
url(r'^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.
4) 创建 django-polls/LICENSE
没有公开许可发布的代码是无用的
5) 创建一个setup.py文件
django-polls/setup.py
import os
from setuptools import find_packages, setup
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
README = readme.read()
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup(
name='django-polls',
version='0.1',
packages=find_packages(),
include_package_data=True,
license='BSD License', # example license
description='A simple Django app to conduct Web-based polls.',
long_description=README,
url='https://www.example.com/',
author='Your Name',
author_email='yourname@example.com',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: X.Y', # replace "X.Y" as appropriate
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', # example license
'Operating System :: OS Independent',
'Programming Language :: Python',
# Replace these appropriately if you are stuck on Python 2.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
6) 创建MANIFEST.in文件
django-polls/MANIFEST.in
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
7) 建议在应用程序中包含详细文档, 可以创建目录django-polls/docs来包含文档
需要在 django-polls/MANIFEST.in 添加一句话
recursive-include docs *
8) 创建包
python setup.py sdist
执行完毕之后会产生一个dist目录和一个新包 django-polls-0.1.tar.gz
3 使用包
现在清除掉项目中的polls, 安装我们打包好的看是否能正常运行
1) 安装包
pip install --user django-polls/dist/django-polls-0.1.tar.gz
2) 运行项目测试
3) 卸载
pip uninstall django-polls
4 发布包
一般可以将包发布到PyPI中
5 virtualenv
安装包多了可能出现冲突问题, 比如同一个包只能装一个版本
而你的项目一个需要这个版本, 一个需要另一个版本
此时可以使用virtualenv, 此工具允许维护使用多个独立的Python环境, 每个环境都有自己的库和包命名空间的副本