简体   繁体   English

带看门狗的py2app ImportError

[英]py2app ImportError with watchdog

I am attempting to use py2app to bundle a small Python app that I've made in Python 2.7 on Mac. 我试图使用py2app捆绑一个我在Mac上用Python 2.7制作的小型Python应用程序。 My app uses the Watchdog library , which is imported at the top of my main file: 我的应用程序使用Watchdog库 ,它在我的主文件顶部导入:

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

When running my program, these import statements work just fine, and the program works as expected. 运行我的程序时,这些import语句运行正常,程序按预期工作。 However, after running py2app, launching the bundled application generates the following error: 但是,运行py2app后,启动捆绑的应用程序会生成以下错误:

ImportError: No module named 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 起初我认为这与嵌套在watchdogobservers模块有关,但为了测试,我添加了这一行

import watchdog

to the top of my program, and then upon running the app, got the error 到我的程序的顶部,然后在运行应用程序时,得到了错误

ImportError: No module named watchdog

so it seems that it actually can't find the watchdog package, for some reason. 所以看起来它实际上找不到watchdog包,出于某种原因。

I tried manually adding the watchdog package using py2app's --packages option: 我尝试使用py2app的--packages选项手动添加watchdog包:

$ python setup.py py2app --packages watchdog

but it had no effect. 但它没有效果。

My unbundled Python program runs just fine from the command line; 我的非捆绑Python程序从命令行运行得很好; other downloaded modules I've imported are giving no errors; 我导入的其他下载模块没有错误; and I have successfully bundled a simple "Hello World!" 我已成功捆绑了一个简单的“Hello World!” app using py2app, so I believe my setup is correct. 应用程序使用py2app,所以我相信我的设置是正确的。

But I'm kind of out of ideas for how to get py2app to find the watchdog package. 但我对如何让py2app找到watchdog包有点想法。 Any ideas or help would be greatly appreciated. 任何想法或帮助将不胜感激。

Edit: Here is the text of my setup.py , as generated by py2applet. 编辑:这是我的setup.py的文本,由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'],
)

Try manually including the desired packages in the setup.py file: 尝试在setup.py文件中手动包含所需的包:

from setuptools import setup

APP = ['watcher.py']
DATA_FILES = []
PKGS = ['watchdog', /*whatever other packages you want to include*/]
OPTIONS = {
    'argv_emulation': True,
    'packages' : PKGS,
}

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

I had installed watchdog 0.5.4, a very old version as it turns out, and got the same error. 我已经安装了看门狗0.5.4,这是一个非常老的版本,结果却出现了同样的错误。 The error was fixed after upgrading it to 0.8.3: 将其升级到0.8.3后修复了错误:

pip install watchdog --upgrade

Your problem generally indicates that the package (in your case "watchdog", or one of its dependencies) isn't installed, or at least not in a location that py2app expects to find packages. 您的问题通常表明软件包(在您的情况下是“看门狗”或其依赖项之一)未安装,或者至少不在py2app期望查找软件包的位置。

Do you use the same python command for running py2app as for running the script from the command-line? 您是否使用相同的python命令运行py2app以及从命令行运行脚本? What is the message of the ImportError you're getting (both when importing "watchdog" and importing "watchdog.observers"? 你得到的ImportError的消息是什么(导入“看门狗”和导入“watchdog.observers”时?

The (way too long) output of py2app should also mention that it cannot find some packages, and which ones. py2app的(太长)输出也应该提到它找不到一些包,哪些包。

As alluded to in one of the answers py2app does not seem to search the same set of paths that are used by the python interpreter, so you need to copy the python library to one of those locations. 正如其中一个答案所提到的,py2app似乎没有搜索python解释器使用的同一组路径,因此您需要将python库复制到其中一个位置。

For example I've got the MacPorts version of Python installed and found that when I had a module installed in /Library/Python/2.7/site-packages/ py2app wasn't finding it, but it would find it when I copied it into /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. 例如,我安装了MacPorts版本的Python,发现当我在/Library/Python/2.7/site-packages/中安装了一个模块时,py2app没有找到它,但是当我将它复制到/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages。 So to copy it over run : 所以要在运行中复制它:

sudo cp /Library/Python/2.7/site-packages/thatmodule.so /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

Then run the py2applet script again and build the app to check. 然后再次运行py2applet脚本并构建应用程序以进行检查。 If it's elsewhere you can do a search for all site-packages locations using Spotlight's command line interface: 如果它在其他地方,您可以使用Spotlight的命令行界面搜索所有site-packages位置:

mdfind -name site-packages

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

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