<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] ## 一、構建工具setup.py的應用場景 1. 將python程序打包 2. 安裝程序到用戶端 3. 將方法對應到命令(應用的入口,例如scrapy) > 在安裝python的相關模塊和庫時,我們一般使用“pip install 模塊名”或者“python setup.py install”,前者是在線安裝,會安裝該包的相關依賴包;后者是下載源碼包然后在本地安裝,不會安裝該包的相關依賴包。所以在安裝普通的python包時,利用pip工具相當簡單。但是在如下場景下,使用python setup.py install會更適合需求: **在編寫相關系統時,python 如何實現連同依賴包一起打包發布?** > 1. 假如我在本機開發一個程序,需要用到python的redis、mysql模塊以及自己編寫的redis_run.py模塊。我怎么實現在服務器上去發布該系統,如何實現依賴模塊和自己編寫的模塊redis_run.py一起打包,實現一鍵安裝呢?同時將自己編寫的redis_run.py模塊以exe文件格式安裝到python的全局執行路徑C:\Python27\Scripts下呢? > 2. 在這種應用場景下,pip工具似乎派不上了用場,只能使用python的構建工具setup.py了,使用此構建工具可以實現上述應用場景需求,只需在 setup.py 文件中寫明依賴的庫和版本,然后到目標機器上使用python setup.py install安裝。 ## 二、setup.py介紹 ~~~ 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各參數介紹:** ~~~ --name 包名稱 --version (-V) 包版本 --author 程序的作者 --author_email 程序的作者的郵箱地址 --maintainer 維護者 --maintainer_email 維護者的郵箱地址 --url 程序的官網地址 --license 程序的授權信息 --description 程序的簡單描述 --long_description 程序的詳細描述 --platforms 程序適用的軟件平臺列表 --classifiers 程序的所屬分類列表 --keywords 程序的關鍵字列表 --packages 需要處理的包目錄(包含__init__.py的文件夾) --py_modules 需要打包的python文件列表 --download_url 程序的下載地址 --cmdclass --data_files 打包時需要打包的數據文件,如圖片,配置文件等 --scripts 安裝時需要執行的腳步列表 --package_dir 告訴setuptools哪些目錄下的文件被映射到哪個源碼包。一個例子:package_dir = {'': 'lib'},表示“root package”中的模塊都在lib 目錄中。 --requires 定義依賴哪些模塊 --provides定義可以為哪些模塊提供依賴 --find_packages() 對于簡單工程來說,手動增加packages參數很容易,剛剛我們用到了這個函數,它默認在和setup.py同一目錄下搜索各個含有 __init__.py的包。 --install_requires - 聲明明依賴包,安裝包時pip會自動安裝 ~~~ > 其實我們可以將包統一放在一個src目錄中,另外,這個包內可能還有aaa.txt文件和data數據文件夾。另外,也可以排除一些特定的包 `find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])` > --install_requires = ["requests"] 需要安裝的依賴包,pip install <包名>的時候自動下載安裝 > --entry_points 動態發現服務和插件,下面詳細講 > 下列entry_points中: console_scripts 指明了命令行工具的名稱;在“redis_run = RedisRun.redis_run:main”中,等號前面指明了工具包的名稱,等號后面的內容指明了程序的入口地址。 ~~~ entry_points={'console_scripts': [ 'redis_run = RedisRun.redis_run:main', ]} ~~~ > 這里可以有多條記錄,這樣一個項目就可以制作多個命令行工具了,比如: ~~~ setup( entry_points = { 'console_scripts': [ 'foo = demo:test', 'bar = demo:test', ]} ) ~~~ ## 三、setup.py的項目示例代碼 ~~~ #!/usr/bin/env python # coding=utf-8 from setuptools import setup ''' 把redis服務打包成C:\Python27\Scripts下的exe文件 ''' setup( name="RedisRun", #pypi中的名稱,pip或者easy_install安裝時使用的名稱,或生成egg文件的名稱 version="1.0", author="Andreas Schroeder", author_email="andreas@drqueue.org", description=("This is a service of redis subscripe"), license="GPLv3", keywords="redis subscripe", url="https://ssl.xxx.org/redmine/projects/RedisRun", packages=['RedisRun'], # 需要打包的目錄列表 # 需要安裝的依賴 install_requires=[ 'redis>=2.10.5', 'setuptools>=16.0', ], # 添加這個選項,在windows下Python目錄的scripts下生成exe文件 # 注意:模塊與函數之間是冒號: entry_points={'console_scripts': [ 'redis_run = RedisRun.redis_run:main', ]}, # long_description=read('README.md'), classifiers=[ # 程序的所屬分類列表 "Development Status :: 3 - Alpha", "Topic :: Utilities", "License :: OSI Approved :: GNU General Public License (GPL)", ], # 此項需要,否則卸載時報windows error zip_safe=False ) ~~~ ## 四、修改后的項目代碼(此時RedisRun模塊是DrQueue模塊的子模塊,這是因為要導入某些公用的模塊) 復制代碼 ~~~ #!/usr/bin/env python # coding=utf-8 from setuptools import setup ''' 把redis服務打包成C:\Python27\Scripts下的exe文件 ''' setup( name="RedisRun", #pypi中的名稱,pip或者easy_install安裝時使用的名稱 version="1.0", author="Andreas Schroeder", author_email="andreas@drqueue.org", description=("This is a service of redis subscripe"), license="GPLv3", keywords="redis subscripe", url="https://ssl.xxx.org/redmine/projects/RedisRun", packages=['DrQueue'], # 需要打包的目錄列表 # 需要安裝的依賴 install_requires=[ 'redis>=2.10.5', ], # 添加這個選項,在windows下Python目錄的scripts下生成exe文件 # 注意:模塊與函數之間是冒號: entry_points={'console_scripts': [ 'redis_run = DrQueue.RedisRun.redis_run:main', ]}, # long_description=read('README.md'), classifiers=[ # 程序的所屬分類列表 "Development Status :: 3 - Alpha", "Topic :: Utilities", "License :: OSI Approved :: GNU General Public License (GPL)", ], # 此項需要,否則卸載時報windows error zip_safe=False ) ~~~ 參考博客: https://www.cnblogs.com/maociping/p/6633948.html ## 五、實例 ### 5.1 創建可發布的工程 工程結構如下: packagetest目錄下,有demo包,setup.py文件 1. 創建項目目錄 ~~~ mkdir packagetest cd packagetest ~~~ 2. 創建demo目錄 ~~~ mkdir demo cd demo ~~~ 3. 將demo目錄變成python包 ~~~ vim __init__.py print("引入demo包!") ~~~ 4. 創建模塊 ~~~ vim test.py import sys def say(word=None): if word == None: word = sys.argv[1] print("command in {0} says {1}".format(sys.argv[0],word)) ~~~ 5. packagetest目錄下創建setup.py文件 具有可執行命令的 ~~~ from setuptools import setup, find_packages setup( name = "sayhello", version = "0.1", packages = find_packages(), entry_points = { 'console_scripts': [ 'say = demo.test:say', ], } ) ~~~ 此時,工程結構如下: ~~~ root@zabbix01:~/workspace# tree packagetest packagetest ├── demo │?? ├── __init__.py │?? └── test.py └── setup.py ~~~ ### 5.2 打包發布 1. 打包 ~~~ python3 setup.py bdist_egg ~~~ 結構: ~~~ root@zabbix01:~/workspace# tree packagetest/ packagetest/ ├── build │?? ├── bdist.linux-x86_64 │?? └── lib │?? └── demo │?? ├── __init__.py │?? └── test.py ├── demo │?? ├── __init__.py │?? └── test.py ├── dist │?? └── sayhello-0.1-py3.4.egg # egg包 ├── sayhello.egg-info │?? ├── dependency_links.txt │?? ├── entry_points.txt │?? ├── PKG-INFO │?? ├── SOURCES.txt │?? └── top_level.txt └── setup.py ~~~ 2. 安裝到本地 ~~~ python3 setup.py install ~~~ ![](https://box.kancloud.cn/e78c243d35f6923e9fe99f726017f73f_733x730.png) 3. 測試命令 ![](https://box.kancloud.cn/330c136da263fd45a44054d6e6112d44_1909x453.png) 4. 直接執行命令 ~~~ root@zabbix01:~/workspace/packagetest# say 'come on' 引入demo包! command in /usr/local/bin/say says come on ~~~ ### 5.3 加入依賴支持 ~~~ install_requires=[ 'scrapy-redis>=0.6.8', ], ~~~ ~~~ from setuptools import setup, find_packages setup( name = "sayhello", version = "0.1", packages = find_packages(), install_requires=[ 'scrapy-redis>=0.6.8', # 依賴 ], entry_points = { 'console_scripts': [ 'say = demo.test:say', ], } ) ~~~ 上邊加入了scrapy-redis依賴,在執行 python3 setup.py install 后會自動下載scrapy-redis安裝到我們的電腦上 ![](https://box.kancloud.cn/3f6d9686d89d97ad6212940935910756_1125x317.png) ![](https://box.kancloud.cn/b031dbdb21d31a288981808c9ad2b346_737x46.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看