python - 如何正确安装 PyGObject?(OSX)
问题描述
我想运行现有的简单示例并使用 GStreamer 编写一些简单的代码 - 特别是使用它的 Python 绑定。我想安装软件包等来启用它。
这是一个例子。
http://brettviren.github.io/pygst-tutorial-org/pygst-tutorial.html
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
Gst.init(None)
# ...
my_playbin = Gst.ElementFactory.make("playbin", None)
assert my_playbin
print my_playbin
我无法让 PyGObject 工作,所以我被困在import gi
第一行之外,无法取得任何进展。
该平台是 MacOS 10.12.6 和 Python 3.6.5。
computer:Desktop me$ python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gi'
>>>
好的,让我们来 RTFM。
https://pygobject.readthedocs.io/en/latest/getting_started.html#macosx-getting-started
看起来很简单,这应该安装 PyGObject,对吧?
我已经安装了 Homebrew,但让我们再做一次以确保。
computer:Desktop me$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
[Snip for brevity]
==> Installation successful!
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh
computer:Desktop me$
好的,现在让我们安装 pygobject3 和 gtk+3
computer:Desktop me$ brew install pygobject3 gtk+3
Updating Homebrew...
Warning: pygobject3 3.32.1 is already installed and up-to-date
To reinstall 3.32.1, run `brew reinstall pygobject3`
Warning: gtk+3 3.24.8 is already installed and up-to-date
To reinstall 3.24.8, run `brew reinstall gtk+3`
computer:Desktop me$
现在让我们再次尝试 Python:
computer:Desktop me$ python3
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gi'
>>>
所以我们已经按照说明进行操作,我们仍然在没有任何功能的情况下开始。
在 brew 安装期间--with-python3
还尝试了各种选项。--without-python
computer:Desktop me$ brew install pygobject3 --with-python3 --without-python
Updating Homebrew...
[SNIP FOR BREVITY]
Error: invalid option: --with-python3
尽管在各种互联网线程中都提到了所有这些选项,但这些选项都是无效选项。
computer:Desktop me$ brew install pygobject3 --with-python@2 gtk+3
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.
[SNIP FOR BREVITY]
Error: invalid option: --with-python@2
有人可以告诉我我错过了什么吗?
解决方案
我知道这个答案有点晚了,但我弄清楚了为什么它不起作用,并为将来遇到它的人提供了一种解决方法。
Homebrew 将pygobject3
通过符号链接从包安装目录中安装,这意味着pygobject3
只能从 homebrew 安装的 python 二进制文件中访问。/usr/local/bin/python3
如果我在, 的安装位置使用 python 可执行文件brew install python
,pygobject3
导入就好了。
显然,这是次优的,我们希望转移pygobject3
到我们首选的 python 安装。
我是这样做的:
$ cd /usr/local/Cellar/pygobject3/3.34.0/lib/python3.7/site-packages/
$ sudo cp -rf * /usr/local/anaconda3/lib/python3.7/site-packages/
我现在可以导入和使用 gi:
(base) 2l4@mac105220:/usr/local/anaconda3/lib/python3.7/ > python
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import GLib
>>> loop = GLib.MainLoop()
>>> loop.run()
推荐阅读
- c# - 3D数组中每边的平均数
- python - 如果在位置找不到文件,则创建新文件
- c# - Xamarin,自动检测安装类型
- kotlin - Tornadofx 中 replaceWith() 后删除组件
- excel - 在 VBA 复制粘贴宏中使用 UDF
- python - 无法在 Pandas 中创建折线图
- html - iOS WKWebView HTML5 音频在手机锁定约 30 秒后停止
- amazon-web-services - Cloud Formation 中 Cognito 的 VerificationMessageTemplate 不起作用
- graph - Neo4J - 根据属性值从两个 MATCH 查询之一中获取结果
- javascript - 在 JavaScript 中从字符串中提取随机数