一个奇怪的 import
错误阻止我安装 Mercurial 扩展。
我正在尝试运行 mercurial_keyring
扩展,这样我就不必每次在项目中使用 Mercurial 时都输入用户名和密码。
我正在使用Python 2.7.1。我使用 https://www.mercurial-scm.org/ 提供的二进制文件安装了 Mercurial。
我使用 pip
安装了 keyring
和 mercurial_keyring
。
我首先尝试通过将其添加到 ~/.hgrc
来添加扩展:
[extensions]
...
mercurial_keyring =
如安装说明中所示 此处。但是,我收到以下错误:
*** failed to import extension mercurial_keyring: No module named mercurial_keyring
根据相同的安装说明,我尝试将 Mercurial 直接指向 mercurial_keyring.py
文件,该文件有效。
[extensions]
...
hgext.mercurial_keyring = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mercurial_keyring.py
事情似乎正在进展。
但是,当我尝试执行任何需要我的密码的 Mercurial 命令以便将其保存在 keyring
时(例如 hg pull
, hg Push
)我收到错误
abort: No module named keyring!
最令人困惑的部分是,mercurial_keyring.py
第 28 行中有一个明确的内容
import keyring
,已解决,没有任何问题。事实上,任何导入密钥环
在类和方法外部都会成功,但在类和方法内部会失败!
为了彻底起见,我会提到这个错误出现在 get_http_password
方法的 PasswordStore
类中的 mercurial_keyring.py
中。以下是尝试
return keyring.get_password(...)
有什么想法吗?
我感觉我错过了一些明显的东西,但我花了很多时间试图弄清楚这一点,而谷歌到目前为止还没有特别有帮助。任何意见将不胜感激。
A bizarre import
error is preventing me from installing a mercurial extension.
I'm trying to get the mercurial_keyring
extension running so that I don't have to type in my user name and password every time I use mercurial for a project.
I'm using Python 2.7.1. I installed mercurial with the binary provided at https://www.mercurial-scm.org/.
I installed keyring
and mercurial_keyring
with pip
.
I first tried to add the extension by adding this to ~/.hgrc
:
[extensions]
...
mercurial_keyring =
as indicated in the installation instructions here. However, I got the following error:
*** failed to import extension mercurial_keyring: No module named mercurial_keyring
From the same installation instructions, I tried pointing mercurial directly to the mercurial_keyring.py
file, which worked.
[extensions]
...
hgext.mercurial_keyring = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mercurial_keyring.py
And things seemed to be moving along.
However, when I try to execute any mercurial commands requiring my password so that it will be saved by keyring
(e.g. hg pull
, hg push
) I get the error
abort: No module named keyring!
The most confusing part is that there is a clear
import keyring
in line 28 of mercurial_keyring.py
that is resolved without any problems. In fact, any import keyring
succeeds outside classes and methods and fails inside them!
Just for the sake of thoroughness, I'll mention that this error arises in the mercurial_keyring.py
in the PasswordStore
class in the get_http_password
method when the following is attempted
return keyring.get_password(...)
Any thoughts?
I have the feeling that I'm missing something obvious, but I've spent a good deal of time trying to figure this out and google has not been particularly helpful so far. Any input will be greatly appreciated.
发布评论
评论(7)
最有可能的是,
hg
使用系统 python (2.6) 运行,而不是您安装的 2.7 副本。尝试在 2.6 下安装
mercurial_keyring
和keyring
,看看是否可以按预期工作。Most likely,
hg
is running using the system python (2.6) rather than the copy of 2.7 you have installed.Try installing
mercurial_keyring
andkeyring
under 2.6, and see if that gets things working as expected.Mercurial 使用名为“demandimport”的功能,该功能会推迟模块的导入,直到第一次使用它们为止。所以,你的
不会在该行失败,但只有在第一次使用时才会失败(即)
Mercurial uses a feature called 'demandimport' which postpones the import of modules, until the first time they are used. So, your
won't fail at that line, but it will wail only when it's used first(i.e)
我遇到了同样的问题,并通过安装扩展来解决它:
sudo easy_install Mercurial_keyring
这会将其安装在 Mercurial 使用的同一 python 下。
I encountered the same issue and resolved it by installing extension with easy install:
sudo easy_install mercurial_keyring
This installs it under the same python that mercurial uses.
@ncoghlan 的答案是正确的(无论如何对我来说),但不完整,而且我没有足够的代表点来发表评论。 (Jeremy S,我认为这回答了您的问题。)
要安装特定版本的 Python,请使用以下修改:
而不是
Use
Same 适用于任何 easy_install 或其他 Python 命令。我从这里的 pip 示例中找到了这一点: 如何安装模块对特定版本使用 pip?
@ncoghlan's answer is right (for me, anyway), but incomplete and I don't have enough rep points to comment. (Jeremy S, I think this answers your question.)
To install for a specific version of Python, use the following modifications:
Instead of
Use
Same applies for any of the easy_install or other Python commands. I found this from an example for pip here: How to install a module use pip for specific version of?
我遇到了同样的问题。问题是我通过brew 安装了mercurial,并使用pip 安装了密钥环。
在brew中卸载mercurial,然后通过pip安装mercurial解决了我的问题。
I ran into same issue. The issue was I installed mercurial via brew and installed the keyring using pip.
Uninstalling the mercurial in brew and then installing mercurial via pip solved the issue for me.
方法中的导入在调用时进行评估,而顶级导入会立即进行评估。导入的行为可以修改,请查看 imp 和 site 模块以及 sys.path。可能发生的情况是,文件末尾的某些代码(象征性地,也可能是初始化时的函数调用等)意外修改了导入行为,或者为了防止和注意到无意的延迟导入 。
import
s in methods are evaluated when they're called, whereas top-level imports are evaluated immediately. The behavior of imports can be modified, have a look at the imp and site modules as well as sys.path. What is probably happening is that some code at the end of the file (figuratively, may also be a function call on initialization or so) modifies the import behavior by accident or to prevent and notice inadvertent lateimport
s.我的问题是我通过
macports
安装了Mercurial
,但通过pip
安装了扩展。为了解决这个问题,我还必须通过 macports 安装扩展。My issue was that I installed
Mercurial
viamacports
, but the extension viapip
. To solve it, I had to install the extension viamacports
as well.