python import seems to behave differently in mercurial_keyring.py file

我们两清 提交于 2019-12-01 05:44:50

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.

Mike

@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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!