Python 包介绍

每个包都需要一个描述包的 setup.py 文件。

考虑一个简单包的以下目录结构:

+-- package_name
|       |
|       +-- __init__.py
|       
+-- setup.py

__init__.py 仅包含线 def foo(): return 100

以下 setup.py 将定义包:

from setuptools import setup

setup(
    name='package_name',                    # package name
    version='0.1',                          # version
    description='Package Description',      # short description
    url='http://example.com',               # package URL
    install_requires=[],                    # list of packages this package depends
                                            # on.
    packages=['package_name'],              # List of module names that installing
                                            # this package will provide.
)

virtualenv 非常适合在不修改其他 Python 环境的情况下测试软件包安装:

$ virtualenv .virtualenv
...
$ source .virtualenv/bin/activate
$ python setup.py install
running install
...
Installed .../package_name-0.1-....egg
...
$ python
>>> import package_name
>>> package_name.foo() 
100