使用 py2app 创建的 Python Mac 应用程序找不到 cvs 命令

发布于 12-03 01:51 字数 2391 浏览 2 评论 0原文

开始之前的一些系统/软件信息:

操作系统:Mac OS X 10.7.1

Python:Active Python 2.7.2.5

wxPython:wxPython2.9-osx-2.9.1.1-cocoa-py2.7

我有一个基于 wxpython 的小型 Mac 应用程序只是测试cvs和svn在Mac平台上的可用性。这是 Mac 应用程序所基于的 Python 代码:

import wx
import commands,os

ID_RUN_BUTTON=1
class Frame(wx.Frame):
    def __init__(self, parent, id, title):
          wx.Frame.__init__(self, parent, id, title, size=(100, 100),style=wx.MINIMIZE_BOX | wx.CLOSE_BOX)
          self.run_button=wx.Button(self,ID_RUN_BUTTON,label='Run')
          self.Bind(wx.EVT_BUTTON, self.OnRun,id=ID_RUN_BUTTON)
          self.Centre()
          self.Show()

    def OnRun(self,evt):
          home_dir=os.path.expanduser("~")
          a=commands.getoutput("cvs")
          b=commands.getoutput("svn help")
          f=open('%s/cvs_test' % (home_dir),'w')
          f.write(a)
          f.write('\n')
          f.write(b)
          f.close()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = Frame(None, -1, 'CVS Tester')
    app.MainLoop()

这是这个简单 GUI 的屏幕截图,其中有一个名为“运行”的按钮。

带有单个“运行”按钮的简单 cvs 测试应用程序的屏幕截图

按“运行”时,它会执行 OnRun 方法并它将两个命令“cvs”和“svn help”的输出保存到用户主目录中名为“cvs_test”的文件中。当我在命令行上使用 python 解释器运行此代码时,这两个命令的输出都被吐到一个文本文件中。 cvs 和 svn 命令均被识别,文件 cvs_test 中的输出符合预期。

现在的问题是,当我使用 py2app 使用以下脚本创建 Mac 应用程序时:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['cvs_test.py']
DATA_FILES = [('icons',['./icons/ark-2.png'])]
OPTIONS = {'iconfile': './icons/ark-2.icns'}

setup(app=APP,data_files=DATA_FILES,options={'py2app': OPTIONS},setup_requires=['py2app'])

Mac 应用程序创建得非常好。但是,当我打开 Mac 应用程序并点击“运行”按钮时,在它创建的 cvs_test 文件中,它显示:

sh:cvs:找不到命令

Mac 应用程序显然正在运行相同的内容脚本,但找不到 cvs 命令。

在我的主目录中,以下是 .profile 文件的内容:

export TERM="xterm"
export PATH='/Developer/usr/bin':$PATH
export PATH='/usr/local/bin':$PATH

我按照 Apple 论坛上发布的技巧添加了路径“/Developer/usr/bin”(单击此处)解决 OS X Lion 上的 cvs 问题。

是什么导致 python 脚本在从命令行运行时识别 cvs,而在作为编译的 Mac 应用程序运行相同的脚本时不识别它?

这个问题快要死我了。我的 bash 和 sh 从终端运行时都可以找到 cvs,但 Mac 应用程序不能。任何建议将不胜感激。

Some system/software info before getting started:

OS: Mac OS X 10.7.1

Python: Active Python 2.7.2.5

wxPython: wxPython2.9-osx-2.9.1.1-cocoa-py2.7

I have a small wxpython-based Mac app that just tests the availability of cvs and svn on a Mac platform. This is the python code on which the Mac app is based:

import wx
import commands,os

ID_RUN_BUTTON=1
class Frame(wx.Frame):
    def __init__(self, parent, id, title):
          wx.Frame.__init__(self, parent, id, title, size=(100, 100),style=wx.MINIMIZE_BOX | wx.CLOSE_BOX)
          self.run_button=wx.Button(self,ID_RUN_BUTTON,label='Run')
          self.Bind(wx.EVT_BUTTON, self.OnRun,id=ID_RUN_BUTTON)
          self.Centre()
          self.Show()

    def OnRun(self,evt):
          home_dir=os.path.expanduser("~")
          a=commands.getoutput("cvs")
          b=commands.getoutput("svn help")
          f=open('%s/cvs_test' % (home_dir),'w')
          f.write(a)
          f.write('\n')
          f.write(b)
          f.close()

if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = Frame(None, -1, 'CVS Tester')
    app.MainLoop()

Here is a screen shot of this simple GUI with one button called 'Run'.

screen shot of the simple cvs test app with a single 'Run' button

On pressing 'Run' it executes the OnRun method and it saves the output of the two commands 'cvs' and 'svn help' into a file called 'cvs_test' in the user's home directory. When I run this code using the python interpreter on command-line, the output of both the commands is spit into a text file. Both cvs and svn commands are recognized and the output in the file cvs_test is as expected.

Now the problem is when I create a Mac app using py2app with the following script:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['cvs_test.py']
DATA_FILES = [('icons',['./icons/ark-2.png'])]
OPTIONS = {'iconfile': './icons/ark-2.icns'}

setup(app=APP,data_files=DATA_FILES,options={'py2app': OPTIONS},setup_requires=['py2app'])

The Mac app is created absolutely fine. But, when I open the Mac app and hit the 'Run' button, in the cvs_test file it created, it says:

"sh: cvs: command not found"

The Mac app is obviously running the same script but it couldn't find the cvs command.

In my home directory, the following are the contents of my .profile file:

export TERM="xterm"
export PATH='/Developer/usr/bin':$PATH
export PATH='/usr/local/bin':$PATH

I added the path '/Developer/usr/bin' by following a trick posted on Apple forums (click here) to resolve the cvs issue on OS X Lion.

What causes the python script to identify cvs when run from command-line and not identify it when the same script is run as a compiled Mac app ?

This question is killing me. My bash and the sh both of them can find cvs when run from the terminal, but the Mac app can't. Any suggestions would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-12-10 01:51:30

当您从 Finder/Dock(即不是终端)运行 OSX GUI 应用程序时,不会启动新的 shell,因此不会读取您的 .profile。

您可以按照 Apple doc

您还可以设置环境(例如添加到路径)或在 python 脚本中显式使用 cvs 和 svn 的完整路径

When you run a OSX GUI app from Finder/Dock ie not the terminal a new shell is not started thus your .profile is not read.

You can set environment variables in ~/.MacOS/environment.plist as per Apple doc

You could also set the environment (e.g. add to the path) or explicitly use the full paths to cvs and svn in the python script

辞别 2024-12-10 01:51:30

尝试将以下内容添加

import os
print os.environ["PATH"]

到您的应用程序中。我怀疑它运行的路径与从命令行运行 Python 代码时的路径不同。

如果事实证明您的路径不是您所期望的,您可以让您的应用程序将 /usr/local/bin 附加到 os.enviorn["PATH"]。如果您的应用程序只需要这些工具,这可能是一个很好的解决方案。

Try adding the following:

import os
print os.environ["PATH"]

to your app. I suspect it's not running with the same path as when you run your Python code from the command line.

If it turns out that your path is not what you expected, you could have your app append /usr/local/bin to the os.enviorn["PATH"]. This could be a good solution if you just need these tools for your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文