5
5
----------
6
6
问题
7
7
----------
8
- You’ve written a useful library, and you want to be able to give it away to others.
8
+ 你已经编写了一个有用的库,想将它分享给其他人。
9
9
10
10
----------
11
11
解决方案
12
12
----------
13
- If you’re going to start giving code away, the first thing to do is to give it a unique name
14
- and clean up its directory structure. For example, a typical library package might look
15
- something like this:
13
+ 如果你想分发你的代码,第一件事就是给它一个唯一的名字,并且清理它的目录结构。
14
+ 例如,一个典型的函数库包会类似下面这样:
16
15
17
16
.. code-block :: python
18
17
@@ -32,8 +31,7 @@ something like this:
32
31
helloworld.py
33
32
...
34
33
35
- To make the package something that you can distribute, first write a setup.py file that
36
- looks like this:
34
+ 要让你的包可以发布出去,首先你要编写一个 ``setup.py `` ,类似下面这样:
37
35
38
36
.. code-block :: python
39
37
@@ -48,8 +46,7 @@ looks like this:
48
46
packages = [' projectname' , ' projectname.utils' ],
49
47
)
50
48
51
- Next, make a file MANIFEST.in that lists various nonsource files that you want to include
52
- in your package:
49
+ 下一步,就是创建一个 ``MANIFEST.in `` 文件,列出所有在你的包中需要包含进来的非源码文件:
53
50
54
51
.. code-block :: python
55
52
@@ -58,40 +55,32 @@ in your package:
58
55
recursive- include examples *
59
56
recursive- include Doc *
60
57
61
- Make sure the setup.py and MANIFEST.in files appear in the top-level directory of your
62
- package. Once you have done this, you should be able to make a source distribution by
63
- typing a command such as this:
58
+ 确保 ``setup.py `` 和 ``MANIFEST.in `` 文件放在你的包的最顶级目录中。
59
+ 一旦你已经做了这些,你就可以像下面这样执行命令来创建一个源码分发包了:
64
60
65
61
.. code-block :: python
66
62
67
63
% bash python3 setup.py sdist
68
64
69
- This will create a file such as projectname-1.0.zip or projectname-1.0.tar.gz, depending
70
- on the platform. If it all works, this file is suitable for giving to others or uploading to
71
- the `Python Package Index <http://pypi.python.org/ >`_.
65
+ 它会创建一个文件比如" projectname-1.0.zip" 或 “ projectname-1.0.tar.gz”,
66
+ 具体依赖于你的系统平台。如果一切正常,
67
+ 这个文件就可以发送给别人使用或者上传至 `Python Package Index <http://pypi.python.org/ >`_.
72
68
73
69
----------
74
70
讨论
75
71
----------
76
- For pure Python code, writing a plain setup.py file is usually straightforward. One potential
77
- gotcha is that you have to manually list every subdirectory that makes up the
78
- packages source code. A common mistake is to only list the top-level directory of a
79
- package and to forget to include package subcomponents. This is why the specification
80
- for packages in setup.py includes the list packages=['projectname', 'project
81
- name.utils'].
82
-
83
-
84
- As most Python programmers know, there are many third-party packaging options,
85
- including setuptools, distribute, and so forth. Some of these are replacements for the
86
- distutils library found in the standard library. Be aware that if you rely on these
87
- packages, users may not be able to install your software unless they also install the
88
- required package manager first. Because of this, you can almost never go wrong by
89
- keeping things as simple as possible. At a bare minimum, make sure your code can be
90
- installed using a standard Python 3 installation. Additional features can be supported
91
- as an option if additional packages are available.
92
-
93
-
94
- Packaging and distribution of code involving C extensions can get considerably more
95
- complicated. Chapter 15 on C extensions has a few details on this. In particular, see
96
- Recipe 15.2.
97
-
72
+ 对于纯Python代码,编写一个普通的 ``setup.py `` 文件通常很简单。
73
+ 一个可能的问题是你必须手动列出所有构成包源码的子目录。
74
+ 一个常见错误就是仅仅只列出一个包的最顶级目录,忘记了包含包的子组件。
75
+ 这也是为什么在 ``setup.py `` 中对于包的说明包含了列表
76
+ ``packages=['projectname', 'projectname.utils'] ``
77
+
78
+ 大部分Python程序员都知道,有很多第三方包管理器供选择,包括setuptools、distribute等等。
79
+ 有些是为了替代标准库中的distutils。注意如果你依赖这些包,
80
+ 用户可能不能安装你的软件,除非他们已经事先安装过所需要的包管理器。
81
+ 正因如此,你更应该时刻记住越简单越好的道理。
82
+ 最好让你的代码使用标准的Python 3安装。
83
+ 如果其他包也需要的话,可以通过一个可选项来支持。
84
+
85
+ 对于涉及到C扩展的代码打包与分发就更复杂点了。
86
+ 第15章对关于C扩展的这方面知识有一些详细讲解,特别是在15.2小节中。
0 commit comments