打包python腳本的血與淚(python能做腳本嗎)

本文目錄一覽:

如何把python腳本打包成exe

使用py2exe

安裝py2exe

進入python程序所在文件夾

在文件夾下寫一個setup.py的python文件,內容如下

from distutils.core import setup

import py2exe

setup(console=[“將要轉換的文件名稱.py”])

打開CMD窗口,將工作目錄切換到python文件所在文件夾,並輸入命令“python setup.py py2exe”

命令成功完成後,進入原python文件的文件夾下可看到新產生了文件夾

進入dist文件夾,裡面的exe就是我們需要的windows程序。

Pycharm怎麼打包Python腳本

python3是必須的,pyqt是做界面的,pycharm是優秀的IDE,pyinstaller生成可執行文件,。

1.先按照教程安裝python3,QT,pycharm軟件,因為目前PyQt5只支持python3.4,所以安裝python3.4。

參考教程PyQt5+python3+pycharm開發環境配置

在環境變量中增加:

QT_QPA_PLATFORM_PLUGIN_PATH

C:\Python34\Lib\site-packages\PyQt5\plugins

2.按照教程創建界面,創建窗口時選“Widget”,然後按照說明創建調用的主程序,就可以運行了。

PyQt5學習筆記03—-Qt Designer生成源碼

3.dos命令行輸入:py -m pip install pyinstaller,安裝pyinstaller。

4.在要製作的exe文件目錄輸入pyinstaller -F -w *.py,就可以製作出exe。生成的文件放在同目錄dist下。

-F(注意大寫)是所有庫文件打包成一個exe,-w是不出黑色控制台窗口。

不加-F參數生成一堆文件,但運行快。壓縮後比單個exe文件還小一點點。

加-F參數生成一個exe文件,運行起來慢。

按照第一步的方法,將pyinstaller加入pycharm的external tool,我加了兩個,一個生成單獨exe文件,一個生成運行帶庫的程序目錄。

好了,環境搞好了,做界面,編程,調試,生成exe,慢慢練習吧!

python pyinstaller問題

打包python腳本為exe的坎坷經歷, by pyinstaller方法

又應驗了那句歌詞. 不經歷風雨, 怎麼見得了彩虹.

安裝過程略去不提, 僅提示: pip install pyinstaller

打包指令

粗看包里的文檔, 然後開始打包:

打開console

就用了這個命令: pyinstaller monitor_rt_quotes.py

結果就是比預期麻煩的多的多的: 幹活, 硬盤不停地balabala叫, 漫長

這裡記錄一下控制台顯示的提示信息:

官方文檔對上述指令的解釋為:

|PyInstaller| analyzes myscript.py and:

Writes myscript.spec in the same folder as the script.

Creates a folder build in the same folder as the script if it does not exist.

Writes some log files and working files in the build folder.

Creates a folder dist in the same folder as the script if it does not exist.

Writes the myscript executable folder in the dist folder.

In the dist folder you find the bundled app you distribute to your users.

巨長的提示後, 得到了exe文件, 有18Mb的大小(太龐大了, 肥肥肥!!!), 但是結果很悲劇:

D:\DB\fz\build\monitor_rt_quotesmonitor_rt_quotes.exe sz000911

Error loading Python DLL: D:\DB\fz\build\monitor_rt_quotes\python27.dll (error code 126)

估計是沒有添加必要的打包選項造成的. 繼續努力.

不死心! 解決問題需要由簡到繁, 先從hellowworld.py做起. 搞定了.

被打包的文件: helloworld.py, 人人皆知.

打包的命令: pyinstaller -D helloworld.py

打包的console提示: 見隨後的引述塊

打包發布exe時的紀律(注意事項):

很簡單的py腳本. Simple is better than complex. 應該遵守Python的規則.

所以在打包為exe時, 應該盡量減少沒有必要的python包的導入. 否則吃的太胖

可以運行的exe文件位於: dist 目錄下, 而不是build目錄下的.

運行後的結果:

D:\DB\fzpyinstaller -D helloworld.py155 INFO: PyInstaller: 3.2.1155 INFO: Python: 2.7.11155 INFO: Platform: Windows-XP-5.1.2600-SP3155 INFO: wrote D:\DB\fz\helloworld.spec155 INFO: UPX is not available.155 INFO: Extending PYTHONPATH with paths

