Python - 使用Setuptools進行程序打包


1- Setuptools簡介

通過Setuptools可以更方便的創建和發布Python包,特別是那些對其它包具有依賴性的狀況;

Python打包用戶指南( Python Packaging User Guide

 

1.1 安裝Setuptools

  • 目前官網新版本Python的安裝包已自帶pip(封裝了setuptools),並不需要手工安裝;
  • 也可通過pip安裝新版本;
$ pip3 show setuptools
Name: setuptools
Version: 40.0.0
Summary: Easily download, build, install, upgrade, and uninstall Python packages
Home-page: https://github.com/pypa/setuptools
Author: Python Packaging Authority
Author-email: distutils-sig@python.org
License: UNKNOWN
Location: c:\python36\lib\site-packages
Requires:
Required-by: PyInstaller, pipenv, kiwisolver, ipython, html5lib

 

1.2 簡單的安裝腳本

setup.py
# coding=utf-8
from setuptools import setup

setup(name='Hello',
      version='1.0',
      description='A simple example',
      author='Anliven',
      author_email='anliven@yeah.net',
      url='www.cnblogs.com/anliven',
      py_modules=['hello'])

setup函數參數詳解
創建setup.py文件后,可通過執行“python setup.py --help”命令獲得幫助信息
setup函數各參數詳解:
>>python setup.py --help
  --name              包名稱
  --version (-V)      包版本
  --author            程序的作者
  --author_email      程序的作者的郵箱地址
  --maintainer        維護者
  --maintainer_email  維護者的郵箱地址
  --url               程序的官網地址
  --license           程序的授權信息
  --description       程序的簡單描述
  --long_description  程序的詳細描述
  --platforms         程序適用的軟件平台列表
  --classifiers       程序的所屬分類列表
  --keywords          程序的關鍵字列表
  --packages  需要打包的目錄列表
  --py_modules  需要打包的python文件列表
  --download_url  程序的下載地址
  --cmdclass  
  --data_files  打包時需要打包的數據文件,如圖片,配置文件等
  --scripts  安裝時需要執行的腳步列表

1.3 獲得幫助信息

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

“--help”

$ py -3 setup.py --help

“--help-commands”

$ py -3 setup.py --help-commands

命令sdist(Source distribution)

$ py -3 setup.py sdist --help

2- 構建模塊(執行build命令

  • 確保所在目錄有模塊文件hello.py;
  • 執行后,將創建build目錄及子目錄lib,同時將模塊文件hello.py復制到子目錄中;
  • 目錄build:setuptools在此目錄組裝包以及編譯擴展庫等;
$ ls -l
total 2
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py


$ cat -n hello.py
     1  # coding=utf-8
     2  print("This is a test!")


$ cat -n setup.py
     1  # coding=utf-8
     2  from setuptools import setup
     3
     4  setup(name='Hello',
     5        version='1.0',
     6        description='A simple example',
     7        author='Anliven',
     8        author_email='anliven@yeah.net',
     9        url='www.cnblogs.com/anliven',
    10        py_modules=['hello'])


$ py -3 setup.py build
running build
running build_py
creating build
creating build\lib
copying hello.py -> build\lib


$ ls -lR
.:
total 2
drwxr-xr-x 1 guowli 1049089   0 Oct 11 17:00 build/
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py

./build:
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 11 17:00 lib/

./build/lib:
total 1
-rw-r--r-- 1 guowli 1049089 39 Oct 11 16:57 hello.py


$ cat -n build/lib/hello.py
     1  # coding=utf-8
     2  print("This is a test!")

$

 

3- 安裝模塊(執行install命令)

  • 執行insall命令時會根據需要自動運行build命令,不需要手工執行build命令;
  • 安裝過程中,Setuptools將創建一個.egg文件(獨立的的Python包);
  • 可用pip命令來確認安裝結果;
特別注意:建議使用“--record log.txt”參數來記錄安裝Python包的安裝路徑,以便以后卸載;
$ pip3 show hello


$ py -3 setup.py install --record log.txt
running install
running bdist_egg
running egg_info
creating Hello.egg-info
writing Hello.egg-info\PKG-INFO
writing dependency_links to Hello.egg-info\dependency_links.txt
writing top-level names to Hello.egg-info\top_level.txt
writing manifest file 'Hello.egg-info\SOURCES.txt'
reading manifest file 'Hello.egg-info\SOURCES.txt'
writing manifest file 'Hello.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
copying build\lib\hello.py -> build\bdist.win-amd64\egg
byte-compiling build\bdist.win-amd64\egg\hello.py to hello.cpython-36.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying Hello.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying Hello.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying Hello.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying Hello.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\Hello-1.0-py3.6.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing Hello-1.0-py3.6.egg
Copying Hello-1.0-py3.6.egg to c:\python36\lib\site-packages
Adding Hello 1.0 to easy-install.pth file

Installed c:\python36\lib\site-packages\hello-1.0-py3.6.egg
Processing dependencies for Hello==1.0
Finished processing dependencies for Hello==1.0
writing list of installed files to 'log.txt'


$ ls -lR
.:
total 3
drwxr-xr-x 1 guowli 1049089   0 Oct 11 17:03 build/
drwxr-xr-x 1 guowli 1049089   0 Oct 11 17:03 dist/
drwxr-xr-x 1 guowli 1049089   0 Oct 11 17:03 Hello.egg-info/
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089  51 Oct 11 17:03 log.txt
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py

./build:
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 11 17:03 bdist.win-amd64/
drwxr-xr-x 1 guowli 1049089 0 Oct 11 17:00 lib/

./build/bdist.win-amd64:
total 0

./build/lib:
total 1
-rw-r--r-- 1 guowli 1049089 39 Oct 11 16:57 hello.py

./dist:
total 4
-rw-r--r-- 1 guowli 1049089 1259 Oct 11 17:03 Hello-1.0-py3.6.egg

./Hello.egg-info:
total 4
-rw-r--r-- 1 guowli 1049089   1 Oct 11 17:03 dependency_links.txt
-rw-r--r-- 1 guowli 1049089 221 Oct 11 17:03 PKG-INFO
-rw-r--r-- 1 guowli 1049089 133 Oct 11 17:03 SOURCES.txt
-rw-r--r-- 1 guowli 1049089   6 Oct 11 17:03 top_level.txt


$ cat log.txt
c:\python36\lib\site-packages\Hello-1.0-py3.6.egg


$ pip3 show hello
Name: hello
Version: 1.0
Summary: A simple example
Home-page: www.cnblogs.com/anliven
Author: Anliven
Author-email: anliven@yeah.net
License: UNKNOWN
Location: c:\python36\lib\site-packages\hello-1.0-py3.6.egg
Requires:
Required-by:


$

4- 安裝演示

執行install命令時,使用參數“-n”,將只進行演示,並不真正執行安裝;
--dry-run (-n) don't actually do anything
py -3 setup.py install -n

5- 卸載模塊

Setuptools沒有提供標准的uninstall命令,需要手工卸載安裝的模塊;
  • 執行“python setup.py install --record log.txt”命令,安裝包的同時記錄Python包的安裝文件路徑;
  • 卸載時,只需要刪除log.txt文件記錄的安裝文件路徑,即可卸載;
  • 如在Linux環境下,可使用“cat log.txt | xargs rm –rf”命令刪除,即可卸載;
$ cat log.txt
c:\python36\lib\site-packages\Hello-1.0-py3.6.egg

手工刪除

$ ls -l /c/Python36/Lib/site-packages/Hello-1.0-py3.6.egg
-rw-r--r-- 1 guowli 1049089 1246 Oct 10 16:49 /c/Python36/Lib/site-packages/Hello-1.0-py3.6.egg

$ pip3 show hello
Name: hello
Version: 1.0
Summary: A simple example
Home-page: UNKNOWN
Author: Anliven
Author-email: anliven@yeah.net
License: UNKNOWN
Location: c:\python36\lib\site-packages\hello-1.0-py3.6.egg
Requires:
Required-by:

$ rm -rf /c/Python36/Lib/site-packages/Hello-1.0-py3.6.egg

$ pip3 show hello

6- 程序打包(創建可分發的Python安裝包)

編寫安裝腳本setup.py后,就可以用來進行程序打包(創建歸檔文件);
支持多種格式,主要分為sdist(Source distribution,源碼發布)和bdist(Built Distribution,可執行文件發布)兩類;

6.1 Source Distribution(執行sdist命令)

  • 使用命令sdist(Source distribution)可以打包成源碼發布;
  • 使用命令開關“--formats”可指定一種或多種壓縮格式(用逗號分隔);
如果執行“py -3 setup.py sdist --formats=tar,zip”,那么將生成.tar和.zip兩個格式文件。
確認可使用的壓縮格式:
$ py -3 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

示例:默認壓縮格式.tar.gz

$ ls -l
total 2
-rw-r--r-- 1 guowli 1049089  39 Oct 10 16:34 hello.py
-rw-r--r-- 1 guowli 1049089 214 Oct 10 16:40 setup.py


$ cat -n hello.py
     1  # coding=utf-8
     2  print("This is a test!")


$ cat -n setup.py
     1  # coding=utf-8
     2  from setuptools import setup
     3
     4  setup(name='Hello',
     5        version='1.0',
     6        description='A simple example',
     7        author='Anliven',
     8        author_email='anliven@yeah.net',
     9        py_modules=['hello'])


$ py -3 setup.py sdist
running sdist
running egg_info
creating Hello.egg-info
writing Hello.egg-info\PKG-INFO
writing dependency_links to Hello.egg-info\dependency_links.txt
writing top-level names to Hello.egg-info\top_level.txt
writing manifest file 'Hello.egg-info\SOURCES.txt'
reading manifest file 'Hello.egg-info\SOURCES.txt'
writing manifest file 'Hello.egg-info\SOURCES.txt'
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md

running check
warning: check: missing required meta-data: url

creating Hello-1.0
creating Hello-1.0\Hello.egg-info
copying files to Hello-1.0...
copying hello.py -> Hello-1.0
copying setup.py -> Hello-1.0
copying Hello.egg-info\PKG-INFO -> Hello-1.0\Hello.egg-info
copying Hello.egg-info\SOURCES.txt -> Hello-1.0\Hello.egg-info
copying Hello.egg-info\dependency_links.txt -> Hello-1.0\Hello.egg-info
copying Hello.egg-info\top_level.txt -> Hello-1.0\Hello.egg-info
Writing Hello-1.0\setup.cfg
creating dist
Creating tar archive
removing 'Hello-1.0' (and everything under it)


$ ls -l
total 2
drwxr-xr-x 1 guowli 1049089   0 Oct 11 15:05 dist/
drwxr-xr-x 1 guowli 1049089   0 Oct 11 15:05 Hello.egg-info/
-rw-r--r-- 1 guowli 1049089  39 Oct 10 16:34 hello.py
-rw-r--r-- 1 guowli 1049089 214 Oct 10 16:40 setup.py


$ ls -lR
.:
total 2
drwxr-xr-x 1 guowli 1049089   0 Oct 11 15:05 dist/
drwxr-xr-x 1 guowli 1049089   0 Oct 11 15:05 Hello.egg-info/
-rw-r--r-- 1 guowli 1049089  39 Oct 10 16:34 hello.py
-rw-r--r-- 1 guowli 1049089 214 Oct 10 16:40 setup.py

./dist:
total 1
-rw-r--r-- 1 guowli 1049089 673 Oct 11 15:05 

./Hello.egg-info:
total 4
-rw-r--r-- 1 guowli 1049089   1 Oct 11 15:05 dependency_links.txt
-rw-r--r-- 1 guowli 1049089 205 Oct 11 15:05 PKG-INFO
-rw-r--r-- 1 guowli 1049089 133 Oct 11 15:05 SOURCES.txt
-rw-r--r-- 1 guowli 1049089   6 Oct 11 15:05 top_level.txt

$

告警提示

可能會出現一些告警提示缺少某些信息,不影響創建歸檔文件,可以忽略。
如果想去除告警,可以在setup.py所在目錄添加相應文件或信息。
warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md
warning: check: missing required meta-data: url

分發與安裝

生成的dist目錄里的Hello-1.0.tar.gz文件就是歸檔文件,可將此文件分發給他人,然后對方可將其解壓縮,再使用腳本setup.py進行安裝。

6-2 Built Distribution(執行bdist命令)

和源碼包相比,由於是預先構建好的可執行文件,所以安裝更快。
確認可使用的壓縮格式:
$ py -3 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
同時為了簡化操作,Setuptools 提供了如下命令(執行“py -3 setup.py --help-commands”獲得詳細信息);
例如:執行“py -3 setup.py bdist_wininst”
  bdist_dumb        create a "dumb" built distribution
  bdist_rpm         create an RPM distribution
  bdist_wininst     create an executable installer for MS Windows

示例:wininst格式(Windows executable installer)

$ ls -l
total 2
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py


$ py -3 setup.py bdist --formats=wininst
running bdist
running bdist_wininst
running build
running build_py
creating build
creating build\lib
copying hello.py -> build\lib
installing to build\bdist.win-amd64\wininst
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wininst
creating build\bdist.win-amd64\wininst\PURELIB
copying build\lib\hello.py -> build\bdist.win-amd64\wininst\PURELIB
running install_egg_info
running egg_info
creating Hello.egg-info
writing Hello.egg-info\PKG-INFO
writing dependency_links to Hello.egg-info\dependency_links.txt
writing top-level names to Hello.egg-info\top_level.txt
writing manifest file 'Hello.egg-info\SOURCES.txt'
reading manifest file 'Hello.egg-info\SOURCES.txt'
writing manifest file 'Hello.egg-info\SOURCES.txt'
Copying Hello.egg-info to build\bdist.win-amd64\wininst\PURELIB\Hello-1.0-py3.6.egg-info
running install_scripts
creating 'C:\Users\guowli\AppData\Local\Temp\tmppdxufr2q.zip' and adding '.' to it
adding 'PURELIB\hello.py'
adding 'PURELIB\Hello-1.0-py3.6.egg-info\dependency_links.txt'
adding 'PURELIB\Hello-1.0-py3.6.egg-info\PKG-INFO'
adding 'PURELIB\Hello-1.0-py3.6.egg-info\SOURCES.txt'
adding 'PURELIB\Hello-1.0-py3.6.egg-info\top_level.txt'
creating dist
removing 'build\bdist.win-amd64\wininst' (and everything under it)


$ ls -lR
.:
total 2
drwxr-xr-x 1 guowli 1049089   0 Oct 12 14:43 build/
drwxr-xr-x 1 guowli 1049089   0 Oct 12 14:43 dist/
drwxr-xr-x 1 guowli 1049089   0 Oct 12 14:43 Hello.egg-info/
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py

./build:
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 12 14:43 bdist.win-amd64/
drwxr-xr-x 1 guowli 1049089 0 Oct 12 14:43 lib/

./build/bdist.win-amd64:
total 0

./build/lib:
total 1
-rw-r--r-- 1 guowli 1049089 39 Oct 11 16:57 hello.py

./dist:
total 580
-rwxr-xr-x 1 guowli 1049089 591354 Oct 12 14:43 Hello-1.0.win-amd64.exe*

./Hello.egg-info:
total 4
-rw-r--r-- 1 guowli 1049089   1 Oct 12 14:43 dependency_links.txt
-rw-r--r-- 1 guowli 1049089 221 Oct 12 14:43 PKG-INFO
-rw-r--r-- 1 guowli 1049089 133 Oct 12 14:43 SOURCES.txt
-rw-r--r-- 1 guowli 1049089   6 Oct 12 14:43 top_level.txt


$

7- wheel與egg格式

Wheel格式包其實也是一種 built 包,是官方推薦的打包方式,用來替換egg格式包;
  • 創建egg包:“python setup.py bdist_egg”
  • 創建wheel包:“python setup.py bdist_wheel”
執行“py -3 setup.py --help-commands”獲得詳細信息;
bdist_wheel    create a wheel distribution
bdist_egg      create an "egg" distribution

7-1 示例:生成Wheel格式包

使用 wheel 打包,首先要安裝 wheel:“pip install wheel”
$ ls -l
total 2
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py


$ py -3 setup.py bdist_wheel
running bdist_wheel
running build
running build_py
creating build
creating build\lib
copying hello.py -> build\lib
installing to build\bdist.win-amd64\wheel
running install
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wheel
copying build\lib\hello.py -> build\bdist.win-amd64\wheel\.
running install_egg_info
running egg_info
creating Hello.egg-info
writing Hello.egg-info\PKG-INFO
writing dependency_links to Hello.egg-info\dependency_links.txt
writing top-level names to Hello.egg-info\top_level.txt
writing manifest file 'Hello.egg-info\SOURCES.txt'
reading manifest file 'Hello.egg-info\SOURCES.txt'
writing manifest file 'Hello.egg-info\SOURCES.txt'
Copying Hello.egg-info to build\bdist.win-amd64\wheel\.\Hello-1.0-py3.6.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\Hello-1.0.dist-info\WHEEL
creating 'D:\Anliven-Running\Zen\PycharmProjects\TestPackage\dist\Hello-1.0-py3-none-any.whl' and adding '.' to it
adding 'hello.py'
adding 'Hello-1.0.dist-info\top_level.txt'
adding 'Hello-1.0.dist-info\WHEEL'
adding 'Hello-1.0.dist-info\METADATA'
adding 'Hello-1.0.dist-info\RECORD'
removing build\bdist.win-amd64\wheel


$ ls -lR
.:
total 2
drwxr-xr-x 1 guowli 1049089   0 Oct 12 15:36 build/
drwxr-xr-x 1 guowli 1049089   0 Oct 12 15:36 dist/
drwxr-xr-x 1 guowli 1049089   0 Oct 12 15:36 Hello.egg-info/
-rw-r--r-- 1 guowli 1049089  39 Oct 11 16:57 hello.py
-rw-r--r-- 1 guowli 1049089 251 Oct 11 17:00 setup.py

./build:
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 12 15:36 bdist.win-amd64/
drwxr-xr-x 1 guowli 1049089 0 Oct 12 15:36 lib/

./build/bdist.win-amd64:
total 0

./build/lib:
total 1
-rw-r--r-- 1 guowli 1049089 39 Oct 11 16:57 hello.py

./dist:
total 4
-rw-r--r-- 1 guowli 1049089 1178 Oct 12 15:36 Hello-1.0-py3-none-any.whl

./Hello.egg-info:
total 4
-rw-r--r-- 1 guowli 1049089   1 Oct 12 15:36 dependency_links.txt
-rw-r--r-- 1 guowli 1049089 221 Oct 12 15:36 PKG-INFO
-rw-r--r-- 1 guowli 1049089 133 Oct 12 15:36 SOURCES.txt
-rw-r--r-- 1 guowli 1049089   6 Oct 12 15:36 top_level.txt


$

7-2 示例:安裝Wheel格式包

可以使用pip直接安裝和卸載wheel格式包
$ pip3 show hello


$ ls -l dist/
total 4
-rw-r--r-- 1 guowli 1049089 1178 Oct 12 15:36 Hello-1.0-py3-none-any.whl


$ pip3 install dist/Hello-1.0-py3-none-any.whl
Processing d:\anliven-running\zen\pycharmprojects\testpackage\dist\hello-1.0-py3-none-any.whl
Installing collected packages: Hello
Successfully installed Hello-1.0


$ pip3 show hello
Name: Hello
Version: 1.0
Summary: A simple example
Home-page: www.cnblogs.com/anliven
Author: Anliven
Author-email: anliven@yeah.net
License: UNKNOWN
Location: c:\python36\lib\site-packages
Requires:
Required-by:


$ pip3 uninstall hello
Uninstalling Hello-1.0:
  Would remove:
    c:\python36\lib\site-packages\hello-1.0.dist-info\*
    c:\python36\lib\site-packages\hello.py
Proceed (y/n)? y
  Successfully uninstalled Hello-1.0


$ pip3 show hello


$

8- 向PyPI注冊包

PyPI(Python Package Index): https://pypi.org/
  1. 執行命令“python setup.py register”向標准庫注冊,將顯示菜單;
  2. 根據提示,注冊包;
  3. 執行命令“python setup.py sdist upload”上傳源碼分發包到PyPI;
$ py -3 setup.py register
running register
running check
warning: check: missing required meta-data: name, version, url

warning: check: missing meta-data: either (author and author_email) or (maintainer and maintainer_email) must be supplied

We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]:

9- 拾遺

9.1 PYTHONPATH

PYTHONPATH是Python搜索路徑,默認import的模塊都會從PYTHONPATH里面尋找;
目前安裝新Python版本時,有“Add Python to Path”選項,選中則會自動完成環境變量配置;
以在Windows系統中設置PYTHONPATH為例:

To set environment variable for Python, open environment variables setting, new a system variable named PYTHONPATH, set its value to the Python installation path and append PYTHONPATH to path.

 

C:\Python36\;C:\Python36\Lib\site-packages;C:\Python36\Scripts

打印PYTHONPATH:

>>> import sys
>>> print(sys.path)
['', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Python36\\lib\\site-packages\\win32', 'C:\\Python36\\lib\\site-packages\\win32\\lib', 'C:\\Python36\\lib\\site-packages\\Pythonwin']
>>>

9-2 對比egg與wheel

一句話總結:wheel文件格式將最終取代egg文件格式,建議使用wheel文件來發布Python包。
Wheel和Egg都是打包的格式,都可用於Python模塊的分發與安裝;
Egg格式是由Setuptools在2004年引入,通過Setuptools可以識別Egg格式並解析安裝;
Wheel格式是由PEP427在2012年定義,本質是zip包格式,最終將替代Egg格式;
Wheel現在被認為是構建和二進制包的標准格式,推薦使用wheel格式發布Python包,目前pip也可直接安裝wheel格式包;

Wheel和Egg的主要的不同點:
  - Wheel有一個官方的PEP427來定義,而Egg沒有PEP定義;
  - Wheel是一種分發格式,即打包格式。而Egg既是一種分發格式,也是一種運行時安裝的格式,並且是可以被import的;
  - Wheel文件不會包含.pyc文件;
  - Wheel使用和PEP376兼容的.dist-info目錄,而Egg使用.egg-info目錄;
  - Wheel有着更豐富的命名規則;
  - Wheel是有版本的。每個Wheel文件都包含wheel規格的版本和打包它的實現;
  - Wheel在內部被sysconfig path type管理,因此轉向其他格式也更容易;

9-3 Python的包管理工具

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM