distributing python software in jar like fashion

A

alf

Hi,

I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.

Thx in advance, A.
 
C

ce

Hi,

I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.

Thx in advance, A.

You can import from zip achieves as PEP 273, but to execute in the
same mechanism as the jar files, recently there is no way afaik.

ce (pain n d'ass)
 
B

Ben Finney

alf said:
I have a small app which consist of a few .py files. Is there any
way to distribute it in jar like fashion as a single file I can just
run python on. I obviously look for platform independent solution.

Python eggs are the platform-independent distributable single-file
archive. Given the way Python works, you can't simply execute them
as-is; you can, however, easily install them.

<URL:http://peak.telecommunity.com/DevCenter/EasyInstall>
 
S

Shane Geiger

Hi,

I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.

Thx in advance, A.

"""
Author: Shane Geiger <[email protected]>


Wed Mar 14 21:55:58 CDT 2007

Hi,

I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.

------

Here's an answer to your question.


To use this script put it in the same directory as your .py files.
See the glob.glob function below if you want to specify a directory if
you want to change that.
(I tried to keep this example simple.)


Use the 'pack_mode()' and 'unpack_mode()' function calls at the bottom
of the script to
control it.

Note: I have not tested this on Windows.

"""


import base64
import string, os
import zipfile
import glob

def readfile(f): return open(f, "r").read()
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)

zip_file_name = 'python_modules.zip'


def write_test_modules():
## some test modules
srgexample="""
print "This was base64 encoded."
"""
srgexample2="""
print "This, too, was base64 encoded, and it was imported from a zip."
"""
writefile('srgexample.py',srgexample)
writefile('srgexample2.py',srgexample2)

def test_mode_cleanup():
os.remove('srgexample.py')
os.remove('srgexample2.py')
os.remove(zip_file_name)


def pack_mode():
"""
PACKING MODE:
(you could automate this procedure if it is important to you)
Zip the source files into one .zip file for easier handling.
Base64 encode the zip file to avoid problems of including the file.
Include the base64 representation of the zip file in a docstring, a
step that you could automate (but I haven't).
"""

write_test_modules()

# open the zip file for writing, and write stuff to it
import zipfile
import glob, os
# open the zip file for writing, and write stuff to it
file = zipfile.ZipFile(zip_file_name, "w")

# for name in glob.glob("*.py"):
for name in [x for x in glob.glob("*.py") if x != __file__]: #
dont' try to zip up this file
file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
file.close()
## Here's how to watch what gets added
## open the file again, to see what's in it
#file = zipfile.ZipFile(zip_file_name, "r")
#for info in file.infolist():
# print info.filename, info.date_time, info.file_size,
info.compress_size

contents = open(zip_file_name, "r").read()
file_contents_in_base64 = base64.encodestring(contents)
print "Assign the following base64 encoded string to the
'python_modules_zip_base64' "
print " variable (after which you can use unpack mode)."

print file_contents_in_base64
# THEN MANUALLY ADD THAT to your main file.
# You could automatically edit the currently running file like this:

test_mode_cleanup()


def unpack_mode():
"""
UNPACKING MODE:
"""
python_modules_zip_base64="""
UEsDBBQAAAAIABq8bjYpPJvJJgAAACYAAAANAAAAc3JnZXhhbXBsZS5weeMqKMrMK1FQCsnILFYo
TyxWSEosTjUzUUjNS85PSU3RU+JSAAIAUEsDBBQAAAAIABq8bjZ5v8x4SAAAAEwAAAAOAAAAc3Jn
ZXhhbXBsZTIucHkdyTEKgDAMBdC9p/h0Dk7iSXqBaCJmaFPagODpFd/6Uh/WArlcNgnhTrh5Yuep
2wpth4sKgZvA4i+r3Ueo4BxewXisLznh8wJQSwECFAMUAAAACAAavG42KTybySYAAAAmAAAADQAA
AAAAAAAAAAAApIEAAAAAc3JnZXhhbXBsZS5weVBLAQIUAxQAAAAIABq8bjZ5v8x4SAAAAEwAAAAO
AAAAAAAAAAAAAACkgVEAAABzcmdleGFtcGxlMi5weVBLBQYAAAAAAgACAHcAAADFAAAAAAA=
"""
zip_file_contents = base64.decodestring(python_modules_zip_base64)
def writefile(f, data, perms=750): open(f, "w").write(data) and
os.chmod(f, perms)
writefile(zip_file_name,zip_file_contents)

# open the file, to see what's in it
#file = zipfile.ZipFile("test.zip", "r")
#for info in file.infolist():
# print info.filename, info.date_time, info.file_size,
info.compress_size

import sys
sys.path.insert(0, zip_file_name) # Add .zip file to front of path
import srgexample
import srgexample2

### you could even remove the file after using it
os.remove(zip_file_name)

pack_mode()
#unpack_mode()





--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
G

Gary Duzan

Hi,

I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.

Thx in advance, A.

There is a new package that has been discussed here recently
called Squisher that should do what you want by packing things into
a single pyc file. There are still some minor issues that need to
be ironed out with running the pyc directly, but it should do
exactly what you want Real Soon Now.

http://groups.google.com/groups/search?q=group:comp.lang.python+squisher&qt_s=Search

Gary Duzan
Motorola CHS
 
J

John Nagle

Were Python "eggs" a flop, or what?

We need to have one packager that everyone agrees on.
Otherwise, installs become a mess, and then you have to have
installers that handle multiple packagers.

John Nagle
 
G

Gary Duzan

Were Python "eggs" a flop, or what?

We need to have one packager that everyone agrees on.
Otherwise, installs become a mess, and then you have to have
installers that handle multiple packagers.

I think the point about Squisher is that you don't have to
install it. You can either import it or run it directly. I'm mostly
interested in it as a way to package a script with a few Python
and C modules, dump it in the field, and run "python myscript.pyc"
to execute it, with no need for anything but the base Python to be
installed on the remote site, and just one file to copy.

Gary Duzan
Motorola CHS
 
R

Robert Kern

John said:
Were Python "eggs" a flop, or what?
No.

We need to have one packager that everyone agrees on.
Otherwise, installs become a mess, and then you have to have
installers that handle multiple packagers.

Eggs and Squisher are complementary tools. Squisher is good for distributing an
application with all of its dependencies in a single file; it is not a packager
or installer for libraries. Eggs are good for distributing libraries and plugins
and their dependencies.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
C

Chris Cioffi

Hi John,

I don't think eggs are a flop, however the pain they are trying to
solve is generally pretty minor in the Python world vs what we often
see with other languages. We're starting to see some push to move
more 3rd party libraries and frameworks to eggs (ie: Turbogears) and
as the developers get used to dealing with eggs we will reach a
critical mass.

The 2 main things holding eggs back, imo:
1. The eggs extensions aren't included in the Python std lib.
2. The Python std lib is fairly robust and we can accomplish a
significant amount of work without needing 3rd party code.

If eggs were included in the std lib, and/or the std lib was packaged
as eggs we'd see a far more rapid uptake. The key is getting the egg
extensions into the main distribution.

Chris
 
C

Colin J. Williams

ce said:
You can import from zip achieves as PEP 273, but to execute in the
same mechanism as the jar files, recently there is no way afaik.
You could treat the collection of files as a Python package. You would
then add an __init__.py file to your collection and, in that __init__.py
arrange to initiate main.py (or whatever you call your initiator program).

Colin W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top