1

For an example I have created welcomeGUI.py file with has a simple GUI as follows using Tkinter.

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()

Then I need to create this file that can be installed on Ubuntu(To create an executable file on Ubuntu). In Windows this is done very easily with .exe file(Using cx-Freeze). Really I have no idea about the file system of Ubuntu and shell files.

Please help me to get an idea about this problem. I don't have any idea how to enter this matter.

RYJ
  • 439
  • 2
  • 16
  • 25

2 Answers2

3

Basically, to make a file executable in Unix systems, you just have to do one thing: allow it to be executed (very surprising ;) ). To do it, you must use the chmod command as follows: chmod +x youfile.py. The +x add the right to be executed.

Now, your system will allow you to execute the script, but for now it's just a simple text file... Ubuntu doesn't know that he must use the python command to run it, so you'll have undermined comportment. To resolve this, we use the sha-bang line (for more information, see the wikipedia page): at the first line of your script, you must write #! program_to_use, in your case it's python. Generally, we take benefit of the env variables, and use #! /usr/bin/env python, but you can also choose yourself the version of python you want, doing #! /usr/bin/pythonX.X where X.X is the version of python.

Spirine
  • 1,837
  • 1
  • 16
  • 28
  • This works well in Ubuntu. But what I should do if I used some codes with OpenCV in my welcomeGUI.py and run this code on Raspberry Pi( without installing OpenCV in Raspberry Pi ) ? Should I install OpenCV in Raspberry Pi or is there any alternative ways to do that task ? – RYJ May 10 '15 at 10:36
  • I've never used OpenCV, but you could either install it using pip, if your raspberry is connected to the internet, or try to create a standalone python (see [here](https://github.com/pyinstaller/pyinstaller/wiki) for example). Finally, you can try to pick up the openCV files (in /use/lib maybe) and copy them with your executable: it may work. – Spirine May 10 '15 at 10:54
  • the notion of executable(main meaning) has nothing to do with Unix. it is only a Turing machine code(aka machine code) that is to be run DIRECTLY on a universal Turing Machine which we modernly call computer(it's actually processor). Everything that really executes does so in a machine code. +x permission on Linux has nothing to do with it: a perfectly readable bash text script should be made runnable and is obviously not a machine executable. – iantonuk Oct 05 '16 at 02:59
0

1 - Add on the top of your file this line: #!/bin/env python (This may differ depending on your ENV variable)

2 - make your file executable by:

a - In Unix system, run the following command:

      `chmod +x myfile.py`

b - In Windows System, you can use the utility py2exe found on http://www.py2exe.org/

more on this can be found at:

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

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • This works. Can I do the same way for any kind of python files ? I mean If I used OpenCV in my .py file and if I run this code a machine with out having installation of OpenCV. – RYJ May 10 '15 at 10:42
  • Yes it will, as stated on the tutorial page of py2exe: "py2exe turns Python programs into packages that can be run on other Windows computers without needing to install Python on those computers. Python is needed on the computer where py2exe itself is run because py2exe is a Python program and it includes parts of Python in the package that is built. " – Iron Fist May 10 '15 at 10:55