Django 关于Packaging your app的基础总结

预准备:安装setuptools和pip工具

  • 1.在工程目录外,创建app的一个父目录,目录名你可以命名为:django-polls.
  • 2.将app目录移动到django-polls中。
  • 3.创建一个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文件,它提供了怎样编译和安装app的细节。创建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='[email protected]',
  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.默认情况下,只有Python的模块和包,包含在包里面。为了包含其他的文件,我们需要创建一个MANIFEST.in文件,为了包含 templates, README.rst 和LICENSE 文件, 创建一个
    django-polls/MANIFEST.in 文件,内容如下:
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
  • 7.可选的一步,但是强烈建议,app最好包含一些细节文档,创建一个空目录django-polls/docs,为以后使用,另外,在django-polls/MANIFEST.in文件中,新增一行:recursive-include docs *
  • 8.尝试使用命令python setup.py sdist来编译你的包,这将会创建一个称为dist的目录并会编译成新的包django-polls-0.1.tar.gz.

补充说明:

  • 1.安装我们app 包,使用pip命令:
    pip install --user django-polls/dist/django-polls-0.1.tar.gz
  • 2.卸载包,使用下面的命令
    pip uninstall django-polls

你可能感兴趣的:(Django 关于Packaging your app的基础总结)