与py2exe打包时不包含keyring模块 [英] keyring module is not included while packaging with py2exe
问题描述
我正在Windows上使用 python 2.7和keyring-3.2.1 制作应用.在eclipse上的python代码中,我使用
I am making an app using python 2.7 on windows and keyring-3.2.1 . In my python code on eclipse, I used
import keyring
keyring.set_password("service","jsonkey",json_res)
json_res= keyring.get_password("service","jsonkey")
工作正常,因为我将json响应存储在密钥环中.但是,当我使用py2exe将python代码转换为exe时,在制作dist时会显示导入错误密钥环.请建议如何在py2exe中包含密钥环.
is working fine as I am storing json response in keyring. But, when I converted python code into exe by using py2exe, it shows import error keyring while making dist. Please suggest how to include keyring in py2exe.
Traceback (most recent call last):
File "APP.py", line 8, in <module>
File "keyring\__init__.pyc", line 12, in <module>
File "keyring\core.pyc", line 15, in <module>
File "keyring\util\platform_.pyc", line 4, in <module>
File "keyring\util\platform.pyc", line 29, in <module>
AttributeError: 'module' object has no attribute 'system'
platform_.py代码为:
from __future__ import absolute_import
import os
import platform
def _data_root_Windows():
try:
root = os.environ['LOCALAPPDATA']
except KeyError:
# Windows XP
root = os.path.join(os.environ['USERPROFILE'], 'Local Settings')
return os.path.join(root, 'Python Keyring')
def _data_root_Linux():
"""
Use freedesktop.org Base Dir Specfication to determine storage
location.
"""
fallback = os.path.expanduser('~/.local/share')
root = os.environ.get('XDG_DATA_HOME', None) or fallback
return os.path.join(root, 'python_keyring')
# by default, use Unix convention
data_root = globals().get('_data_root_' + platform.system(), _data_root_Linux)
platform.py代码为:
import os
import sys
# While we support Python 2.4, use a convoluted technique to import
# platform from the stdlib.
# With Python 2.5 or later, just do "from __future__ import absolute_import"
# and "import platform"
exec('__import__("platform", globals=dict())')
platform = sys.modules['platform']
def _data_root_Windows():
try:
root = os.environ['LOCALAPPDATA']
except KeyError:
# Windows XP
root = os.path.join(os.environ['USERPROFILE'], 'Local Settings')
return os.path.join(root, 'Python Keyring')
def _data_root_Linux():
"""
Use freedesktop.org Base Dir Specfication to determine storage
location.
"""
fallback = os.path.expanduser('~/.local/share')
root = os.environ.get('XDG_DATA_HOME', None) or fallback
return os.path.join(root, 'python_keyring')
# by default, use Unix convention
data_root = globals().get('_data_root_' + platform.system(), _data_root_Linux)
推荐答案
您报告的问题是由于环境中包含无效模块而引起的,这可能是由于一个版本的密钥环安装不正确而导致的.您将要确保已删除旧版本的密匙环的残留物.特别要确保您的网站包中没有名为keyring \ util \ platform.*的文件.
The issue you're reporting is due to an environment that contains invalid modules, perhaps from an improper installation of one version of keyring over another. You will want to ensure that you've removed remnants of the older version of keyring. In particular, make sure there's no file called keyring\util\platform.* in your site-packages.
但是,这样做之后,您将遇到另一个问题.密钥环以编程方式加载其后端模块 ,因此py2exe不会检测到它们.
After doing that, however, you'll encounter another problem. Keyring loads its backend modules programmatically, so py2exe won't detect them.
要解决此问题,您需要在py2exe选项中添加一个"packages"声明,以专门包含keyring.backends
软件包.我使用Python 2.7调用了以下setup.py
脚本,将"app.py"(导入密钥环)转换为exe:
To work around that, you'll want to add a 'packages' declaration to your py2exe options to specifically include the keyring.backends
package. I invoked the following setup.py
script with Python 2.7 to convert 'app.py' (which imports keyring) to an exe:
from distutils.core import setup
import py2exe
setup(
console=['app.py'],
options=dict(py2exe=dict(
packages='keyring.backends',
)),
)
生成的app.exe将导入并调用密钥环.
The resulting app.exe will import and invoke keyring.
这篇关于与py2exe打包时不包含keyring模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!