1

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()
1
  • I have created a setup file for this program but I can not get past the py2exe command to execute I keep getting an invalid syntax error Commented Aug 21, 2016 at 20:15

2 Answers 2

1

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!!

Sign up to request clarification or add additional context in comments.

12 Comments

where am I typing Then type: python setup.py py2exe
Where am I supposed to be typing and running the setup.py file? it is not working for me I keep receiving symtax error : invalid syntax
this is what I get back from using the command C:\python27\setup.py\py2exe Volume in drive C has no label. Volume Serial Number is 4AC8-FD98 Directory of C:\python27\setup.py File Not Found C:\Users\Nicholas>dir C:\python27\setup.py Volume in drive C has no label. Volume Serial Number is 4AC8-FD98 Directory of C:\python27 08/21/2016 04:31 PM 88 setup.py 1 File(s) 88 bytes 0 Dir(s) 385,825,923,072 bytes free C:\Users\Nicholas>
Microsoft Windows [Version 10.0.10586] (c) 2015 Microsoft Corporation. All rights reserved. C:\Users\Nicholas>dir C:\python27\setup.py\py2exe Volume in drive C has no label. Volume Serial Number is 4AC8-FD98 Directory of C:\python27\setup.py File Not Found C:\Users\Nicholas>
something is reaaaally wrong on your setup. You seem to have a directory called setup.py in your python installation! Delete it. Work in another directory. I'm sorry, you won't have much more info with the tutorial posted in the above (fine) answer.
|
1

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"]) 

2 Comments

I have read the tutorial for py2exe it doesn't make sense to me. I've tried what they say to do on there and it doesn't seem to work. But I can see that's because I was typing within my pyscript. I have the setup file created but now what do I do? I type python setup.py py2exe but where how do I get the setup script to run and make my exe file
That is a different tutorial then I have read previously helps me very much thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.