Python學習筆記-打包發佈Python模塊或程序,安裝包

Python模塊、擴展和應用程序可以按以下幾種形式進行打包和發佈:

 

python setup.py獲取幫助的方式

    python setup.py --help

    python setup.py --help-commands 所有可以使用的命令,如build,install

    python setup.py COMMAND --help 獲取特定命令的幫助

    python setup.py COMMAND --help-formats 獲取特定命令支持使用的格式

 

打包

    1.壓縮文件(使用distutils)

        Windows的Zip文件和類Unix平臺的.tar.gz文件

    2.自動解包或自動安裝可執行文件

        Windows中的.exe文件

    3.自包含的,不要求安裝的預備運行科執行程序

        Windows的.exe文件、Unix上帶有一個小的腳本前綴的ZIP壓縮文件、Mac上的.app文件等

    4.平臺相關的安裝程序

        Windows上的.msi文件、Linux上常見的.rpm、src.rpm和.dep文件等

    5.Python eggs

        較流行的第三方擴展

 

 

發佈

    “發佈”是指一個文件集合,這些文件聯合在一起可使用distutils構建、打包和發佈模塊

    創建好的發佈可以用於安裝,可上傳到ftp,上傳到各大網絡讓人下載,也可上傳到PyPI與他人共享

 

創建發佈

    將各代碼文件組織到模塊容器中

    準備一個README或README.txt文件

    而後在容器中創建setup.py文件

 

setup.py中setup()中可用參數:

    platforms: 平臺列表

    license: 許可證

    py_modules: 各模塊名稱組成的列表,此些模塊可能位於包的根目錄下(modname),也可能位於某子包目錄中(subpkg1.modname)

    packages: 各子包名稱的列表

    ......

    

setup.py關鍵字大體分爲兩類:

1.元數據信息

2.包中的內容列表

 

python setup.py sdist打包(會指定默認格式tar.gz)

    可以爲sdist指定打包格式:

            zip: zip file

            gztar: tar.gz file

            bztar: tar.bz2 vil2

            ztar: tar.Z file

            tar: tar file

指定格式sdist打包的方式:

[root@kurol pkg1]# python36 setup.py sdist --help-formats
List of available source distribution formats:
  --formats=bztar  bzip2'ed tar-file
  --formats=gztar  gzip'ed tar-file
  --formats=tar    uncompressed tar file
  --formats=xztar  xz'ed tar-file
  --formats=zip    ZIP file
  --formats=ztar   compressed tar file


python setup.py bdist打包(二進制發行版)(會指定默認格式tar.gz)

        可以爲bdist指定的格式:

                gztar: tar.gz file

                ztar: tar.Z file

                zip: zip file

                rpm: RPM Package

                pkgtool: Solaris pkgtool

                wininst: Windows上自解壓的zip格式包

                msi:Microsoft Installer

指定格式sdist打包的方式:

[root@kurol pkg1]# python36 setup.py bdist --help-formats 
List of available distribution formats:
  --formats=rpm      RPM distribution
  --formats=gztar    gzip'ed tar file
  --formats=bztar    bzip2'ed tar file
  --formats=xztar    xz'ed tar file
  --formats=ztar     compressed tar file
  --formats=tar      tar file
  --formats=wininst  Windows executable installer
  --formats=zip      ZIP file
  --formats=msi      Microsoft Installer


打包 ,例:

[root@kurol python361]# cd pkg1/
[root@kurol pkg1]# ls
__init__.py __pycache__ mymmm.py
[root@kurol pkg1]# touch REMAIN.txt
[root@kurol pkg1]# touch setup.py
[root@kurol pkg1]# vim setup.py
 #!/usr/bin/python36
 #
 from distutils.core import setup
 
 setup(
         name = 'pkg1',
         version = '1.0',
         author = 'MageEdu',
         author_email = '[email protected]',
         py_modules = ['mymmm'],
         url = 'http://www.mykurol.com',
         download_url = 'http://www.mykurol.com/pymodules/download/',
         description = 'test module',
 )
[root@kurol pkg1]# python36 setup.py sdist
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

