简体   繁体   English

带有用户包的 py2app

[英]py2app with user packages

I am working on a little cross-platform python project.我正在做一个小的跨平台 python 项目。 I want to associate file extensions with the program, so for OSX it seems the script must become an Application.我想将文件扩展名与程序相关联,因此对于 OSX,脚本似乎必须成为应用程序。 py2app seems like the perfect tool for the job, but I am having a lot of trouble finding good documentation on it. py2app 似乎是完成这项工作的完美工具,但我很难找到有关它的良好文档。

My python script relies on a package I wrote ( k.tk ) and symlinked to /Library/Python/2.7/site-packages/k/tk (the individual files are symlinked into the folder - the folder is not a symlink).我的 python 脚本依赖于我编写的包 ( k.tk ) 并k.tk链接到/Library/Python/2.7/site-packages/k/tk (单个文件被符号链接到文件夹中 - 该文件夹不是符号链接)。

This is the setup.py script I am using:这是我正在使用的 setup.py 脚本:

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

Usage:
    python setup.py py2app
"""

from setuptools import setup
import sys

APP = ['randomizer.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True, 'includes': ['k.tk']}

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

I am having two problems with my setup, both of which I have found a really ugly workaround for that I do not like.我的设置有两个问题,我都找到了一个我不喜欢的非常丑陋的解决方法。

First Problem - Aliased Build第一个问题 - 别名构建

The first problem occurs when building with python setup.py py2app -A .使用python setup.py py2app -A构建时会出现第一个问题。 This is supposed to build the application with links to the source files to make development easier.这应该是通过链接到源文件来构建应用程序,以使开发更容易。 When I try to run the resulting application, however, it says但是,当我尝试运行生成的应用程序时,它说

Import Error: No module named k.tk导入错误:没有名为 k.tk 的模块

The workaround for this is to append these lines to the python script:解决方法是将这些行附加到 python 脚本中:

import sys
sys.path.append("/Library/Python/2.7/site-packages/")

It seems like this implies that something is wrong with the python environment when the it is called from the application, but I don't know enough about py2app to know how to fix it.这似乎意味着当从应用程序调用 python 环境时,python 环境有问题,但我对 py2app 了解不够,不知道如何修复它。

Second Problem - Full Build第二个问题 - 完整构建

The second problem occurs when building with python setup.py py2app .使用python setup.py py2app构建时会出现第二个问题。 This triggers a regular build and copies all libraries into the application.这会触发常规构建并将所有库复制到应用程序中。 When I run this application, I get当我运行这个应用程序时,我得到

OSError: [Errno 20] Not a directory: '[path snipped]/application.app/Contents/Resources/lib/python2.7/site-packages.zip/k' OSError: [Errno 20] 不是目录: '[path snipped]/application.app/Contents/Resources/lib/python2.7/site-packages.zip/k'

The ugly workaround here was to open the application's files and extract site-packages.zip, into a folder called site-packages.zip.此处丑陋的解决方法是打开应用程序的文件并将 site-packages.zip 解压缩到名为 site-packages.zip 的文件夹中。 Now the path given in the error message is a directory, and the application runs fine.现在错误消息中给出的路径是一个目录,应用程序运行良好。

Does anyone know how to fix this?有谁知道如何解决这一问题? I am feeling pretty stumped, and the only documentation I can find is this , which seems extremely limited.我感到很困惑,我能找到的唯一文档是this ,这似乎非常有限。

Update更新

I tried copying /Library/Python/2.7/site-packages/k directory into the project directory.我尝试将/Library/Python/2.7/site-packages/k目录复制到项目目录中。 This fixed the Aliased Build, but unfortunately not the Full Build.这修复了别名构建,但不幸的是不是完整构建。

I also tried following the instructions in the Python Documentation ( here ) to properly package and install.我还尝试按照 Python 文档(此处)中的说明正确打包和安装。 This had the same effect.这具有相同的效果。 I am feeling quite frustrated;我感到很沮丧; why would it be trying to look in a zip file as if it were a folder?为什么它会试图查看 zip 文件,就好像它是一个文件夹一样?

Update更新

I ended up writing a build script that just overwrites the zip with a directory with the same name.我最终编写了一个构建脚本,该脚本只是用同名目录覆盖了 zip。 This solves the problem, but I am not happy with the solution.这解决了问题,但我对解决方案不满意。

I also have moved on to trying to build for Windows with py2exe.我也开始尝试使用 py2exe 为 Windows 构建。 In Windows, I experience the exact same problem!在 Windows 中,我遇到了完全相同的问题! This time library.zip is created and must be extracted for the program to run.这次 library.zip 已创建,必须解压缩才能运行程序。 Surely these programs do not just happen to have the same exact bug, but I cannot imagine why they both would incorrectly expect a zip file to be accessible the same way a folder is.当然,这些程序不只是碰巧有相同的错误,但我无法想象为什么他们都会错误地期望可以像文件夹一样访问 zip 文件。

py2app needs a separate Python installation other than system Python for the bundle to work (I suggest HomeBrew ). py2app需要一个单独的 Python 安装而不是系统 Python 才能使包工作(我建议使用HomeBrew )。 Also instead of includes you can try packages if k.tk is a package with more modules deeper in it.如果k.tk是一个包含更多模块的包,您也可以尝试packages而不是includes

If for some reason solution above does not work, try to add some const files that 3rd party packages open by open(*, 'r')如果由于某种原因上述解决方案不起作用,请尝试添加一些由open(*, 'r')第三方包打开的 const 文件

add what they want to open into data_files=[..]将他们想要打开的内容添加到 data_files=[..]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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