簡體   English   中英

如何使用python代碼創建Linux可執行文件

[英]How to create a Linux executable file using python code

例如,我使用Tkinter創建了具有簡單GUI的welcomeGUI.py文件。

from Tkinter import *
import ttk

class Application():

    def __init__(self, master):
        frame = Create_widgets(master)
        frame.pack()

class Create_widgets(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)

        self.toplabel = Label(master, text="Welcome!", font=('cambria', 20, 'bold'),
                         fg="white", bg="Midnight Blue")
        self.toplabel.pack(fill=X, ipady=100)

        statuslabel = Label(master, bg="Midnight Blue")
        statuslabel.pack(fill=X)
        self.midlabel = Label(master, text="Device ready,connect a flash drive to continue...",
                         font=('Ubuntu-L', 12), fg= "white", bg="Midnight Blue", anchor="n")
        self.midlabel.pack(fill=X, ipady=5)

        bottomlabel = Label(master, bg="Gainsboro")
        bottomlabel.pack(side=BOTTOM, fill=X)

        button1 = ttk.Button(bottomlabel, text="Close")
        button1.pack(side=BOTTOM)

#**** Main ****

root = Tk()
root.title("Projector Plus")
root.configure(bg="Midnight Blue")
root.minsize(550, 550)

pro = Application(root)

root.mainloop()

然后,我需要創建可以在Ubuntu上安裝的文件(要在Ubuntu上創建可執行文件)。 在Windows中,使用.exe文件(使用cx-Freeze)非常容易做到這一點。 真的,我對Ubuntu和Shell文件的文件系統一無所知。

請幫助我了解這個問題。 我不知道如何輸入這件事。

基本上,要使文件在Unix系統中可執行,您只需要做一件事:允許它執行(非常令人驚訝;))。 為此,必須使用chmod命令,如下所示: chmod +x youfile.py +x添加執行權。

現在,您的系統將允許您執行該腳本,但是現在它只是一個簡單的文本文件... Ubuntu不知道他必須使用python命令來運行它,因此您會破壞包容性。 要解決此問題,我們使用“ shabang”行(有關更多信息,請參見Wikipedia頁面 ):在腳本的第一行,您必須輸入#! program_to_use #! program_to_use ,在您的情況下是python。 通常,我們利用env變量,並使用#! /usr/bin/env python #! /usr/bin/env python ,但是您也可以選擇#! /usr/bin/pythonX.X來選擇自己想要的python版本#! /usr/bin/pythonX.X #! /usr/bin/pythonX.X ,其中XX是python的版本。

1-在文件頂部添加以下行: #!/bin/env python (這可能會因您的ENV變量而異)

2-通過以下方式使文件可執行:

a-在Unix系統中,運行以下命令:

      `chmod +x myfile.py`

b-在Windows系統中,您可以使用http://www.py2exe.org/上的實用程序py2exe

有關更多信息,請參見:

https://docs.python.org/2/faq/windows.html#how-do-i-make-an-executable-from-a-python-script

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM