網站首頁 編程語言 正文
1. wheel介紹
.whl文件(WHL file)也稱為輪子(wheel),這是用于python分發(distribution)的標準內置包格式(standard built-package format)。它包含安裝所需的所有文件和元數據(metadata)。.whl文件使用zip進行壓縮。.whl文件還包含有關此wheel文件支持的Python版本和平臺的信息。.whl文件格式是一種即裝即用格式(ready-to-install format),允許在不構建源代碼分發(without building the source distribution)的情況下運行安裝包。
.whl文件本質上是zip文件,這些.whl文件可以使用解壓縮選項(unzip option)或標準解壓縮軟件應用程序(如WinZIP和WinRAR)解壓縮。
.whl文件按照以下約定命名:
{dist}-{version}(-{build})?-{python.version}-{abi}-{platform}.whl
如果我們之前使用pip安裝過Python包,那么我們的系統上很可能已經有輪子(wheel)被安裝過。pip是安裝wheel的包管理器。
我們可以通過pip安裝已下載的.whl文件:pip install <filename>.whl ,安裝包后,我們可以執行Python shell并嘗試導入包:import whl_dist_name
wheel的直接好處是我們與其他人共享我們的包,他們不必擔心構建它。他們只需pip install后即可使用該軟件包。它也是一個更小的共享文件(與所有源代碼相比),安裝速度更快,因為它不需要運行安裝腳本。
2. wheel的類型
(1).universal wheel:包含py2.py3-none-any.whl。它在任何操作系統和平臺上都支持Python 2和Python 3。
(2).pure-python wheel:包含py3-none-any.whl或py2-none-any.whl。它支持Python 3或Python 2,但不能同時支持兩者。它在其它方面與universal wheel相同,但它會被標記為py2或py3而不是py2.py3標簽。
(3).platform wheel:支持特定的Python版本和平臺。
3. 創建wheel
(1).將所有模塊(python腳本)、包(包含模塊的文件夾/目錄)保存在父目錄中。隨意命名根目錄,通常與項目相關。
(2).最好創建一個空的名為__init__.py文件,并將此__init__.py文件放在所有包目錄和子包目錄下。無需將其保存在根目錄中。
(3).創建一個名為setup.py的文件并將其放在根目錄中。此腳本的內容至少應包括:distribution name, version number, and list of package names
(4).轉到你可以運行python和pip命令的命令提示符。在命令提示符下更改目錄并導航到放置setup.py的項目的根目錄,執行以下命令:擴展名為.whl的文件將在根目錄下自動創建的子目錄中創建,名為dist。
wheel中并不包含setup.py,wheel不必運行setup.py腳本。
注:需提取安裝wheel setuptools: pip install wheel setuptools,在conda中默認是安裝的
python setup.py bdist_wheel --universal # universal wheel
python setup.py bdist_wheel # pure-Python wheel
這里通過conda在虛擬環境base下創建一個wheel,取名為testwheel目錄組織結構如下所示:
setup.py內容如下:
import setuptools
setuptools.setup(
name="testwheel",
version="1.0.0",
author="fengbingchun",
author_email="fengbingchun@163.com",
description="test wheel",
packages=setuptools.find_packages(),
url="https://github.com/fengbingchun",
license="MIT",
python_requires=">=3.8"
)
testwheel目錄下的__init__.py是個空文件,math目錄下的__init__.py內容如下:
from .math_add import *
from .math_sub import *
math_add.py內容如下:
def add3(a, b, c):
print("call add operation: three parameters ...")
return (a+b+c)
def add2(a, b):
print("call add operation: two parameters ...")
return (a+b)
math_sub.py內容如下:
def sub3(a, b, c):
print("call sub operation: three parameters ...")
return (a-b-c)
def sub2(a, b):
print("call sub operation: two parameters ...")
return (a-b)
執行如下命令生成wheel,此wheel僅限于在Python3上執行,將終端定位到setup.py目錄下
執行完上述命令后會產生3個新的目錄,build, dist, testwhell.egg-info,各個目錄的內容如下所示,生成的whell在dist目錄下,全名為testwheel-1.0.0-py3-none-any.whl,只需將此文件分發出去,其他人安裝后即能使用。
4. 導入使用wheel
如果你想在項目中安裝已經安裝過的wheel文件,需要更新此wheel的版本號。如果版本號保持不變,pip將不會安裝它。或者先卸載已安裝的whell: pip uninstall testwheel
通過conda新建一個虛擬環境testwheel,用來測試上面生成的testwheel-1.0.0-py3-none-any.whl,將終端定位到其它的test目錄下,并將生成的wheel文件拷貝到此目錄下,安裝,執行結果如下圖所示:
會將此wheel安裝到anaconda3/envs/testwheel/lib/python3.8/site-packages/目錄下,如下圖所示:
在tmp下添加一個test.py文件,用于測試wheel,內容如下:
from testwheel.math import math_add, math_sub
a, b, c = 10, 5, 2
print("add3:", math_add.add3(a, b, c))
print("add2:", math_add.add2(a, b))
print("sub3:", math_sub.sub3(a, b, c))
print("sub2:", math_sub.sub2(a, b))
print("test finish")
執行結果如下所示:可見正確的調用了wheel中的接口
如果需要反復的調整wheel的內容,需要反復的測試,又不想修改version number,一種方法是可先卸載已安裝的wheel,然后再次安裝新的wheel,如下圖所示:也可使用--force-reinstall
GitHub傳送門
原文鏈接:https://blog.csdn.net/fengbingchun/article/details/126910333
相關推薦
- 2022-06-19 python繪制分組對比柱狀圖_python
- 2023-01-10 redis中Could?not?get?a?resource?from?the?pool異常及解決方
- 2022-05-18 ASP.NET?Core?6框架揭秘實例演示之如何承載你的后臺服務_實用技巧
- 2022-12-06 C++中調用復制(拷貝)函數的三種情況總結_C 語言
- 2022-07-22 Math.ceil() 函數使用介紹
- 2022-04-20 C語言特殊符號的補充理解_C 語言
- 2023-05-24 Python?的第三方調試庫????pysnooper???使用示例_python
- 2022-07-11 Could not transfer artifact org.springframework.bo
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支