开发者

python import seems to behave differently in mercurial_keyring.py file

开发者 https://www.devze.com 2023-02-14 16:13 出处:网络
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 ev

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 po开发者_如何学编程inting 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.


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 and keyring under 2.6, and see if that gets things working as expected.


Mercurial uses a feature called 'demandimport' which postpones the import of modules, until the first time they are used. So, your

import keyring

won't fail at that line, but it will wail only when it's used first(i.e)

return keyring.get_password(...)


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'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

easy_install keyring

Use

easy_install-2.6 keyring

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?


imports 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 late imports.


My issue was that I installed Mercurial via macports, but the extension via pip. To solve it, I had to install the extension via macports as well.

sudo port install py-keyring py-mercurial_keyring


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.

pip install mercurial
0

精彩评论

暂无评论...
验证码 换一张
取 消