python的構建工具setup.py

1、構建工具setup.py的應用場景html

      在安裝python的相關模塊和庫時,咱們通常使用「pip install  模塊名」或者「python setup.py install」,前者是在線安裝,會安裝該包的相關依賴包;後者是下載源碼包而後在本地安裝,不會安裝該包的相關依賴包。因此在安裝普通的python包時,利用pip工具至關簡單。可是在以下場景下,使用python setup.py install會更適合需求:python

在編寫相關係統時,python 如何實現連同依賴包一塊兒打包發佈?mysql

      假如我在本機開發一個程序,須要用到python的redis、mysql模塊以及本身編寫的redis_run.py模塊。我怎麼實如今服務器上去發佈該系統,如何實現依賴模塊和本身編寫的模塊redis_run.py一塊兒打包,實現一鍵安裝呢?同時將本身編寫的redis_run.py模塊以exe文件格式安裝到python的全局執行路徑C:\Python27\Scripts下呢?redis

在這種應用場景下,pip工具彷佛派不上了用場,只能使用python的構建工具setup.py了,使用此構建工具能夠實現上述應用場景需求,只需在 setup.py 文件中寫明依賴的庫和版本,而後到目標機器上使用python setup.py install安裝。sql

from setuptools import setup, find_packages  
  
setup(  
    name = "test",  
    version = "1.0",  
    keywords = ("test", "xxx"),  
    description = "eds sdk",  
    long_description = "eds sdk for python",  
    license = "MIT Licence",  
  
    url = "http://test.com",  
    author = "test",  
    author_email = "test@gmail.com",  
  
    packages = find_packages(),  
    include_package_data = True,  
    platforms = "any",  
    install_requires = [],  
  
    scripts = [],  
    entry_points = {  
        'console_scripts': [  
            'test = test.help:main'  
        ]  
    }  
)

  setup.py各參數介紹:windows

 1 from setuptools import setup, find_packages  
 2   
 3 setup(  
 4     name = "test",  
 5     version = "1.0",  
 6     keywords = ("test", "xxx"),  
 7     description = "eds sdk",  
 8     long_description = "eds sdk for python",  
 9     license = "MIT Licence",  
10   
11     url = "http://test.com",  
12     author = "test",  
13     author_email = "test@gmail.com",  
14   
15     packages = find_packages(),  
16     include_package_data = True,  
17     platforms = "any",  
18     install_requires = [],  
19   
20     scripts = [],  
21     entry_points = {  
22         'console_scripts': [  
23             'test = test.help:main'  
24         ]  
25     }  
26 )
27 按 Ctrl+C 複製代碼
28     setup.py各參數介紹:
29 
30 --name 包名稱
31 --version (-V) 包版本
32 --author 程序的做者
33 --author_email 程序的做者的郵箱地址
34 --maintainer 維護者
35 --maintainer_email 維護者的郵箱地址
36 --url 程序的官網地址
37 --license 程序的受權信息
38 --description 程序的簡單描述
39 --long_description 程序的詳細描述
40 --platforms 程序適用的軟件平臺列表
41 --classifiers 程序的所屬分類列表
42 --keywords 程序的關鍵字列表
43 --packages 須要處理的包目錄(包含__init__.py的文件夾) 
44 --py_modules 須要打包的python文件列表
45 --download_url 程序的下載地址
46 --cmdclass 
47 --data_files 打包時須要打包的數據文件,如圖片,配置文件等
48 --scripts 安裝時須要執行的腳步列表
49 --package_dir 告訴setuptools哪些目錄下的文件被映射到哪一個源碼包。一個例子:package_dir = {'': 'lib'},表示「root package」中的模塊都在lib 目錄中。
50 --requires 定義依賴哪些模塊 
51 --provides定義能夠爲哪些模塊提供依賴 
52 --find_packages() 對於簡單工程來講,手動增長packages參數很容易,剛剛咱們用到了這個函數,它默認在和setup.py同一目錄下搜索各個含有 __init__.py的包。
53 
54                           其實咱們能夠將包統一放在一個src目錄中,另外,這個包內可能還有aaa.txt文件和data數據文件夾。另外,也能夠排除一些特定的包
55 
56                           find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])
57 
58 --install_requires = ["requests"] 須要安裝的依賴包
59 --entry_points 動態發現服務和插件,下面詳細講

