python的文件项目打包

打算自己做一个 python 项目, 然后推到 pypi 上去.

这次的目的记录写一个及其简单的过程,所以如果有具体问题,请看官方文档。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ tree
.
├── MANIFEST.in
├── README.md
├── myapp
   ├── 1.txt
   ├── __init__.py
   ├── __pycache__
      ├── __init__.cpython-36.pyc
      └── test.cpython-36.pyc
   ├── test.py
   └── txt
       └── 1.txt
├── myapp2
   └── test222.py
└── setup.py

4 directories, 10 files
  1. MANIFEST.in 用来记录除了 py 文件外,需要打包的文件
1
2
3
4
# 包含文件
include README.md
# 递归-包含
recursive-include myapp/txt *
  1. README.md 一个用来简单介绍的文档

  2. myappmyapp2 都是存放代码

  3. setup.py 是用来安装和打包的主要文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

setup(
    name='kentxxq',               # 项目名
    version='1.0.2',              # 版本号
    zip_safe=False,               # 因为部分工具不支持zip,推荐禁用
    packages=find_packages(),     # 当前目录下所有的包
    include_package_data=True,    # 启用清单文件MANIFEST.in
    install_requires=[    # 依赖列表
        "Scrapy>=1.4.0",
    ]

    # 上传到PyPI所需要的信息
    author="kentxxq",
    author_email="我的邮箱@qq.com",
    description="This is an Example Package",
    license="PSF",
    keywords="hello world example examples",
    url="https://a805429509.github.io/",
)
1
2
3
4
python setup.py sdist

# 构建物在这里
dist/kentxxq-1.0.2.tar.gz

直接拷贝 dist/kentxxq-1.0.2.tar.gz 到目标机器,进行解压。

1
python setup.py install
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$ tree kentxxq-1.0.2-py3.6.egg
kentxxq-1.0.2-py3.6.egg
├── EGG-INFO
│   ├── dependency_links.txt
│   ├── not-zip-safe
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
└── myapp
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   └── test.cpython-36.pyc
    ├── test.py
    └── txt
        └── 1.txt

4 directories, 11 files
  1. 没有 __init__.py 的 myapp2 是没有打包的。
  2. myapp 中 1.txt 没有进入包内,而 MANIFEST.in 中的记录的 recursive-include myapp/txt * 在包内。