Python shove module not working after py2app build on mac - python

I am using the shove module for object persistence. It works fine but when I attempt to build the app using py2app, it ceases to work. The following code works normally except after freezing using py2app:
import os
from shove import Shove
if __name__ == '__main__':
home=os.path.expanduser("~")
path = os.path.join(home, 'testdb')
uri = 'file://{0}'.format(path)
print path
print uri
db = Shove(uri)
print db.keys()
db['1'] = 'dog'
db['2'] = 'cat'
db.sync()
db.close()
The setup.py file contains the following:
from setuptools import setup
OPTIONS = dict(
argv_emulation = True,
includes=['future_builtins','concurrent.futures']
)
DATA_FILES = []
setup(
app='highlightdb.py',
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
The traceback I am getting is:
highlightdb[68150]: db = Shove(uri)
highlightdb[68150]: File "shove/core.pyc", line 23, in __init__
highlightdb[68150]: File "shove/_imports.pyc", line 46, in store_backend
highlightdb[68150]: KeyError: 'file'
What could the problem be?

I found the problem is that shove functionality is largely implemented using plugins, which are loaded using pkg_resources.iter_entry_points. Unfortunately py2app doesn't support iter_entry_points. Nevertheless I found this, which may be a possible workaround.

Did you restarted Python after installing Shove?
The same KeyError: 'file' happens if you don't restart: you can import the library, but Shove can't load any modules

Related

How to fix Py2app being unable to access image files?

Bear with me I'm a python newbie trying to make a simple py2app that can create a basic template mc resourcepack, with all the nested folders, dependencies, etc.
I have managed to create the application, and the folder is created, everything is running smoothly except that I am unable to access the image files no matter what I try.
Here is the folder structure:
Vine.app/Contents/Resources/image files + pyfile that is being run
I am on MacOs and using the PIL library to handle the images
Running the .py file itself makes everything work fine but not when run through the .app
This is what I have tried:
packimage = Image.open('pack.png')
packimage = packimage.save('vinepack/pack.png')
packimage = Image.open('Contents/pack.png')
packimage = packimage.save('vinepack/pack.png')
packimage = Image.open('Resources/Contents/pack.png')
packimage = packimage.save('vinepack/pack.png')
Setup.py file used to make the app:
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['Vine.py']
DATA_FILES = ['pack.png', 'MobTextures/allay.png', 'MobTextures/bat.png', 'MobTextures/bee.png', 'MobTextures/big_sea_turtle.png', 'MobTextures/blaze.png', 'MobTextures/cave_spider.png', 'MobTextures/chicken.png', 'MobTextures/cod.png', 'MobTextures/cow.png', 'MobTextures/dragon.png', 'MobTextures/drowned.png', 'MobTextures/enderman.png', 'MobTextures/endermite.png', 'MobTextures/evoker.png', 'MobTextures/fox.png', 'MobTextures/glow_squid.png', 'MobTextures/goat.png', 'MobTextures/hoglin.png', 'MobTextures/husk.png', 'MobTextures/illusioner.png', 'MobTextures/iron_golem.png', 'MobTextures/magmacube.png', 'MobTextures/ocelot.png', 'MobTextures/panda.png', 'MobTextures/phantom.png', 'MobTextures/pig.png', 'MobTextures/piglin.png', 'MobTextures/pillager.png', 'MobTextures/polarbear.png', 'MobTextures/pufferfish.png', 'MobTextures/ravager.png', 'MobTextures/salmon.png', 'MobTextures/sheep.png', 'MobTextures/silverfish.png', 'MobTextures/skeleton.png', 'MobTextures/slime.png', 'MobTextures/spider.png', 'MobTextures/squid.png', 'MobTextures/stray.png', 'MobTextures/strider.png', 'MobTextures/tadpole.png', 'MobTextures/vex.png', 'MobTextures/vindicator.png', 'MobTextures/wandering_trader.png', 'MobTextures/warden.png', 'MobTextures/witch.png', 'MobTextures/wither.png', 'MobTextures/zoglin.png', 'MobTextures/zombie.png']
OPTIONS = {'iconfile': 'icon.icns',
}
setup(
app=APP,
APP_NAME = "Vine",
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Thank you very much in advance for any help you can offer!
(Please I have been struggling/crying for 4 hours now)

Kedro cannot find run

As a part of upgrading Kedro from 0.16.2 to 0.17.3 in our organization, I've made changes to Kedro related files in our codebase based on Kedro starter pyspark-iris on 0.17.3.
Now I get an error of Error: No such command 'run' on kedro run.
setup.py
from setuptools import find_packages, setup
entry_point = "kedro-project = kedro-package.__main__:main"
# get the dependencies and installs
with open("requirements.txt", "r", encoding="utf-8") as f:
# Make sure we strip all comments and options (e.g "--extra-index-url")
# that arise from a modified pip.conf file that configure global options
# when running kedro build-reqs
requires = []
for line in f:
req = line.split("#", 1)[0].strip()
if req and not req.startswith("--"):
requires.append(req)
setup(
name="kedro-package",
version="0.1",
packages=find_packages(exclude=["tests"]),
entry_points={"console_scripts": [entry_point]},
install_requires=requires,
extras_require={
"docs": [
"sphinx~=3.4.3",
"sphinx_rtd_theme==0.5.1",
"nbsphinx==0.8.1",
"nbstripout==0.3.3",
"recommonmark==0.7.1",
"sphinx-autodoc-typehints==1.11.1",
"sphinx_copybutton==0.3.1",
"jupyter_client>=5.1.0, <6.0",
"tornado>=4.2, <6.0",
"ipykernel~=5.3",
]
},
)
main.py
from pathlib import Path
from kedro.framework.project import configure_project
import logging
from .cli import run
def main():
package_name = str(Path(__file__).resolve().parent.name)
logging.getLogger(__name__).info(f"package name is: {package_name}")
configure_project(package_name=package_name)
run()
if __name__ == "__main__":
main()
and cli.py is at the same level as main.py which are directly inside the package (altered to kedro-package here for anonymity)
This only happens when performing kedro run on the EMR. When we run locally we don't see that error. Rather it errors out because it can't connect to S3, which is expected.
Additionally, I've tried running

Cx-Freeze Error - Python 34

I have a Cx_Freeze setup file that I am trying to make work. What is terribly frustrating is that it used to Freeze appropriately. Now, however, I get the following error:
edit. the error that shows up is not a Python exception through the console, but a crash report when attempting to launch the resulting exe file generated through the freeze.
'File 'notetest.py', line 1, in
_find_and_load importlib_bootstrap.py, line 2214
....
AttributeError 'module' object has no attribute '_fix_up_module'
My setup.py file follows:
import sys
import os
from cx_Freeze import setup, Executable
build_exe_options = {'packages': [], 'excludes' : []}
base = 'Win32GUI'
exe = Executable(
script = 'notetest.py',
initScript = None,
base = 'Win32GUI',
targetName = 'MedicaidAid.exe',
compress = True,
appendScriptToExe = True,
appendScriptToLibrary = True,
icon = None
)
setup( name = 'MedicaidAid',
version = '0.85',
description = 'MedicaidAid Software',
options = {'build_exe': build_exe_options},
executables = [Executable('notetest.py', base = base)])
You should install cx_freeze from this site. It contains an important patch that solves the problem (see this discussion for detailed).
Apparently this is a known issue with Cx_Freeze. BitBucket.
Apparently there is a compiling error that occurs. The bitbucket link lists the work-arounds.
I had the same issue and the solution by Northcat worked just perfectly. The issue of missing patch in the other cx_freeze came into picture while installing(some alert messages were thrown) it. It is this missing patch because of which the alerts were given. The new installation from this link had no such alerts while installing and it so worked fine.

keyring module is not included while packaging with py2exe

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

Python and Freeze Application with shelve.py

I am trying to freeze an application that involves the use of the shelve module. To freeze it, I am using the GUI2EXE python code and utilizing the cx_freeze portion (everything works great if i remove the shelve part).
When I go to run my compiled application, it complains of
File "anydbm.pyc", line 62, in ?
ImportError: no dbm clone found; tried ['dbhash', 'gdbm', 'dbm',
'dumbdbm']
I have searched around for answers. Most of them said to add this to the script:
for i in ['dbhash', 'gdbm', 'dbm', 'dumbdbm']:
try: eval('import '+i)
except: pass
But, this didn't do anything for me. If i include the dbhash module i then get errors related to no bsddb module exists. I cannot seem to figure this problem out. Did i implement the above incorrectly? Am i missing something?
PS, I need to use cx_freeze -- the others (py2exe, pyinstaller) do not work well with the other portions of my program. Also, i really would like to use shelve -- like i said, it compiles and works fine without it.
Thanks!
EDIT
Per Mike's request, I have attached the setup script. Yes, I have tried to include the modules (not shown) but it doesn't work. I have even included anydbm and dbhash in my main script. This doesn't seem to work either.
Also, if you know of a better way to store my variables/lists/dicts/etc than shelve, I would love to know. I tried ZODB (didn't build well either). Currently, i did find pdict (with PersistentDict) and this works well when I freeze the application. However, I find shelve to be faster. Would like to get shelve working if possible...
My setup script:
from cx_Freeze import setup, Executable
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter']
packages = []
path = []
for i in ['dbhash', 'gdbm', 'dbm', 'dumbdbm']:
try:
eval('import '+i)
except:
pass
GUI2Exe_Target_1 = Executable(
# what to build
script = "myscript.py",
initScript = None,
base = 'Win32GUI',
targetDir = r"dist",
targetName = "myscript.exe",
compress = True,
copyDependentFiles = False,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
setup(
version = "0.1",
description = "No Description",
author = "No Author",
name = "cx_Freeze Sample File",
options = {"build_exe": {"includes": includes,
"excludes": excludes,
"packages": packages,
"path": path
}
},
executables = [GUI2Exe_Target_1]
)
eval('import foo') will always fail: eval is for expressions, but import is a statement. You should avoid except: clauses that don't specify the exception type - they hide real bugs in your code.
Try something like this:
for dbmodule in ['dbhash', 'gdbm', 'dbm', 'dumbdbm']:
try:
__import__(dbmodule)
except ImportError:
pass
else:
# If we found the module, ensure it's copied to the build directory.
packages.append(dbmodule)
You could use pickle instead of shelve to store your data. Or you could use ConfigObj to create a text file with most of that information: http://www.voidspace.org.uk/python/configobj.html
I suppose you could even use SQLite to store most of your data too. If you're trying to save the state of your wxPython GUI, see the PersistentManager: http://xoomer.virgilio.it/infinity77/Phoenix/lib.agw.persist.persistencemanager.PersistenceManager.html

Categories