keyring module is not included while packaging with py2exe
Asked Answered
T

1

8

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")

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 code is :

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 code is:

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)
Top answered 8/11, 2013 at 5:31 Comment(3)
py2exe seems to not include the "platform" module (which is from the python standard lib). Try to manually add the module in your setup.py file, and see if it solves the issue. Also you may be using a old version of keyring : try updating your module.Ruin
did you recently update the keyring (like, before posting your question on SO) ? because I've look at the source code of ver 3.2.1 of keyring and it doesn't contain ./utils/platform.py (you can look at it here : source code ). I suspect there is a name-conflict between the local file and the standard lib module : try moving platform.py AND platform.pyc to a different folder.Ruin
@georgesl same error again. In platform_.py, you can see import platform in line 4, that is causing error. I tried with vault to store string, it also produced same error. Is there any way apart from using keyring.Top
I
7

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.

After doing that, however, you'll encounter another problem. Keyring loads its backend modules programmatically, so py2exe won't detect them.

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

The resulting app.exe will import and invoke keyring.

Indignation answered 2/12, 2013 at 14:12 Comment(4)
I am getting these error: Traceback (most recent call last): File "app.py", line 1, in <module> File "keyring_init_.pyc", line 12, in <module> File "keyring\core.pyc", line 14, in <module> File "keyring\backend.pyc", line 10, in <module> File "keyring\util\properties.pyc", line 1, in <module> File "collections.pyc", line 12, in <module> File "heapq.pyc", line 134, in <module> ImportError: No module named bisectTop
I had done pip unistall keyring. It removes keyring form python27 site-packages folder. Also I had tried with uninstalling the python.Top
That ImportError is in the standard library, and I didn't encounter that when I ran py2exe on my sample app using the recipe above. Now it seems like something else is broken in the Python environment (or maybe py2exe). Why it wouldn't include the bisect module, I don't know.Indignation
I found that adding package name keyring was sufficient; no .backends suffix needed.Parris

© 2022 - 2024 — McMap. All rights reserved.