file yammm.py (for module yammm) not found
writing manifest file 'MANIFEST'
creating pkg1-1.0
making hard links in pkg1-1.0...
hard linking setup.py -> pkg1-1.0
creating dist
Creating tar archive
removing 'pkg1-1.0' (and everything under it)

[root@kurol pkg1]# ls    ##自動生成了MANIFEST文件
MANIFEST  REMAIN.txt  __init__.py  __pycache__  dist  mymmm.py  setup.py
[root@kurol pkg1]# cd dist/
[root@kurol dist]# ls
<strong>pkg1-1.0.tar.gz</strong>


使用bdist

[root@kurol dist]# cd -
/opt/python361/pkg1
[root@kurol pkg1]# python36 setup.py bdist 
running bdist
running bdist_dumb
running build
running build_py
file yammm.py (for module yammm) not found
file yammm.py (for module yammm) not found
installing to build/bdist.linux-x86_64/dumb
running install
running install_lib
warning: install_lib: 'build/lib' does not exist -- no Python modules to install

running install_egg_info
Creating build/bdist.linux-x86_64/dumb/usr/local/python361/lib/python3.6/site-packages/
Writing build/bdist.linux-x86_64/dumb/usr/local/python361/lib/python3.6/site-packages/pkg1-1.0-py3.6.egg-info
Creating tar archive
removing 'build/bdist.linux-x86_64/dumb' (and everything under it)
[root@kurol pkg1]# cd dist/
[root@kurol dist]# ls
<strong>pkg1-1.0.linux-x86_64.tar.gz</strong>  pkg1-1.0.tar.gz


指定爲zip格式:

[root@kurol pkg1]# python36 setup.py bdist --formats=zip
running bdist
running bdist_dumb
running build
running build_py
file yammm.py (for module yammm) not found
file yammm.py (for module yammm) not found
installing to build/bdist.linux-x86_64/dumb
running install
running install_lib
warning: install_lib: 'build/lib' does not exist -- no Python modules to install

running install_egg_info
Creating build/bdist.linux-x86_64/dumb/usr/local/python361/lib/python3.6/site-packages/
Writing build/bdist.linux-x86_64/dumb/usr/local/python361/lib/python3.6/site-packages/pkg1-1.0-py3.6.egg-info
creating '/opt/python361/pkg1/dist/pkg1-1.0.linux-x86_64.zip' and adding '.' to it
adding 'usr/local/python361/lib/python3.6/site-packages/pkg1-1.0-py3.6.egg-info'
removing 'build/bdist.linux-x86_64/dumb' (and everything under it)
[root@kurol pkg1]# cd dist/
[root@kurol dist]# ls
pkg1-1.0.linux-x86_64.tar.gz  <strong>pkg1-1.0.linux-x86_64.zip</strong>  pkg1-1.0.tar.gz


安裝包

    python setup.py install

    build and install:

    python setup.py build:

        --build-base /path/to/build_dir      ##指定build路徑,build在其他路徑進行,保證源碼的整潔程度

            lib,lib.platform

第三方模塊的大多數默認路徑通常爲:site-packages(站點包)

如 /usr/local/python361/lib/python3.6/site-packages

 

第三方模塊自定義安裝路徑:

    --user 如果用戶沒有寫權限,安裝到指定用戶的目錄下(只有普通權限,沒有辦法寫到公共目錄中)

    --prefix 指定python庫文件的安裝路徑(對公共目錄有寫權限才能操作)

    --exec-prefix 跟python無關的,有其他語言所實現的跟平臺有關的,已經編譯好的相關文件的安裝路徑(對公共目錄有寫權限才能操作)

 

深度定製 (期望對python模塊安裝做深度定製)(路徑都是自己定義):

    --install-purelib /path/to/python_lib    (純Python庫文件)

    --install-platlib /paht/to/plat_lib    (擴展模塊,其他語言所實現的)

       --install-lib /path/to/lib ( 也可不加區分)

        如果同時出現前面3種,第三種lib覆蓋前面2種,lib優先級最高。

    --install-scripts /path/to/bin(可執行文件的安裝路徑)

    --install-data (指定數據文件安裝路徑)

    --install-headers(指定C代碼的頭文件安裝路徑)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章