好吧,我在 setuptools 源代码中进行了一些调查,这一切都归结为 setuptools 中的一个错误(easy_install.py):
# On Windows/wininst, add a .py extension and an .exe launcher
if group=='gui_scripts':
ext, launcher = '-script.pyw', 'gui.exe'
old = ['.pyw']
new_header = re.sub('(?i)python.exe','pythonw.exe',header)
else:
ext, launcher = '-script.py', 'cli.exe'
old = ['.py','.pyc','.pyo']
new_header = re.sub('(?i)pythonw.exe','python.exe',header)
if os.path.exists(new_header[2:-1]) or sys.platform!='win32':
hdr = new_header
else:
hdr = header
最后if
语句决定是否将 pythonw.exe 或 python.exe 的路径写入“frontend-script.pyw”的 shebang 中。由于此 shebang 由创建的 EXE 文件评估,因此有必要else
语句不被执行。问题是new_header[2:-1]
在我的例子中是“C:\Program Files (x86)\Python26\pythonw.exe”(带引号!),所以os.path.exists
说它不存在,因为引号。
我将尝试让 setuptools 开发人员纠正这个问题。剩下的问题是 pythonw.exe 的绝对路径。如果我创建 Windows setup/MSI 安装程序,shebang 将包含我的 pythonw.exe 路径(“C:\Program Files (x86)\Python26\pythonw.exe”),但用户可能已在“C:\Python26”中安装了 Python ”。报告这个问题后我会报告最终的解决方案。
我两年多前发布了这篇文章,很抱歉我还没有提供我的解决方案。不确定是否有更现代的解决方案(可能分发 http://packages.python.org/distribute/setuptools.html#developer-s-guide提供了一些东西),但这是我当时使用的(复制粘贴):
File dogsync-frontend-script.pyw
#!pythonw.exe
# This script will be executed by the primary Python version that is installed, which might as well be Python 3. But
# we want to execute it with the Python version that belongs to this script's path. So let's do a major hack:
import os
import sys
import subprocess
if sys.argv[-1] == "magic":
from dogsync_frontend.launcher import main
main()
else:
# The CPython folder hierarchy is assumed here (<installation>\pythonw.exe, <installation>\Scripts\<thisscript>)
subprocess.Popen([os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "pythonw.exe")),
__file__,
"magic"])
File dogsync-frontend.exe
自动复制自<python installation>\lib\site-packages\setuptools\gui.exe
(见下文)。该文件会自动执行脚本<name of EXE>-script.py[w]
如果我没记错的话。
File setup.py
from setuptools import __file__ as setupToolsFilename
if os.name == "nt":
# Use a customized (major hack) start script instead of the one that gets automatically created by setuptools
# when the "gui_scripts" parameter is used. This way, we don't need setuptools installed in order to run DogSync.
shutil.copy2(os.path.join(os.path.dirname(setupToolsFilename), "gui.exe"),
"build-environment/windows-scripts/dogsync-frontend.exe")
startScripts = dict(scripts = ["build-environment/windows-scripts/dogsync-frontend-script.pyw",
"build-environment/windows-scripts/dogsync-frontend.exe"])
else:
# For Linux, I don't have a solution to remove the runtime dependency on setuptools (yet)
startScripts = dict(entry_points = {"gui_scripts" : ['dogsync-frontend = dogsync_frontend.launcher:main']})
setup(<other options>,
**startScripts)
通过此设置,exe/pyw 文件将复制到<python installation>\Scripts
(在 Windows 上)并启动dogsync-frontend.exe
将在没有控制台的情况下运行 pyw 脚本。由于 setuptools 多年来没有得到任何更新,因此该解决方案仍然有效。