Nicholas Shaffer
Nicholas Shaffer

Reputation: 80

EXE file for python scripts

i am using python 2.7 and py2exe to try and make an exe file for my script. but it is not going so well.. my file works perfectly until I add the py2exe commands what am I doin wrong here? I need to know how to write the setup function and call it so that python knows to create and EXE file not just a compiled .py. Also this is attempted using a windows operating system.

from time import strftime
import os.path
# setup.py
import py2exe

setup(console=["LogFile.py"])


def main():
    getTime()

def getTime():
    time = strftime("%Y-%m-%d %I:%M:%S")
    printTime(time)

def printTime(time):
    savePath = "C:\Users\Nicholas\Documents"
    logFile = "LogInLog.txt"
    files = open(os.path.join(savePath, logFile), "a+")
    openPosition = files.tell()
    files.write("A LogIn occured.")
    files.write(time)
    files.seek(openPosition)
    print(files.read())
    files.close()



main()

Upvotes: 1

Views: 240

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

It doesn't work that way

First, remove the setup line from your script. The setup script is a different script. Your script, fixed:

from time import strftime
import os.path

def main():
    getTime()

def getTime():
    time = strftime("%Y-%m-%d %I:%M:%S")
    printTime(time)

def printTime(time):
    savePath = r"C:\Users\Nicholas\Documents"
    logFile = "LogInLog.txt"
    files = open(os.path.join(savePath, logFile), "a+")
    openPosition = files.tell()
    files.write("A LogIn occured.")
    files.write(time)
    files.seek(openPosition)
    print(files.read())
    files.close()

Then create a file called setup.py

import py2exe
from distutils.core import setup
setup(console=["LogFile.py"])

Then type (in a command prompt, not from within python interpreter):

python setup.py py2exe

it creates the executable & aux files in dist subdir

After that go to dist

C:\DATA\jff\data\python\stackoverflow\dist>LogFile.exe
Traceback (most recent call last):
  File "LogFile.py", line 25, in <module>
  File "LogFile.py", line 6, in main
  File "LogFile.py", line 10, in getTime
  File "LogFile.py", line 15, in printTime
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Nicholas\\Documents\\LogInLog.txt'

crashing, normal I don't have your directories: it works!!

Upvotes: 1

pingze
pingze

Reputation: 1049

Look at this py2exe Tutorial.

Your mistakes are: 1. Missed from distutils.core import setup 2. Did not make a new file to use py2exe.

You need: 1. Remove import py2exeand setup(console=["LogFile.py"]) 2. create new file "psetup.py", with code bellow:

from distutils.core import setup
import py2exe
setup(console=["your_code_name.py"]) 

Upvotes: 1

Related Questions