?
相信大家也有過這樣的困惑,本來已經(jīng)使用Python寫好了腳本代碼,又不想公開代碼邏輯,但又要給別人使用這些功能?
這可怎么辦呢?
有沒有什么辦法,可以將Python腳本打包成一個exe文件,這樣發(fā)布的時候,我只需要提交一個exe文件,別人只要雙擊打開就能使用我的功能,而我又不需要公開代碼,且不一舉兩得?
答案肯定是有的,請看下文分解。
這里我隨意編寫了一個Python腳本作為例子來介紹,功能很簡單,就是每秒鐘輸出一個索引值,值是遞加1的,代碼如下:
import time
index = 1
print('start running')
for index in range(1,1000):
print(index)
index = index + 1
time.sleep(1)
print('stop running ...')
使用Python命令行運行的結果如下:
那么我們使用工具把這個Python代碼打包成一個exe文件。
這里需要用到一個Python的工具包,交Pyinstaller,我們使用以下命令就可以安裝它。
pip install pyinstaller
如下所示:(黃色字體提示pip有新版本要更新,可以不用管他)
提示安裝成功后,輸入下面的命令,有正常輸出表示pyinstaller安裝成功了。
pyinstaller.exe --help
下面我們就開始使用這個工具將python腳本打包成exe文件。
首先,我們進到Python腳本文件所在的目錄,如下:(我的是powershell命令行環(huán)境)
接著使用命令開始打包Python腳本,交互輸出log如下所示:
PS Z:win_share_workspacetest_sharetest_python2exe> pyinstaller.exe -F .running.py
179 INFO: PyInstaller: 4.5.1
180 INFO: Python: 3.7.8
188 INFO: Platform: Windows-10-10.0.19041-SP0
316 INFO: wrote Z:win_share_workspacetest_sharetest_python2exerunning.spec
501 INFO: UPX is not available.
1113 INFO: Extending PYTHONPATH with paths
['Z:\win_share_workspace\test_share\test_python2exe',
'Z:\win_share_workspace\test_share\test_python2exe']
6010 INFO: checking Analysis
6047 INFO: Building Analysis because Analysis-00.toc is non existent
6048 INFO: Initializing module dependency graph...
6119 INFO: Caching module graph hooks...
6148 INFO: Analyzing base_library.zip ...
13118 INFO: Processing pre-find module path hook distutils from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
13120 INFO: distutils: retargeting to non-venv dir 'c:\users\redtea\appdata\local\programs\python\python37\lib'
15205 INFO: Caching module dependency graph...
16314 INFO: running Analysis Analysis-00.toc
16322 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:usersredteaappdatalocalprogramspythonpython37python.exe
18942 INFO: Analyzing Z:win_share_workspacetest_sharetest_python2exerunning.py
19151 INFO: Processing module hooks...
19153 INFO: Loading module hook 'hook-difflib.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
19155 INFO: Loading module hook 'hook-distutils.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
19157 INFO: Loading module hook 'hook-encodings.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20863 INFO: Loading module hook 'hook-heapq.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20865 INFO: Loading module hook 'hook-pickle.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20867 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
20869 INFO: Loading module hook 'hook-xml.py' from 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks'...
21563 INFO: Looking for ctypes DLLs
21564 INFO: Analyzing run-time hooks ...
21567 INFO: Including run-time hook 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py'
21636 INFO: Including run-time hook 'c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py'
21642 INFO: Looking for dynamic libraries
22600 INFO: Looking for eggs
22600 INFO: Using Python library c:usersredteaappdatalocalprogramspythonpython37python37.dll
22600 INFO: Found binding redirects:
[]
22748 INFO: Warnings written to Z:win_share_workspacetest_sharetest_python2exebuildrunningwarn-running.txt
23472 INFO: Graph cross-reference written to Z:win_share_workspacetest_sharetest_python2exebuildrunningxref-running.html
23842 INFO: checking PYZ
23921 INFO: Building PYZ because PYZ-00.toc is non existent
23921 INFO: Building PYZ (ZlibArchive) Z:win_share_workspacetest_sharetest_python2exebuildrunningPYZ-00.pyz
26078 INFO: Building PYZ (ZlibArchive) Z:win_share_workspacetest_sharetest_python2exebuildrunningPYZ-00.pyz completed successfully.
26806 INFO: checking PKG
26830 INFO: Building PKG because PKG-00.toc is non existent
26830 INFO: Building PKG (CArchive) PKG-00.pkg
48506 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
48830 INFO: Bootloader c:usersredteaappdatalocalprogramspythonpython37libsite-packagesPyInstallerbootloaderWindows-64bitrun.exe
48830 INFO: checking EXE
48912 INFO: Building EXE because EXE-00.toc is non existent
48913 INFO: Building EXE from EXE-00.toc
49652 INFO: Copying icons from ['c:\users\redtea\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\images\icon-console.ico']
50456 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
50456 INFO: Writing RT_ICON 1 resource with 3752 bytes
50457 INFO: Writing RT_ICON 2 resource with 2216 bytes
50458 INFO: Writing RT_ICON 3 resource with 1384 bytes
50458 INFO: Writing RT_ICON 4 resource with 37019 bytes
50458 INFO: Writing RT_ICON 5 resource with 9640 bytes
50459 INFO: Writing RT_ICON 6 resource with 4264 bytes
50459 INFO: Writing RT_ICON 7 resource with 1128 bytes
52342 INFO: Updating manifest in Z:win_share_workspacetest_sharetest_python2exebuildrunningrun.exe.z10cqb8f
54272 INFO: Updating resource type 24 name 1 language 0
56760 INFO: Appending archive to EXE Z:win_share_workspacetest_sharetest_python2exedistrunning.exe
155327 INFO: Building EXE from EXE-00.toc completed successfully.
看到最后提示successfully,就表示打包成功了。生成的exew文件位于dist目錄,我們打開看看。
打包出來的文件還是畢竟大,簡單幾行代碼就達到了6MB,主要是因為它把一些Python的依賴庫也打包進來了,所以我們雙擊就可以運行,運行效果與Python命令行運行一模一樣。
好了,本期將Python腳本打包成exe文件的教程到這里就結束了,你學會了嗎?
更多實用教程,請關注我的主頁,謝謝大家。
?審核編輯:湯梓紅
-
exe
+關注
關注
0文章
110瀏覽量
32380 -
python
+關注
關注
56文章
4792瀏覽量
84627 -
腳本
+關注
關注
1文章
389瀏覽量
14858
發(fā)布評論請先 登錄
相關推薦
評論