我需要一个独立的Python解释器/编译器!

0 投票
6 回答
4871 浏览
提问于 2025-04-16 08:24

我需要一个可以把Python文件(.py或.pyw)转换成.exe格式的工具。

我遇到的工具只有:

- cx_freeze(不太好用)

- py2exe(太复杂了)

补充:上面这两个程序对我来说都很复杂,因为你需要创建很多设置文件,还要输入一堆参数和命令才能让它们工作。我发现了一个叫gui2exe.py的东西,但我似乎无法正确加载它……有什么想法吗?

我想要的是一个不需要通过Python命令行运行的程序。最好是一个独立的程序,你只需选择文件,选择输出格式(.exe),然后点击转换就行。不要太复杂,因为我刚开始学习。

我想要这个工具的原因是,我有一个程序,我的朋友想看看,但他不想下载Python来查看。而且我也不想让他能修改源代码。

有什么好的建议吗?

6 个回答

1

你可以看看 gui2exe,这是一个很不错的工具,可以帮助你更方便地使用 py2exe、pyinstaller 等程序。

2

一个选择是使用IronPython。IronPython是Python的一种变种,它专门为.NET平台设计,和微软的Visual Studio配合得非常好。

5

Pyinstaller可能会对你有帮助...

不过,py2exe其实也不复杂...

看看这个py2exe的示例设置(来自这里,不过是意大利语,所以我翻译了一下 http://bancaldo.wordpress.com/2010/05/13/python-py2exe-setup-py-sample/

#!/usr/bin/python

from distutils.core import setup
import py2exe, sys, wx, os

# Se eseguito senza argomenti, crea l'exe in quiet mode.
# If executed without args, it makes the exe in quiet mode
if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")

class FileBrowser(wx.FileDialog):
    def __init__(self):
        wildcard = "Python files (*.py)|*.py|" \
            "Tutti i files (*.*)|*.*"
        dialog = wx.FileDialog(None, "Choose the file", os.getcwd(),
            "", wildcard, wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            print(dialog.GetPath())
        self.file = dialog.GetPath()
        self.fin = open(self.file, 'r')
        dialog.Destroy()

class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # info di versione
        self.version = "1.0.0"
        self.company_name = "Bancaldo TM"
        self.copyright = "no copyright"
        self.name = "py2exe sample files"

manifest_template = '''
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
     type='win32'
     name='Microsoft.VC90.CRT'
     version='9.0.21022.8'
     processorArchitecture='*'
     publicKeyToken='1fc8b3b9a1e18e3b' />
    </dependentAssembly>
  </dependency>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
         type="win32"
         name="Microsoft.Windows.Common-Controls"
         version="6.0.0.0"
         processorArchitecture="*"
         publicKeyToken="6595b64144ccf1df"
         language="*" />
    </dependentAssembly>
  </dependency>
</assembly>
'''
# File Browser
app = wx.PySimpleApp()
fb = FileBrowser()
# Assegno il nome all'eseguibile di uscita
# Give the name at the exe file being created
textentry = wx.TextEntryDialog(None, "name file EXE?",'','')
if textentry.ShowModal() == wx.ID_OK:
    destname = textentry.GetValue()

RT_MANIFEST = 24

test_wx = Target(
    description = "A GUI app",
    script = fb.file,     # programma sorgente dal quale creiamo l'exe
                          # source from wich we create the exe
    other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="tried"))],
    icon_resources = [(1, "py.ico")],
#    dest_base = "prova_banco") # Nome file di destinazione
                                #Name Destination file
    dest_base = destname) # Nome file di destinazione

setup(
    data_files=["py.ico"],
    options = {"py2exe": {"compressed": 1,
                          "optimize": 2,
                          "ascii": 1,
                          "bundle_files": 1}},
    zipfile = None,
    windows = [test_wx],
    )

它还包含一个小的图形界面,可以帮助你选择文件;)

编辑:

这是一个更简单的示例,可能更有用 :)

from distutils.core import setup
import py2exe
setup(
    name = 'AppPyName',
    description = 'Python-based App',
    version = '1.0',
    windows = [{'script': 'Main.pyw'}],
    options = {'py2exe': {'bundle_files': 1,'packages':'encodings','includes': 'cairo, pango, pangocairo, atk, gobject',}},
    data_files=[ 'gui.glade',]
    zipfile = None, 
)

逐步教程:

1- 创建一个.py文件,命名为'hello.py',这个文件不需要图形界面。

2- 创建一个setup.py文件。

from distutils.core import setup
import py2exe

setup(console=['hello.py'])

注意使用'console'而不是'windows',因为你没有图形界面。

然后,把你的hello.py文件和setup.py文件放到Python目录下;接着打开命令提示符(cmd),当你到达正确的目录(通常是C:\python2x)时,输入:

python setup.py py2exe

你的exe文件会在dist目录下。如果缺少某些.dll文件,只需下载并放到Python目录下即可。

一旦你的程序变得更复杂,可能需要其他指令;看看我发布的另外两个setup.py示例。希望这能帮到你。

撰写回答