1

I just created an python script that solve me a problem i need but i want to convert this script to exe file to run it in any windows machine without need of install python on it I have search of how could i convert the py to exe and run it and i have found that i could use script called py2exe the problem here that i want to convert my file to exe and run it as a windows service continuously on the my PC.

Here is the my script:

import socket, sys, serial

HOST = ''   # Symbolic name, meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

# try:

#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    # print('Connected with {}:{}'.format(addr[0], addr[1]))
    str = conn.recv(100)
    n_str = str[8:]
    last_c = n_str.find('%')
    last_str = n_str[:last_c]
    final_str = last_str.replace('+',' ')[:-3]
    print(final_str)
    try:
        pole = serial.Serial('COM4')
        pole.write('                                          \r\n')
        pole.write(final_str+'\r\n')
        pole.close()
    except:
        print(Exception.message)



s.close()

Could i have some help here

Mostafa Mohamed
  • 816
  • 15
  • 39
  • Windows services have some special requirements. See https://stackoverflow.com/q/32404/291641 for an answer that gives the right way to do this. You cannot just make any old executable be a service. – patthoyts Feb 07 '16 at 14:00

1 Answers1

4

Python is an interpreted language, not a compiled one. As such, it needs its interpreter in order to be executed.

Bearing that in mind, you can use this: http://www.py2exe.org

More options given here: a good python to exe compiler?

or even better, in here: https://wiki.python.org/moin/DistributionUtilities

Pouria
  • 1,081
  • 9
  • 24
  • do you mean even if i convert it to exe i must install python to run it ? – Mostafa Mohamed Feb 07 '16 at 13:56
  • Either that, or the package must contain everything in which `.exe` is executed must contain everything the file needs to run. Nothing will be "embedded in the file". Well it theory, it can be embedded, but then you'll have a terribly large file. A good example of this are Apps in Mac. When you open the container, you can see everything in there! That's in fact why Python interpreter is pre-installed on every OS (except the good old Windows, of course)! – Pouria Feb 07 '16 at 14:15
  • Ok could you tell me how can i run that exe as a service please ? i updated my code – Mostafa Mohamed Feb 07 '16 at 14:41
  • I can, but Python documentations can do it in a much more coherent manner. Here is the most important part, though I suggest your read the entire document: https://docs.python.org/3.5/faq/windows.html#how-can-i-embed-python-into-a-windows-application – Pouria Feb 07 '16 at 14:51
  • @MostafaMohamed, if this answered your question, please mark it as such so it will be useful to other people too. :) Otherwise, please elaborate! – Pouria Feb 17 '16 at 14:06