本文介绍了带看门狗的 py2app 导入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 py2app 来捆绑我在 Mac 上的 Python 2.7 中制作的小型 Python 应用程序.我的应用程序使用 Watchdog 库,它在我的主文件顶部导入:

I am attempting to use py2app to bundle a small Python app that I've made in Python 2.7 on Mac. My app uses the Watchdog library, which is imported at the top of my main file:

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

运行我的程序时,这些导入语句工作正常,程序按预期工作.但是,运行py2app后,启动捆绑的应用程序会产生如下错误:

When running my program, these import statements work just fine, and the program works as expected. However, after running py2app, launching the bundled application generates the following error:

ImportError: No module named watchdog.observers

起初我认为这是与嵌套在 watchdog 中的 observers 模块有关,但为了测试这一点,我添加了该行

At first I thought it was something to do with the observers module being nested inside watchdog, but to test that, I added the line

import watchdog

到我的程序的顶部,然后在运行应用程序时出现错误

to the top of my program, and then upon running the app, got the error

ImportError: No module named watchdog

所以它似乎由于某种原因实际上找不到 watchdog 包.

so it seems that it actually can't find the watchdog package, for some reason.

我尝试使用 py2app 的 --packages 选项手动添加 watchdog 包:

I tried manually adding the watchdog package using py2app's --packages option:

$ python setup.py py2app --packages watchdog

但是没有效果.

我的未捆绑的 Python 程序在命令行中运行得很好;我导入的其他下载模块没有错误;我已经成功捆绑了一个简单的Hello World!"应用程序使用 py2app,所以我相信我的设置是正确的.

My unbundled Python program runs just fine from the command line; other downloaded modules I've imported are giving no errors; and I have successfully bundled a simple "Hello World!" app using py2app, so I believe my setup is correct.

但我对如何让 py2app 找到 watchdog 包有点想法.任何想法或帮助将不胜感激.

But I'm kind of out of ideas for how to get py2app to find the watchdog package. Any ideas or help would be greatly appreciated.

这是我的 setup.py 的文本,由 py2applet 生成.我没有修改过.

Here is the text of my setup.py, as generated by py2applet. I haven't modified it.

from setuptools import setup

APP = ['watcher.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}

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

推荐答案

我已经安装了 watchdog 0.5.4,事实证明这是一个非常旧的版本,并且遇到了同样的错误.将其升级到 0.8.3 后修复了该错误:

I had installed watchdog 0.5.4, a very old version as it turns out, and got the same error. The error was fixed after upgrading it to 0.8.3:

pip install watchdog --upgrade

这篇关于带看门狗的 py2app 导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:38