简体   繁体   中英

Help converting .py to .exe, using py2exe

The script I ran

# p2e_simple_con.py
# very simple script to make an executable file with py2exe
# put this script and your code script into the same folder
# run p2e_simple_con.py
# it will create a subfolder 'dist' where your exe file is in
# has the same name as the script_file with extension exe
# (the other subfolder 'build' can be deleted)
# note: with console code put a wait line at the end

from distutils.core import setup
import py2exe
import sys

sys.argv.append("py2exe")
sys.argv.append("-q")

# this is your code script file, change it as needed
# use it in the working directory or give full path
script_file = 'DateTime_Test1.py'

# create a single .exe file and use compression
# (the .exe file is 30% larger with no compression)
setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': 1,}},
    zipfile = None,
    # replace console with windows for a GUI program
    console = [{'script': script_file}]
)

I tried this but got the following error

C:\Python26\Lib\distutils\dist.py:266: UserWarning: Unknown distribution option: 'console'
  warnings.warn(msg)
C:\Python26\Lib\distutils\dist.py:266: UserWarning: Unknown distribution option: 'zipfile'
  warnings.warn(msg)
usage: py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: py2exe.py --help [cmd1 cmd2 ...]
   or: py2exe.py --help-commands
   or: py2exe.py cmd --help

error: invalid command 'py2exe'

The lines:

sys.argv.append("py2exe")
sys.argv.append("-q")

seem "unusual" and likely to be causing that "invalid command" message. I'd be curious to know the rationale for having them.

Update

A common convention is to name the script setup.py (although in this case it's named p2e_simple_con.py it seems). The setup function in it parses the command line, and usually you expect to run it with a command such as:

python setup.py py2exe

where py2exe is a command that the py2exe package understands and then builds the EXE file. So as long as the py2exe package is installed, that command should be accepted.

The line sys.argv.append("py2exe") adds that command to the command-line, so in your case you should just be able to type:

python p2e_simple_con.py

to build the exe. However, if you haven't installed the py2exe package, then the base distutils wouldn't recognise the py2exe command. But in that case, I'd expect you to get an exception for the line import py2exe . So, I'm not sure what's going on.

What is the command you used to run the setup program?

The problem might be that py2exe is for python 3,

the one for python 2 seems to be py2exe2msi

I'm also getting a similar error in python 2, trying to do code that was written for python 3 version.

This is just an idea (not really an answer but I can't comment).

Since it seems from the error message that there is a parameter given to py2exe that it doesn't accept, I'd guess it's the -q parameter. So try removing the line sys.argv.append("-q") and run the scrip then.

I haven't used py2exe myself so I can't really test this. It's just an idea.

The two warnings are due to an errant comma inside the options dictionary, after the final value. You want to have

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': 1}},
    zipfile = None,
    # replace console with windows for a GUI program
    console = [{'script': script_file}]
)

Try and see if that fixes your error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM