python shelve ... bsddb deprecated ... how to get shelve to use another database? - python

I have an app developed in python 2.7.2 on OS X.
I use the module shelve and seems to default to bsddb on the mac.
The program won't run on a Windows 7 machine with ActiveState python 2.7 because the module bsddb is not present and is not in ActiveState's package manager (pypm). ActiveState's documentation says deprecated at v 2.6.
I guess it tries bdddb because the OS X python which created the DB defaults to bsddb.
When I delete the shelve database and run it on Windows, it happily uses some other underlying database. The Mac's python is also happy.
So I think I should enforce the use of a non-bdsdb backend for shelve. Like the gdbm module.
But I can't work out how to do that.

You can set the type of db created by setting anydbm._defaultmod before calling shelve.open.
This works for Python 2.6 (and maybe for 2.7?), but since anydbm._defaultmod is a private variable, be aware that this is a hack.
anydbm._defaultmod=__import__('gdbm')
For example:
import anydbm
import whichdb
import contextlib
anydbm._defaultmod=__import__('gdbm')
filename='/tmp/shelf.dat'
with contextlib.closing(shelve.open(filename)) as f: pass
result=whichdb.whichdb(filename)
print(result)
# gdbm

I seemed to have asked the wrong question. When building the windows exe, py2exe was not including an dbm modules (it couldn't infer this dependency), so at runtime python in desperation tried to find the bdbm module.
this script setup.py includes a module which makes the py2exe version behave like the version run normally. It includes a dbm-clone module (I'm only storing ten simple dictionaries so the basic dumbdbm module is good enough
from distutils.core import setup
import py2exe, sys, os
from glob import glob
sys.argv.append('py2exe')
data_files = [("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
setup(
data_files=data_files,
windows = ["cashflowSim.py"],
options={
"py2exe":{"includes":["dumbdbm"]}},
zipfile = None
)

Related

How do i convert my Python script (that has imported modules) to a windows exe?

Here are the import modules that my script uses :
import datetime
from dateutil import parser
from tkinter import filedialog
import tkinter
import mailbox
import pprint
import json
import urllib.request
from tkinter import *
#my script code here
How can i convert it into a windows exe. Im using python 3.4. People have suggested cx_freeze however there is no documentation on it therefore have no idea how to use it? Py2exe worked on a test script with no imported modules, but when i tried to compile my script, it didnt work? If my script is called test.py, what would the cx_freeze command be to covnert it?
Try www.py2exe.org/
py2exe is a nice module that you may find useful.
Or, if you are in linux/mac then you might try freeze method try https://wiki.python.org/moin/Freeze
I highly suggest PyInstaller.
I used to use it in order to create the exe file with multiple library files, then compress all these files into a self extracting archive, obtaining a fully working standalone exe file.
It doesn't require other scripts or code, you only have to create the file using "Makespec.py" and "Build.py".
If I'm not wrong, there is a new version compatible with Python 3.4...otherwise you could convert your script to Python 2.7.

PyPy Sandbox: Cannot import rpython module

I'm trying to use PyPy to create a server-side sandbox with limited access to my file system. I am working on Ubuntu 12.04 64 bit machine and have been trying to install the full source code for PyPy from here: http://pypy.org/download.html#sandboxed-version (scroll down to the section "Building from source").
My problem is that whenever I try running pypy_interact.py (located in pypy/pypy/sandbox), I get the following error:
ImportError: No module named rpython.translator.sandbox.sandlib
The module that cannot be imported has the following path: pypy/rpython/translator/sandbox/sandlib.py. The contents of pypy_interact.py are as follows:
import sys, os
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..\
', '..', '..')))
from rpython.translator.sandbox.sandlib import SimpleIOSandboxedProc
from rpython.translator.sandbox.sandlib import VirtualizedSandboxedProc
from rpython.translator.sandbox.vfs import Dir, RealDir, RealFile
import pypy
LIB_ROOT = os.path.dirname(os.path.dirname(pypy.__file__))
I feel like this is a really simple fix -- I just started learning Python a few days ago so I'm not exactly sure how to go about fixing the issue/don't understand imports too well yet. Any advice? Thanks very much.
Rpython typically expects that you set PYTHONPATH to include the root of your pypy checkout and not mess with the sys.path.
So you typically call the script via
PYTHONPATH=$PYTHONPATH:path/to/pypy/source path/to/pypy_interact.py

Importing a dll in python on Ubuntu

I am using python 2.6.5 on an Ubuntu intalled server.
I need to integrate an API for our applicaion, in that case, i needed to use a DLL given to me by the API provider. Their example of code about api integration is written in Visual Basic... I made a search on google and found some examples of using ctypes , and i try using cdll and pydll, which caused the following error...
OSError: /home//some.dll: invalid ELF header
One possibility is using IronPython, but i do not have much information about ironpython so i am not sure if it will handle my needs completely..
Is there any available module that let me use that dll on python (or aynthing that i am missing from the exixting ones). It is hard to upgrade my python version?
DLLs may be windows creatures, but if a DLL is 'pure .NET' and doesn't utilize executables specific to windows etc., then it can work often in Linux, through Mono. (mono ipy.exe).
Ironpython's System and similiar windows modules are customized to be os agnostic (to a untested degree).
I have successfully run NHibernate, FluentNHibernate, log4net, and a few other commonly used DLLS in Ubuntu.
import clr
import sys
sys.path.append(os.path.abspath('./DLL')) #where your dlls are
clr.AddReference('System')
clr.AddReference('FluentNHibernate')
from FluentNHibernate.Cfg.Db import PostgreSQLConfiguration
The key seems to be to import DLLs in this fashion. If a dll imports another (fluentnhibernate imports nhibernate), you don't need to import Nhibernate for example.
DLLs are Windows creatures. The only way you'll be able to use a DLL is by using a Windows build of Python. You'll be able to run Windows Python on Ubuntu by having Windows installed inside a virtual machine. You also might be able to run it using Wine.
An alternative, of course, is to ask your API provider if they have a Linux version of the API.
First, check if your DLL is a .NET Assembly file. An "Assembly DLL file" has nothing to do with the assembler. It's simply a way the .NET framework stores its bytecode inside a DLL file!
Do file library.dll in Linux. If it says something like this:
PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
then you're lucky: it's an assembly file. You can run it on Linux.
Install Mono. Install Python.NET. Forget IronPython: it's dead.
Now, in Python.NET, you can do this:
import clr
clr.AddReference('./library.dll')
# the library has just registered a namespace we can use
from LibraryName import *
but how do you know what to import?
Auto-complete.
Or use monop tool to inspect the DLL like this:
$ monop -r library.dll
Assembly Information:
LibraryName
Version=9.9.3.0
Culture=neutral
PublicKeyToken=null
LibraryName.ClassName
...
$ monop -r library.dll LibraryName.ClassName
public class ClassName {
public ClassName (string inputString);
...
}
and it will tell you everything about that library

python app to exe not working on WinSRV2003

I created little app for sending out emails when something is wrong with server. Used py2exe to create exe file. While it is works absolutely fine on Win7 i have problems with running it on WinSRV2003. I do not believe that it has something to do with code itself.
Please see imports below
import pyodbc, sys, smtplib, os
from datetime import date
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
import email.iterators
import email.generator
setup.py file:
from distutils.core import setup
import py2exe
import modulefinder
modulefinder.AddPackagePath("mail.mime", "base")
modulefinder.AddPackagePath("mail.mime", "multipart")
modulefinder.AddPackagePath("mail.mime", "nonmultipart")
modulefinder.AddPackagePath("mail.mime", "audio")
modulefinder.AddPackagePath("mail.mime", "image")
modulefinder.AddPackagePath("mail.mime", "message")
modulefinder.AddPackagePath("mail.mime", "application")
setup(console=['capfile_tester.py'],
options = { "py2exe": { "includes": "decimal, datetime, email" } })
And also one line from py2exe output that might be interesting
The following modules appear to be missing
['_scproxy']
Error message when trying to start it:
This application has failed to start because application configuration is incorrect. Reinstalling the application may fix this problem.
What came to my mind is could it missing some registry keys taht would allow app to run?
A search on _scproxy seems to indicate that _scproxy is a new module in 2.6. Perhaps somehow Python 2.5 is involved? py2exe is supposed to make a completely self-contained executable, so I don't see how that's possible, though.
Another possibility is that _scproxy depends on a dll that isn't available in Windows 2003? Have you tried running your program without py2exe on Win2003?
I'd say this is a missing DLL's problem. You should check and see the DLL's your application bundles ( or presumes to exist on the target computer ). I think you can do that with the depends.exe that comes with Visual Studio.
EDIT: I just remembered. Make sure you run py2exe with a Python 2.5 installation. The 2.6 had some bugs and that made the exe not work on several machines.
Googling for your "this application has failed to start..." message suggests strongly this is a DLL problem, probably with msvcp80.dll and friends. This is a very common occurrence with recent Windows/Python/py2exe given how MS keeps changing MSVCC libraries etc. Different Python versions are linked with different libraries and if they aren't pre-installed on your target machine you can get problems like this. Sometimes installing the appropriate redistributable package from MS works.
Note that the py2exe warnings, in this case about _scproxy, can almost always be ignored. It's very common to get what amount to spurious reports of missing modules like that. 95% of the time we can ignore them, even when we see literally dozens of modules "missing".
I had a similar problem where COM objects were involved. Maybe that's the case here, too. This description solved my problems. My software would then run on different Windows versions, which it before would not.

Create plugins for python standalone executables

how to create a good plugin engine for standalone executables created with pyInstaller, py2exe or similar tools?
I do not have experience with py2exe, but pyInstaller uses an import hook to import packages from it's compressed repository. Of course I am able to import dynamically another compressed repository created with pyInstaller and execute the code - this may be a simple plugin engine.
Problems appears when the plugin (this what is imported dynamically) uses a library that is not present in original repository (never imported). This is because import hook is for the original application and searches for packages in original repository - not the one imported later (plugin package repository).
Is there an easy way to solve this problem? Maybe there exist such engine?
When compiling to exe, your going to have this issue.
The only option I can think of to allow users access with thier plugins to use any python library is to include all libraries in the exe package.
It's probably a good idea to limit supported libraries to a subset, and list it in your documentation. Up to you.
I've only used py2exe.
In py2exe you can specify libraries that were not found in the search in the setup.py file.
Here's a sample:
from distutils.core import setup
import py2exe
setup (name = "script2compile",
console=['script2compile.pyw'],
version = "1.4",
author = "me",
author_email="somemail#me.com",
url="myurl.com",
windows = [{
"script":"script2compile.pyw",
"icon_resources":[(1,"./ICONS/app.ico")] # Icon file to use for display
}],
# put packages/libraries to include in the "packages" list
options = {"py2exe":{"packages": [ "pickle",
"csv",
"Tkconstants",
"Tkinter",
"tkFileDialog",
"pyexpat",
"xml.dom.minidom",
"win32pdh",
"win32pdhutil",
"win32api",
"win32con",
"subprocess",
]}}
)
import win32pdh
import win32pdhutil
import win32api
PyInstaller does have a plugin system for handling hidden imports, and ships with several of those already in. See the webpage (http://www.pyinstaller.org) which says:
The main goal of PyInstaller is to be compatible with 3rd-party packages out-of-the-box. This means that, with PyInstaller, all the required tricks to make external packages work are already integrated within PyInstaller itself so that there is no user intervention required. You'll never be required to look for tricks in wikis and apply custom modification to your files or your setup scripts. Check our compatibility list of SupportedPackages.

Categories