ython的開發(fā)效率非常的高,但是當(dāng)我們用python寫一些小工具需要給用戶使用的時(shí)候,用戶大多數(shù)是沒有安裝python的。
本文介紹兩種python的打包方案,使用pyinstaller和nuitka打包成exe(或其他操作系統(tǒng)的可執(zhí)行程序)后便可以直接分發(fā)給用戶,無需用戶安裝python。
這邊使用pyqt6來做演示,使用pyqt6我們可以快速創(chuàng)建一個(gè)跨平臺(tái)原生gui程序,也是這兩個(gè)打包工具最常用的場(chǎng)景之一。
安裝pyqt6庫(kù):
pip install pyqt6
創(chuàng)建mainwindow.py,寫入以下內(nèi)容:
import sys
from PyQt6.QtWidgets import QWidget, QToolTip, QPushButton, QApplication
from PyQt6.QtGui import QFont
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont("SansSerif", 10))
self.setToolTip("This is a <b>QWidget</b> widget")
btn=QPushButton("Button", self)
btn.setToolTip("This is a <b>QPushButton</b> widget")
btn.resize(btn.sizeHint())
btn.move(50, 50)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle("Tooltips")
self.show()
def main():
app=QApplication(sys.argv)
ex=Example()
sys.exit(app.exec())
if __name__=="__main__":
main()
執(zhí)行程序:
python mainwindow.py
執(zhí)行后會(huì)打開一個(gè)demo窗口:
安裝:
pip installer pyinstaller
打包為文件夾:
pyinstaller -D -w ./mainwindow.py
該命令會(huì)打包exe、python解釋器和其他dll到 dist/mainwindow 中。
打包為單個(gè)exe:
pyinstaller -F -w ./mainwindow.py
該命令只輸出一個(gè)mainwindow.exe到dist目錄中,打開 dist/mainwindow.exe 時(shí)會(huì)將所有依賴項(xiàng)解壓到臨時(shí)目錄中然后運(yùn)行,當(dāng)然這一步對(duì)用戶是無感的。
使用這兩種方法生成的exe都可以直接分發(fā)給用戶,pyinstaller是大多數(shù)時(shí)候的首選項(xiàng),方便快捷。不過pyinstaller最大的問題是無法隱藏源碼以及效率稍微慢一些。
nuitka使用c語(yǔ)言編譯器將python源碼及模塊編譯成原生二進(jìn)制,據(jù)此可以做到完全隱藏源碼,同時(shí)提高運(yùn)行效率。
安裝:
pip installer nuitka
安裝的時(shí)候nuitka會(huì)根據(jù)我們的操作系統(tǒng)選擇最佳的c語(yǔ)言編譯器。
打包為文件夾:
python -m nuitka mainwindow.py --standalone --enable-plugin=pyqt6 --disable-console
該命令會(huì)輸出 mainwindow.exe 到當(dāng)前目錄下,我們可以直接拿來執(zhí)行或者分發(fā)給其他用戶。
需要注意的是這種方式也是解壓到臨時(shí)目錄中,效率相比打包成文件夾慢一點(diǎn),但是整體還是比 pyinstaller 快。
原文鏈接:https://juejin.cn/post/7369876349488775207
本說明
jdk:1.8
scala:2.11.8
spark:2.3.0
hadoop:2.8.3
環(huán)境準(zhǔn)備
jdk配置
1、配置JAVA_HOME與Path:
Path
2、驗(yàn)證配置:
scala安裝與配置
1、scala下載:
訪問官方地址http://www.scala-lang.org/download/2.11.8.html
2、配置Path:
3、驗(yàn)證配置:
Spark安裝與配置
1、spark下載:
訪問官方地址http://spark.apache.org/downloads.html
選中官方推薦的地址即可下載,其他地址也可用(建議采用迅雷等下載工具下載,速度比較會(huì)快很多)
2、解壓至D盤;
3、配置Path:
4、通過spark-shell進(jìn)入Spark的交互式命令行模式:
如上,可以看到對(duì)應(yīng)的spark、scala、java版本,同時(shí)也看到了異常信息,該異常信息是由于hadoop導(dǎo)致的,下面來配置hadoop。
Hadoop安裝與配置
1、hadoop下載
訪問官方http://hadoop.apache.org/releases.html
進(jìn)入下載頁(yè),如下建議地址(同樣建議用迅雷等下載工具下載,速度更快)
2、解壓至
3、配置HADOOP_HOME&Path:
path:
4、winutils下載:https://github.com/steveloughran/winutils
下載對(duì)應(yīng)版本的bin目錄直接替換本地bin目錄即可。
5、此時(shí)繼續(xù)spark-shell進(jìn)入spark命令行交互模式:
此時(shí)即不會(huì)出現(xiàn)上述的異常提示。
6、訪問控制臺(tái)中的webui如下:
本文實(shí)現(xiàn)RAR批量解壓的功能,通過python腳本調(diào)用WinRAR.exe解壓文件時(shí)似乎不會(huì)再有廣告框彈出。
通過python調(diào)用WinRAR.exe程序?qū)崿F(xiàn)RAR文件的批量解壓,代碼如下:
import argparse
import os
class RarExtractor:
def __init__(self, in_dir="./", out_dir="./", pwds=None, exe=None):
self.in_dir=in_dir
self.out_dir=out_dir
self.pwds=pwds if pwds else ['1234']
self.exe='"%s"' % exe if exe else '"C:\Program Files\WinRAR\WinRAR.exe"'
def extract_files(self, pwds, file_path, dst):
if not os.path.exists(dst):
os.mkdir(dst)
if os.path.isdir(dst) and os.path.isfile(file_path):
try:
for pwd in pwds:
extract_cmd=r'%s x -y -p%s %s %s' % (self.exe, pwd, file_path, dst)
if os.system(extract_cmd)==0:
print("Extract %s OK." % file_path)
return 0
else:
print("Extract %s failed." % file_path)
return -1
except RuntimeError:
print("error")
return -1
else:
print('File not exist')
return -1
def extract_all_rar(self):
for root, dirs, files in os.walk(self.in_dir):
for f in files:
(filename, ext)=os.path.splitext(f)
if ext=='.rar':
file_path=os.path.join(root, f)
print(file_path)
self.extract_files(self.pwds, file_path, os.path.join(self.out_dir, filename))
def _parse_options():
parser=argparse.ArgumentParser()
parser.add_argument("--in_dir", action="store", dest="in_dir", required=True, help="Rar files dir")
parser.add_argument("--out_dir", action="store", dest="out_dir", required=False, help="Extracted file dir")
parser.add_argument("--pwds", nargs='+', action="store", dest="pwds", required=False,
help="Password list to extract Rar: --pwds 1111 2222 3333")
parser.add_argument("--exe", action="store", dest="exe", required=False, help="RAR exe install path")
return parser.parse_args()
if __name__=='__main__':
options=_parse_options()
extractor=RarExtractor(options.in_dir, options.out_dir, options.pwds, options.exe)
extractor.extract_all_rar()
需要傳入的參數(shù)為:
三、測(cè)試
在目錄D:\rar_test\下新建3個(gè)txt文件,使用RAR加密壓縮,密碼為1024、2048和4096。
通過以下命令測(cè)試:
python rar_extractor.py --in_dir D:\rar_test\ --out_dir D:\rar_test\ --pwds 1024 2048 4096 --exe "C:\Program Files\WinRAR\WinRAR.exe"
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。