Create standalone MoinMoin wiki executable - python

I'm trying to create a standalone, desktop version of a MoinMoin wiki so I can distribute it on a CDROM to people who may or may not have Python installed. I've tried both py2exe and bbfreeze with no luck. They both create an executable, but when that executable is run I get the same error from both:
C:\python_class\cdrom\bb-binary>wikiserver.exe
2011-08-22 15:06:21,312 WARNING MoinMoin.log:138 load_config for "C:\python_class\cdrom\bb-binary\wikiserverlogging.conf
" failed with "No section: 'formatters'".
2011-08-22 15:06:21,312 WARNING MoinMoin.log:139 using logging configuration read from built-in fallback in MoinMoin.log
module!
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "__main__.py", line 128, in <module>
File "__main__wikiserver__.py", line 35, in <module>
File "MoinMoin/script/__init__.py", line 138, in run
File "MoinMoin/script/__init__.py", line 248, in mainloop
File "MoinMoin/wikiutil.py", line 1078, in importBuiltinPlugin
File "MoinMoin/wikiutil.py", line 1117, in builtinPlugins
File "MoinMoin/util/pysupport.py", line 81, in importName
ImportError: No module named server
Here is the setup.py script I used for py2exe:
from distutils.core import setup
import py2exe
includes = ["MoinMoin"]
excludes = []
packages = []
setup(options = {
"py2exe" : {
"includes" : includes,
"excludes" : excludes,
"packages" : packages,
"dist_dir" : "dist"
}
},
console=["wikiserver.py"])
And here is the setup.py script I used for bbfreeze:
from bbfreeze import Freezer
includes = ["MoinMoin.*"]
excludes = []
f = Freezer(distdir="bb-binary", includes=includes, excludes=excludes)
f.addScript("wikiserver.py")
f.use_compression = 0
f.include_py = True
f()
If anyone has any help or suggestions, I'd greatly appreciate it!
Thanks,
Doug

py2exe has limitations in identifying which modules to include, especially if they are imported conditionally. For example,
import module
on its own line will work, however,
if someCondition:
import module
won't. As is often the case with many large frameworks, MoinMoin only imports the modules it needs to use when it needs them. Unfortunately, you will need to tell py2exe to include these missing modules manually, and this is going to take some trial-and-error until you find all the ones you need.
See here for how to include modules manually.

Related

PyCharm: importing cython module that uses dynamic libraries itself - ImportError & Code Completion

