为什么我无法导入 pygtk?

发布于 2024-09-10 17:59:55 字数 366 浏览 7 评论 0原文

我按照 中的说明进行操作这篇文章。 一切都安装成功。但是,当我运行 python 时,我无法导入 pygtk。具体来说,它是这样写的:

>>>导入 pygtk\n “ImportError:没有名为 pygtk 的模块”

我猜我必须执行一些命令,例如 make 或其他命令,但我找不到任何地方说明要做什么。请帮助,我感到非常沮丧。

编辑:我可能应该提到我使用的是 Mac OS X

I followed the instructions in this post. Everything installed successfully. However, when I run python I cannot import pygtk. Specifically, it says this:

>>> import pygtk \n
“ImportError: No module named pygtk”

I'm guessing I have to do some commands like make or something, but I can't find anywhere where it says what to do. Please help, I am getting very frustrated.

Edit: I should probably mention I'm on Mac OS X

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

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

发布评论

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

评论(6

一抹苦笑 2024-09-17 17:59:55

你如何运行Python?它是 OSX 版本(/usr/bin/python)还是 MacPorts 版本(/opt/local/bin/python)附带的版本?

您链接到的页面包含使用 MacPorts 安装 pygtk 的说明。所以它应该与 python 安装一起运行。有关如何配置 PATH 变量以使用适当的 python 安装的帮助,请参阅 MacPorts wiki

编辑:尝试显式运行 macports python:“/opt/local/bin/python”,然后导入 pygtk。另外,检查文件系统上的 macports python site-packages 目录下是否存在 pygtk(通常类似于 /opt/local/lib/python2.5/site-packages)。

How are you running python? Is it the one that comes with OSX (/usr/bin/python) or the MacPorts version (/opt/local/bin/python)?

The page you linked to has the instructions for installing pygtk under using MacPorts. So it should run with that installation of python. See the MacPorts wiki for help on how to configure your PATH variable to use the appropriate python installation.

EDIT: Try running the macports python explicitly: "/opt/local/bin/python" and then import pygtk. Also, check under the macports python site-packages directory on the filesystem to see if pygtk exists there (usually something like /opt/local/lib/python2.5/site-packages).

窗影残 2024-09-17 17:59:55

如果您从虚拟环境运行 git mergetool,则 python 解释器无法找到 pygtk。修复 virtualenv 的 python 路径或首先停用 virtualenv。

If you are running git mergetool from virtual environment, then python interpreter cannot find pygtk. fix your python path for virtualenv or deactivate virtualenv first.

陈独秀 2024-09-17 17:59:55

下面对我有用 - 假设你安装了 HomeBrew

我试图安装 meld 并遇到相同的错误 - 这修复了它

brew install python

Below worked for me - assumes you have HomeBrew installed

I was trying to install meld and was getting the same error - this fixed it

brew install python
回忆追雨的时光 2024-09-17 17:59:55

您可能已经单独安装了 pip(而不是通过 macports)。因此,您的软件包安装在 macports 安装的 python 无法读取的位置。例如,在我的 OS X 中,以下代码可以工作:

[user]$ /usr/bin/python
>>> import pip
>>> for package in pip.get_installed_distributions():
>>>     print package, package.location

但是如果我启动 /opt/local/bin/python (这是我的默认 python)并说“import pip”,那么它会给出一个导入错误,指出没有名为的模块点。

可能有两种方法适合您:

1) 使用 macports 安装 pip,并将其设置为默认值 (port install pip)。使用此 pip 再次安装 pygtk

2) 使用 /usr/bin/python 显式启动 python,并在那里编写代码。

可能有一种方法可以让 /opt python (来自 macports)读取非 macports pip 安装的模块,但我不知道。

It is likely that you've installed pip separately (rather than through macports). So, your packages are being installed in a location that is not readable by macports-installed python. For example, in my OS X, the following code works:

[user]$ /usr/bin/python
>>> import pip
>>> for package in pip.get_installed_distributions():
>>>     print package, package.location

But if I start /opt/local/bin/python (which is my default python) and say "import pip", then it gives an importerror stating there is no module named pip.

There might be two things that work for you:

1) Install pip using macports, and set that as your default (port install pip). Install pygtk again with this pip

2) Launch python explicitly with /usr/bin/python, and write your codes there.

There may be a way to have the /opt python (from macports) read modules installed by non-macports pip, but I am not aware of it.

醉生梦死 2024-09-17 17:59:55
a> pip install pygtk - (windows only), 
b> brew install python

不知道为什么,第一个选项说它只适用于 Windows。
下面对我来说工作得很好。

a> pip install pygtk - (windows only), 
b> brew install python

Not sure why, first options is saying its only works on windows.
Below is working fine for me.

没︽人懂的悲伤 2024-09-17 17:59:55

对这个问题的一个非常普遍的看法,如果 python 抱怨找不到我知道存在的模块,我就会这样做:
(这是非常普遍而不是基本的东西,所以如果这是你在发布之前就已经尝试过的东西,我深表歉意......在这种情况下,我希望它对其他人有用)

1:转到 python 安装目录并制作确保该模块确实存在(或者:弄清楚它到底在哪里——我有一些模块是项目的一部分,因此不在主目录中)。 ...有时这会发现该模块实际上并未安装,尽管它看起来像是安装了)

2:确保您写得正确(大写/小写字母可能是令人沮丧的根源 - 导入语句需要反映模块的目录名称)

3:如果它不在 python 路径中,则设置 $PYTHONPATH 环境变量或在脚本开头放置类似的内容将有所帮助:(

import sys
sys.path.append('\\path\\to_the_directory\\containing_themodule')

需要双斜杠以确保它们不是读作特殊字符)
在此示例中,pytk 将位于 \path\to_the_directory\containing_themodule\pytk' 中。

A very general view on the problem, this is what I do if python complains about not finding a module that I know exists:
(This is very general rather basic stuff, so apologies if this is stuff that you already tried even before posting here... in that case I hope it'll be useful to someone else)

1: Go to the python installation directory and make sure the module is actually there (or: figure out where exactly it is -- I have some modules that are part of a project, and thus not in the main directory). ... sometimes this will uncover that the module is not actually installed although it looked like it was)

2: make sure you're writing it correct (capital/lowercase letters are a likely source of frustration -- the import statement needs to reflect the module's directory name)

3: if it isn't located in the python path, either setting the $PYTHONPATH environment variable or putting something like this at the beginning of your script will help:

import sys
sys.path.append('\\path\\to_the_directory\\containing_themodule')

(double slashes required to make sure they're not read as special characters)
in this example, pytk would be in \path\to_the_directory\containing_themodule\pytk'.

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