下列entry_points中: console_scripts 指明瞭命令行工具的名稱;在「redis_run = RedisRun.redis_run:main」中,等號前面指明瞭工具包的名稱,等號後面的內容指明瞭程序的入口地址。服務器

entry_points={'console_scripts': [
        'redis_run = RedisRun.redis_run:main',
]}

 這裏能夠有多條記錄,這樣一個項目就能夠製做多個命令行工具了,好比:ide

1 setup(
2     entry_points = {
3         'console_scripts': [
4             'foo = demo:test',
5             'bar = demo:test',
6         ]}
7

3、setup.py的項目示例代碼函數

 1 #!/usr/bin/env python
 2 # coding=utf-8
 3 
 4 from setuptools import setup
 5 
 6 '''
 7 把redis服務打包成C:\Python27\Scripts下的exe文件
 8 '''
 9 
10 setup(
11     name="RedisRun",  #pypi中的名稱,pip或者easy_install安裝時使用的名稱,或生成egg文件的名稱
12     version="1.0",
13     author="Andreas Schroeder",
14     author_email="andreas@drqueue.org",
15     description=("This is a service of redis subscripe"),
16     license="GPLv3",
17     keywords="redis subscripe",
18     url="https://ssl.xxx.org/redmine/projects/RedisRun",
19     packages=['RedisRun'],  # 須要打包的目錄列表
20 
21     # 須要安裝的依賴
22     install_requires=[
23         'redis>=2.10.5',
24         'setuptools>=16.0',
25     ],
26 
27     # 添加這個選項,在windows下Python目錄的scripts下生成exe文件
28     # 注意:模塊與函數之間是冒號:
29     entry_points={'console_scripts': [
30         'redis_run = RedisRun.redis_run:main',
31     ]},
32 
33     # long_description=read('README.md'),
34     classifiers=[  # 程序的所屬分類列表
35         "Development Status :: 3 - Alpha",
36         "Topic :: Utilities",
37         "License :: OSI Approved :: GNU General Public License (GPL)",
38     ],
39     # 此項須要,不然卸載時報windows error
40     zip_safe=False
41 )

4、修改後的項目代碼(此時RedisRun模塊是DrQueue模塊的子模塊,這是由於要導入某些公用的模塊)工具

 1 #!/usr/bin/env python
 2 # coding=utf-8
 3 
 4 from setuptools import setup
 5 
 6 '''
 7 把redis服務打包成C:\Python27\Scripts下的exe文件
 8 '''
 9 
10 setup(
11     name="RedisRun",  #pypi中的名稱,pip或者easy_install安裝時使用的名稱
12     version="1.0",
13     author="Andreas Schroeder",
14     author_email="andreas@drqueue.org",
15     description=("This is a service of redis subscripe"),
16     license="GPLv3",
17     keywords="redis subscripe",
18     url="https://ssl.xxx.org/redmine/projects/RedisRun",
19     packages=['DrQueue'],  # 須要打包的目錄列表
20 
21     # 須要安裝的依賴
22     install_requires=[
23         'redis>=2.10.5',
24     ],
25 
26     # 添加這個選項,在windows下Python目錄的scripts下生成exe文件
27     # 注意:模塊與函數之間是冒號:
28     entry_points={'console_scripts': [
29         'redis_run = DrQueue.RedisRun.redis_run:main',
30     ]},
31 
32     # long_description=read('README.md'),
33     classifiers=[  # 程序的所屬分類列表
34         "Development Status :: 3 - Alpha",
35         "Topic :: Utilities",
36         "License :: OSI Approved :: GNU General Public License (GPL)",
37     ],
38     # 此項須要,不然卸載時報windows error
39     zip_safe=False
40 )

此時項目的目錄結構爲:

參考博客:

  http://www.javashuo.com/article/p-fuyrkuvo-c.html

相關文章
相關標籤/搜索