The project packaged into modules ideas from the flask tutorial document, which is not on the PyCon and Amin together a photo Well, you may not see more people's things. Interested can look at the installation of the project documentation can be part of the flask project packaged into a package so that it can then import the project and allow anywhere, or by
pip install youproject.whl
way of the project to install after installing dependencies.
Can first glance at the official packaging guidelines: Jump
1.setuptools Profile
setuptools
It isdistutils
(self-Baidu) enhanced version. It extended a lot of features, can better help developers create and distribute Python packages. Most users will use the Python setuptools more advanced modules.
2. Packet Format
Python library packaged formats include Wheel and Egg. Egg format was introduced in 2004 by the setuptools, while Wheel format is defined by the PEP427 in 2012. Egg install and use Wheel do not need to re-build and compile, it should complete the test and built prior to release. Now widespread use
Wheel
of more
3.setup.py file
from setuptools import find_packages, setup
setup(
name='flaskr',
version='1.0.0',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=[
'flask',
],
)
相关参数可以[查看](https://setuptools.readthedocs.io/en/latest/setuptools.html#metadata)
Common parameters explained:
parameter name | Explanation |
---|---|
name | Package Name |
version | Package Version |
author | Author program |
author_email | Author of the program e-mail address |
maintainer | Defenders |
maintainer_email | Defenders mail address |
url | The official website address of the program |
license | Program authorization information |
description | A brief description of the program |
long_description | A detailed description of the program |
platforms | Procedures applicable software platform list |
classifiers | Category list of programs belongs |
keywords | Keyword list of programs |
packages | Package directory need to be addressed (usually contain init file .py folder) |
py_modules | The Python package requires single file list |
download_url | Download the program |
cmdclass | Add a custom command |
package_data | Specified data file needs to contain encapsulated |
include_package_data | Automatically include all versioned (cvs / svn / git) encapsulated data files |
exclude_package_data | When include_package_data is True This option is used to exclude some files |
data_files | Needs to be packaged when the packed data files such as pictures, profiles, etc. |
ext_modules | Designated expansion module |
scripts | Specified executable script, the installation script will be installed to the system PATH. |
package_dir | Specify which files in a directory which is mapped to the source package |
requires | Specify other packages dependent |
provides | Specifies which modules may be provided to depend |
install_requires | When installation requires the installation dependencies |
entry_points | Dynamic discovery of services and plug-ins, speak in detail below |
setup_requires | Specifies to run the setup.py file itself depends package |
dependency_links | Specify dependencies of Download |
extras_require | Advanced / additional features need to rely on the current package bundle |
zip_safe | Not compressed, but the installation in the form of directory |
3.1. find_packages(where='',exclude=(),include=('*',))
The above setup.py
used find_packages()
, we can easily add the packages do not need to be added manually. The default search setup.py
under the same directory containing each __init__.py
directory as the package to be added.
The first parameter of the function is used to specify which directory search package, the parameters
exclude
used to specify which packages to exclude,include
specify the package to include.
3.2. include_package_data:
Set to True, the package automatically adds version control data file receiving when the file does not need to add controlled version, use
package_data
.
3.3manifest.in file (with the setup.py the same directory)
文件内容就是需要保护在分发包中的文件
include flaskr/schema.sql
graft flaskr/static
graft flaskr/templates
global-exclude *.pyc
复制所有
static
和templates
文件夹中的文件,schema.sql
文件,但是排除所有字节文件。
3.4. zip_safe
zip_safe
参数决定包是否作为一个zip压缩后的 egg 文件安装,还是作为一个以 .egg 结尾的目录安装。因为有些工具不支持 zip 压缩文件,而且压缩后的包也不方便调试,所以建议将其设为 False,即zip_safe=False
3.5. install_requires
如果你的包依赖于其他包,可以指定
install_requires
参数,为一个列表
install_requires=[
'requests>=1.0',
'flask>=1.0'
]
默认从pypi下载安装指定依赖包,也可以指定链接下载依赖
dependency_links = [
"http://packages.example.com/snapshots/foo-1.0.tar.gz",
"http://example2.com/p/bar-1.0.tar.gz",
]
4.本地测试安装
pip3 install -e .
在当前文件夹中寻找setup.py并在开发模式下安装,安装好后通过
pip list
即可查看本地安装的情况。换个文件夹开始导入你的包进行测试,是否可以正常使用。
5.构建发行文件
先安装好wheel
库
pip3 install wheel
安装好后,执行
bdist_wheel
构建发行文件
python3 setup.py bdist_wheel
会生成
dist
目录,下面有一个flaskr-1.0.0-py3-none-any.whl
类似的由项目名称、版本号和项目安装要求的标记组成。复制该文件到别的计算机,通过pip
命令安装该文件。
6.发布包(简略)
如果要将包发布到PyPI(Python Package Index)官方维护的第三方包仓库,需要先注册pypi的账号,然后创建
~/.pypirc
文件进行一些配置
[distutils]
index-servers = pypi
[pypi]
username:xxx
password:xxx
注册项目
python3 setup.py register
成功注册后,构建源码包发布
python3 setup.py sdist upload
最后的最后,别忘了
README.md
和LICENSE
,最好再加上requirements.txt
,可以参考文章开头官网的打包流程