使用 easy_install 在 Cocoa 应用程序中捆绑 python 工具

发布于 2024-12-27 03:29:22 字数 301 浏览 4 评论 0原文

我的 Cocoa 应用程序需要使用 easy_install 在用户系统上安装 Python 命令行工具。理想情况下,我希望将一个 bash 文件与我的应用程序捆绑在一起,然后我可以运行它。但据我所知这是不可能的,因为包安装在Python的“site-packages”目录中。

有没有办法创建这些文件的“包”? 如果没有,我应该如何运行 easy_install 安装?我想将 .pkg 文件与我的应用程序捆绑在一起,然后在必要时可以打开该文件,但我无法让此安装包仅运行脚本。

如果您有解决此问题的想法,我会很高兴。

亲切的问候, 法比安

My Cocoa app requires a Python command line tool to be installed on the user's system using easy_install. Ideally, I'd want to bundle one bash file with my app which I can then run. But as far as I know this is not possible, because packages are installed in the "site-packages" directory of Python.

Is there a way to create a "package" of those files?
If not, how should I run the easy_install installation? I wanted to bundle a .pkg file with my app which I can then open if necessary, but I wasn't able to let this installation package run a script only.

If you have ideas on how to fix this, I'd be glad.

Kind regards,
Fabian

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

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

发布评论

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

评论(2

耳根太软 2025-01-03 03:29:22

如果您可以将命令行工具与您的应用程序一起提供,并且只有您的应用程序将使用它(而不是用户直接使用该工具),则您可以直接在您的应用程序中包含并使用命令行工具,如下所示:

  • 将命令行工具存储在应用程序目录中的某个位置。
  • 设置Python模块搜索路径,以便它查找您需要的模块。

设置相对于 Python 程序的 Python 模块搜索路径可以使用

import sys
import os.path as path

sys.path.append(path.join(path.dirname(__file__),
    '<relative path between this program and the command line tool module>'))

import <command line tool module>

相对路径可以使用 .. 父目录约定编写:这适用于 Unix(包括Mac OS X)和 Windows。

PS:如果很多程序需要访问命令行工具模块,您可以:

  • 要么将上述代码放在每个程序中,

  • 或者,如果您想要更容易维护的东西,您可以创建自己的模块 my_runner 并将其用作代理:您的模块 my_runner 将导入所有原始的 runner 功能如下所以:

    <块引用>

    导入系统
    导入 os.path 作为路径
    
    sys.path.append(path.join(path.dirname(__file__),
        '<该程序与原始ino模块之间的相对路径>'))
    
    从 ino.runner 导入 *
    

    然后,您可以通过代理在所有程序中使用原始的运行程序模块,只需“正常”导入您自己的 my_runner 模块即可:

    <块引用>

    from my_runner 导入 main
    

PPS:如果您想要一个更健壮的路径,即使工作目录稍后发生更改也能正常工作,可以使用以下命令将潜在的本地路径转换为绝对路径:path.join(path.绝对路径(路径.dirname(__file__)),...

If you can ship the command line tool with your application, and if only your application will be using it (instead of the tool being used directly by the user), you can directly include and use the command line tool with your application like so:

  • Store the command line tool somewhere in your application directory.
  • Set the Python module search path so that it looks for the module you need.

Setting the Python module search path relative to a Python program can be done with

import sys
import os.path as path

sys.path.append(path.join(path.dirname(__file__),
    '<relative path between this program and the command line tool module>'))

import <command line tool module>

The relative path can be written with the .. parent directory convention: this works both on Unix (including Mac OS X) and Windows.

PS: If many programs need to access the command line tool module, you can:

  • Either put the above code in each of your programs,

  • or, if you want something easier to maintain, you can create your own module my_runner and use it as a proxy: your module my_runner would import all the original runner functions like so:

    import sys
    import os.path as path
    
    sys.path.append(path.join(path.dirname(__file__),
        '<relative path between this program and the original ino module>'))
    
    from ino.runner import *
    

    You can then use the original runner module in all your programs through your proxy, by simply doing "normal" imports of your own my_runnermodule:

    from my_runner import main
    

PPS: If you want a more robust path that works even if the working directory later changes, it is possible to convert a potentially local path to an absolute path with: path.join(path.abspath(path.dirname(__file__)),….

冷清清 2025-01-03 03:29:22

感谢 EOL,我现在可以使用它了。该解决方案涉及下面列出的几个步骤。

  1. 通过打包 .egg 文件并像这样引用它们来删除依赖项:

    导入系统
    导入 os.path 作为路径
    sys.path.append(path.join(path.dirname(__file__), '<.egg 文件的相对路径>'))
    
    从依赖项导入一些东西
    
  2. 从现在独立的 python 构建单个 .egg 文件使用 setup.py

    的模块使用

  3. 引用生成的 <命令行工具中的 code>.egg 文件,如下所示1.
  4. 在 Cocoa 应用程序中捆绑单个 .egg 文件和命令行工具,

我希望这对某人有用。

Thanks to EOL I got it working now. The solution involved several steps listed below.

  1. Remove dependencies by packaging the .egg files and referencing them like this:

    import sys
    import os.path as path
    sys.path.append(path.join(path.dirname(__file__), '<relative path to .egg file>'))
    
    from dependency import something
    
  2. Build a single .egg file from the now independent python module using setup.py

  3. Reference the resulting .egg file in the command line tool like in 1.
  4. Bundle the single .egg file and the command line tool in the Cocoa app

I hope this will be useful to someone.

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