Ubuntu下使用PyInstaller的實(shí)戰(zhàn)案例

小樊
82
2024-09-05 11:05:55

在這個(gè)實(shí)戰(zhàn)案例中,我們將學(xué)習(xí)如何在Ubuntu系統(tǒng)下使用PyInstaller將Python腳本轉(zhuǎn)換為可執(zhí)行文件

  1. 安裝Python和pip(如果尚未安裝):
sudo apt update
sudo apt install python3 python3-pip
  1. 使用pip安裝PyInstaller:
pip3 install pyinstaller
  1. 創(chuàng)建一個(gè)簡(jiǎn)單的Python腳本作為示例。創(chuàng)建一個(gè)名為hello_world.py的文件,并添加以下內(nèi)容:
print("Hello, World!")
  1. 使用PyInstaller將Python腳本轉(zhuǎn)換為可執(zhí)行文件。在終端中,導(dǎo)航到包含hello_world.py的文件夾,然后運(yùn)行以下命令:
pyinstaller --onefile hello_world.py

這將創(chuàng)建一個(gè)名為dist的新文件夾,其中包含一個(gè)名為hello_world的可執(zhí)行文件。--onefile選項(xiàng)表示將所有文件合并到一個(gè)可執(zhí)行文件中。

  1. 運(yùn)行生成的可執(zhí)行文件:
./dist/hello_world

你應(yīng)該會(huì)看到輸出 “Hello, World!”。

這就是在Ubuntu下使用PyInstaller將Python腳本轉(zhuǎn)換為可執(zhí)行文件的實(shí)戰(zhàn)案例。現(xiàn)在,你可以使用相同的方法將其他Python腳本轉(zhuǎn)換為可執(zhí)行文件。

0