I've got a cython module that is a wrapper around a c++ library that communicates with a database. That library is called libpersistence.so. That library itself is dynamically linked with libhiredis.so.
I wrote a cython wrapper around the libpersistence.so to create a py_persistence.cpython-35m-x86_64-linux-gnu.so file to import as a module in my python scripts to use the cpp persistence layer.
However, upon importing the py_persistence module there's two issues in PyCharm:
Importing the Persistence class from the module does not get recognized by PyCharm. This also means there is no code completing
Running my tests leads to an ImportError where the libpersistence.so file shared object file can not be opened.
I use the following directory structure.
top_level/
indexing_service/
bin/
libhiredis.so.0.13
libpersistence.so
kCore/
include/
hiredis/
persistence/
query_service/
__init__
runpython.sh
py_persistence/
__init__
py_persistence.cpython-35m-x86_64-linux-gnu.so
py_persistence.pxd
py_persistence.pyx
setup.py
setup.sh
test/
test_persistence.py
The setup.sh file just specifies python3 setup.py build_ext --inplace
The setup.py file is the following:
from distutils.core import setup
from Cython.Distutils import build_ext, Extension
setup(
cmdclass = { 'build_ext' : build_ext },
ext_modules = [
Extension(
'py_persistence',
sources=['py_persistence.pyx'],
libraries=['persistence'],
language='c++',
extra_link_args=['-L../../indexing_service/bin/'],
runtime_library_dirs=['../../indexing_service/bin/'],
extra_compile_args=[
'-std=c++14',
'-I../../indexing_service/kCore/include/persistence',
'-I../../indexing_service/kCore/include/hiredis'
],
cython_directives={
'c_string_type':'unicode',
'c_string_encoding':'utf8',
},
)],
)
So when building the cython py_persistence library it links to the both the libpersistence.so and the libhiredis.so.0.13 files for dynamic linking. This works, this compiles and it builds the py_persistence.cpython-35m-x86_64-linux-gnu.so file correctly.
When I run my tests using the runpython.py helper script I wrote everything works perfectly fine.
The runpython.py explicitely adds the required directories to the runtime library path:
export PYTHONPATH=$PYTHONPATH:.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../indexing_service/bin/:.
python3 ${#:1}
Running the tests:
./runpython.sh -m unittest discover py_persistence/test -v
This works perfectly.
However getting this setup working so that I can run it from PyCharm is giving me a lot of trouble. I don't seem to be able to correctly specfify the paths. In the test_persistence.py file I get an ImportError while importing the py_persistence library.
Import code:
from py_persistence.py_persistence import Persistence
The error:
Error
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/case.py", line 58, in testPartExecutor
yield
File "/usr/lib/python3.5/unittest/case.py", line 600, in run
testMethod()
File "/usr/lib/python3.5/unittest/loader.py", line 34, in testFailure
raise self._exception
ImportError: Failed to import test module: test_persistence
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
File "/home/tom/work/dev/alexandria.works/query_service/py_persistence/test/test_persistence.py", line 3, in <module>
from py_persistence.py_persistence import Persistence
ImportError: libpersistence.so: cannot open shared object file: No such file or directory
I've already tried adding the indexing_service/bin/ folder to the path in PyCharm for the virtual environment:
Interpreter Paths in PyCharm
The run configuration used to run the tests is the following:
Run Configuration
The problems I'm experiencing are the ImportError and the fact that code completion isn't working.
I'm at a loss about what else to try here... Printing sys.path in the test_persistence.py file tells me that the indexing_service/bin/ folder is on the path.
['/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm',
'/home/tom/work/dev/alexandria.works/query_service/py_persistence/test',
'/home/tom/work/dev/alexandria.works/indexing_service/bin',
'/home/tom/work/dev/alexandria.works/query_service',
'/home/tom/Applications/pycharm-2017.1.1/helpers/pycharm',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python35.zip',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/plat-x86_64-linux-gnu',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/lib-dynload', '/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/home/tom/work/dev/alexandria.works/query_service/dev-env/lib/python3.5/site-packages']
Anybody know what I missed?

program wont work after being compiled with py2exe

I wrote a program it works fine without any errors when I run it from the python interpreter, I then used py2exe to turn it in to an .exe but when I run it it does'nt work anymore... I get this error:
Traceback (most recent call last):
File "pass.py", line 1, in <module>
File "dropbox\__init__.pyc", line 3, in <module>
File "dropbox\client.pyc", line 22, in <module>
File "dropbox\rest.pyc", line 26, in <module>
File "pkg_resources.pyc", line 950, in resource_filename
File "pkg_resources.pyc", line 1638, in get_resource_filename
NotImplementedError: resource_filename() only supported for .egg, not .zip
Am I supposed to do something when using py2exe when I have downloaded modules imported into the program?
these are the modules imported :
import dropbox
import os
import getpass
from time import sleep
please help !
I fixed this problem using the solution found here
basically you modify ~line 26 in rest.py found in the dropbox library
to this:
try:
TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')
except:
# if current module is frozen, use library.zip path for trusted-certs.crt path
# trusted-certs.crt must exist in same directory as library.zip
if hasattr(sys,'frozen'):
resource_name = sys.prefix
resource_name.strip('/')
resource_name += '/trusted-certs.crt'
TRUSTED_CERT_FILE = resource_name
else:
raise
and then place the trusted-certs.crt file also found in the dropbox library in the same folder as your executable

entry_points path changed in development mode install

My console_scripts/entry_points are not working in "develop" mode install and produce a traceback saying ImportError cannot load the module containing the entry points. Need help understanding how entry_points figure out which module to load and what paths to setup and stuff. Help fixing my error.
I have a setup.py script that has some entry points as follows
entry_points = {'console_scripts':[
'pyjampiler=pyjs.pyjampiler:Builder',
'pyjscompile=pyjs.translator:main',
'pyjsbuild=pyjs.browser:build_script',
]}
my code is organized as
workspace/
setup.py
pyjs/
src/
pyjs/
browser.py
My setup.py makes use of packages and package_dir arguments to setup() in setup.py to make sure the pyjs package gets picked up from pyjs/src/pyjs and so a regular install produces the following console scripts containing the following and it runs fine. it is able to load the module and call the entry point fine.
sys.exit(
load_entry_point('pyjs==0.8.1', 'console_scripts', 'pyjsbuild')()
)
But when I install and run it in development as "python setup.py develop", the install goes fine and I see the egg.lnk files getting created. but executing the console script causes the following errors
localhost:pyjs sarvi$ lpython/bin/pyjsbuild
Traceback (most recent call last):
File "lpython/bin/pyjsbuild", line 8, in <module>
load_entry_point('pyjs==0.8.1', 'console_scripts', 'pyjsbuild')()
File "/Users/sarvi/Workspace/pyjs/lpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 318, in load_entry_point
File "/Users/sarvi/Workspace/pyjs/lpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2221, in load_entry_point
File "/Users/sarvi/Workspace/pyjs/lpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 1954, in load
ImportError: No module named browser
I suspect it has something to do with sys.path and the directory structure in the development sources. How can I get this to work in "develop" mode install?

How can I use py2exe with arcpy?

I'm trying to convert a python script to a stand-alone executable using py2exe. The script is built mostly using arcpy, with a Tkinter GUI.
The setup.py script is as follows:
from distutils.core import setup
import py2exe
script = r"pathtoscript.py"
options = {'py2exe':{
"includes": ['arcpy', 'arcgisscripting'],
"packages": ['arcpy', 'arcgisscripting'],
"dll_excludes": ["mswsock.dll", "powrprof.dll"]
}}
setup(windows=[script], options=options)
When run, setup.py creates the .exe as expected, but when I try to run the executable, I get the following error:
Traceback (most recent call last):
File "autolim.py", line 7, in <module>
File "arcpy\__init__.pyc", line 21, in <module>
File "arcpy\geoprocessing\__init__.pyc", line 14, in <module>
File "arcpy\geoprocessing\_base.pyc", line 14, in <module>
File "arcgisscripting.pyc", line 12, in <module>
File "arcgisscripting.pyc", line 10, in __load
ImportError: DLL load failed: The specified module could not be found.
I use python 2.7 and arcgis 10.1 - feel free to ask if I've forgotten any useful information.
Can anyone tell me what I need to do to get the executable working properly?
Many thanks!
I had the same problem...
Since your users will have python/arcpy installed on their machines have your arcpy scripts in a data_files directory.
setup(windows=[script], data_files=[('scripts', glob(r'/path/to/scripts/*.py')], options=options)
Then call them using subprocess.Popen
import subprocess
subprocess.Popen(r'python "%s\scripts\autolim.py"' % self.basedir)
Popen is non-blocking so it won't freeze your GUI while the arcpy script runs.
If you want to print any print statements in your arcpy script change to
p = subprocess.Popen(r'python "%s\scripts\autolim.py"' % self.basedir, stdout=subprocess.PIPE)
while True:
line = p.stdout.readline()
if not line:
break
sys.stdout.write(line)
sys.stdout.flush()
I suggest you see this step: https://community.esri.com/t5/python-questions/using-py2exe-with-arcpy-it-can-be-done-easily/td-p/360520
You must add exclude in setup options and copy the site-packages path.
I have tried it and it works.

py2exe + sqlalchemy + sqlite problem

I am playing around with getting some basic stuff to work in Python before i go into full speed dev mode. Here are the specifics:
Python 2.5.4
PyQt4 4.4.3
SqlAlchemy 0.5.2
py2exe 0.6.9
setuptools 0.6c9
pysqlite 2.5.1
setup.py:
from distutils.core import setup
import py2exe
setup(windows=[{"script" : "main.py"}], options={"py2exe" : {"includes" : ["sip", "PyQt4.QtSql","sqlite3"],"packages":["sqlite3",]}})
py2exe appears to generate the .exe file correctly, but when i execute dist/main.exe i get this in the main.exe.log
Traceback (most recent call last):
File "main.py", line 18, in <module>
File "main.py", line 14, in main
File "db\manager.pyc", line 12, in __init__
File "sqlalchemy\engine\__init__.pyc", line 223, in create_engine
File "sqlalchemy\engine\strategies.pyc", line 48, in create
File "sqlalchemy\engine\url.pyc", line 91, in get_dialect
ImportError: No module named sqlite
I've been googling my heart out, but can't seem to find any solutions to this. If i can't get this to work now, my hopes of using Python for this project will be dashed and i will start over using Ruby... (not that there is anything wrong with Ruby, i just wanted to use this project as a good way to teach myself Python)
you need to include the sqlalchemy.databases.sqlite package
setup(
windows=[{"script" : "main.py"}],
options={"py2exe" : {
"includes": ["sip", "PyQt4.QtSql"],
"packages": ["sqlalchemy.databases.sqlite"]
}})
you need change to sqlalchemy.dialects.sqlite package
setup(
windows=[{"script" : "main.py"}],
options={"py2exe" : {
"includes": ["sip", "PyQt4.QtSql"],
"packages": ["sqlalchemy.dialects.sqlite"]
}})

Categories

Resources