[‘D:\\DB’, ‘D:\\DB\\fz’]155 INFO: checking Analysis155 INFO: Building Analysis because out00-Analysis.toc is non existent155 INFO: Initializing module dependency graph…155 INFO: Initializing module graph hooks…312 INFO: running Analysis out00-Analysis.toc327 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable  required by d:\anaconda2\python.exe327 INFO: Found C:\WINDOWS\WinSxS\Policies\x86_policy.9.0.Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_x-ww_b7353f75\9.0.30729.1.policy327 INFO: Searching for assembly x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww …327 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e.manifest327 INFO: Searching for file msvcr90.dll327 INFO: Found file C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e\msvcr90.dll327 INFO: Searching for file msvcp90.dll327 INFO: Found file C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e\msvcp90.dll327 INFO: Searching for file msvcm90.dll343 INFO: Found file C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e\msvcm90.dll343 INFO: Found C:\WINDOWS\WinSxS\Policies\x86_policy.9.0.Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_x-ww_b7353f75\9.0.30729.1.policy343 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) – (9, 0,30729, 1)484 INFO: Caching module hooks…500 INFO: Analyzing D:\DB\fz\helloworld.py500 INFO: Loading module hooks…500 INFO: Loading module hook “hook-encodings.py”…5405 INFO: Loading module hook “hook-httplib.py”…5421 INFO: Looking for ctypes DLLs5437 INFO: Analyzing run-time hooks …5452 INFO: Looking for dynamic libraries6155 INFO: Looking for eggs6155 INFO: Using Python library d:\anaconda2\python27.dll6171 INFO: Found binding redirects:[BindingRedirect(name=u’Microsoft.VC90.CRT’, language=None, arch=u’x86′, oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 1), publicKeyToken=u’1fc8b3b9a1e18e3b’)]6187 INFO: Warnings written to D:\DB\fz\build\helloworld\warnhelloworld.txt6280 INFO: checking PYZ6296 INFO: Building PYZ because out00-PYZ.toc is non existent6296 INFO: Building PYZ (ZlibArchive) D:\DB\fz\build\helloworld\out00-PYZ.pyz6968 INFO: Building PYZ (ZlibArchive) D:\DB\fz\build\helloworld\out00-PYZ.pyz completed successfully.7062 INFO: checking PKG7062 INFO: Building PKG because out00-PKG.toc is non existent7077 INFO: Building PKG (CArchive) out00-PKG.pkg7125 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.7140 INFO: Bootloader d:\anaconda2\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe7140 INFO: checking EXE7155 INFO: Building EXE because out00-EXE.toc is non existent7155 INFO: Building EXE from out00-EXE.toc7171 INFO: Appending archive to EXE D:\DB\fz\build\helloworld\helloworld.exe7171 INFO: Building EXE from out00-EXE.toc completed successfully.7187 INFO: checking COLLECT7202 INFO: Building COLLECT because out00-COLLECT.toc is non existent7202 INFO: Building COLLECT out00-COLLECT.toc7218 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) – (9, 0, 30729, 1)7375 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) – (9, 0, 30729, 1)7390 INFO: Updating manifest in C:\Documents and Settings\Administrator\Application Data\pyinstaller\bincache00_py27_32bit\python27.dll7405 INFO: Updating resource type 24 name 2 language 10337468 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) – (9, 0, 30729, 1)7484 INFO: Updating manifest in C:\Documents and Settings\Administrator\Application Data\pyinstaller\bincache00_py27_32bit\unicodedata.pyd7500 INFO: Updating resource type 24 name 2 language 10337546 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) – (9, 0, 30729, 1)7562 INFO: Updating manifest in C:\Documents and Settings\Administrator\Application Data\pyinstaller\bincache00_py27_32bit\_hashlib.pyd7562 INFO: Updating resource type 24 name 2 language 10337609 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) – (9, 0, 30729, 1)7625 INFO: Updating manifest in C:\Documents and Settings\Administrator\Application Data\pyinstaller\bincache00_py27_32bit\_ctypes.pyd7640 INFO: Updating resource type 24 name 2 language 10337687 INFO: Redirecting Microsoft.VC90

如何將一整個python工程打包

1、下載並安裝py2exe,可下載最新版本。

2、編寫安裝腳本,比較簡單,不過多解釋,如下:

Screenshot_setup.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from distutils.core import setup

import py2exe

includes = [“encodings”, “encodings.*”]

data_files = [‘CameraDll.dll’]

options = {“py2exe”:

{ “compressed”: 1,

“optimize”: 2,

“bundle_files”: 1,

“includes”: includes

}

}

setup(

version = “1.0”,

description = “Screenshot Tool”,

name = “Screenshot Tool”,

options = options,

zipfile=None,

data_files = data_files,

console=[{“script”: “Screenshot.py”, “icon_resources”: [(1, “Screenshot.ico”)] }]

)

各參數含義見第5小節或執行python命令行下執行help(py2exe)查看。

3、打包程序:

a、將要找包的程序、文件和打包腳本放在同一目錄下,如下:

E:\tmp\pyexe_tmptree /F

文件夾 PATH 列表

卷序列號碼為 0007F240 101F:F68D

E:.

CameraDll.dll

screenshot.ico

Screenshot.py

screenshot_setup.py

沒有子文件夾

E:\tmp\pyexe_tmp

b、執行打包腳本:

E:\tmp\pyexe_tmppython screenshot_setup.py py2exe

python screenshot_setup.py py2exe

running py2exe

creating E:\tmp\pyexe_tmp\build

creating E:\tmp\pyexe_tmp\build\bdist.win32

creating E:\tmp\pyexe_tmp\build\bdist.win32\winexe

creating E:\tmp\pyexe_tmp\build\bdist.win32\winexe\collect-2.6

creating E:\tmp\pyexe_tmp\build\bdist.win32\winexe\bundle-2.6

creating E:\tmp\pyexe_tmp\build\bdist.win32\winexe\temp

creating E:\tmp\pyexe_tmp\dist

*** searching for required modules ***

*** parsing results ***

*** finding dlls needed ***

*** create binaries ***

*** byte compile python files ***

writing byte-compilation script ‘e:\tmp\tmp1ecbcw.py’

原創文章,作者:BT20E,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129019.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
BT20E的頭像BT20E
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論