473,749 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I package a python script and modules into a single script?

I would like to package my main script and all the
modules it imports into a single script that will
run just as the collection would. It should not
need to package standard Python lib modules -- just
my local modules. This is not the same question
that would be answered with py2exe or py2app. I
don't need to package the script into a binary
along with Python. I expect Python to be installed.
I also don't want to use distutils to install the
script. The script will always run on Unix.

I thought that there might be some way to run a
script package from a tar file. One way would be to
have the package script untar itself into a temp
directory; run the main script; then delete the
temporary package directory when done. That sounds
clunky and prone to leave around trash if someone
does a 'kill -9'. However, this might be an
acceptable compromise. I'm sure that I've seen a
tool around like this, but I can't find it anymore.
I checked the usual places (google and sf.net).

I also considered simply cutting and pasting all
the modules I need into one single, giant script,
but that's less appealing than the tarball idea
(unless it could be done automatically).

Yours,
Noah

Nov 3 '05 #1
10 3487
"Noah" <no**@noah.or g> writes:
I would like to package my main script and all the
modules it imports into a single script that will
run just as the collection would. It should not
need to package standard Python lib modules -- just
my local modules. This is not the same question
that would be answered with py2exe or py2app. I
don't need to package the script into a binary
along with Python. I expect Python to be installed.
I also don't want to use distutils to install the
script. The script will always run on Unix.

I thought that there might be some way to run a
script package from a tar file. One way would be to
have the package script untar itself into a temp
directory; run the main script; then delete the
temporary package directory when done. That sounds
clunky and prone to leave around trash if someone
does a 'kill -9'. However, this might be an
acceptable compromise. I'm sure that I've seen a
tool around like this, but I can't find it anymore.


Wasn't this named 'squeeze'?

Thomas
Nov 3 '05 #2
Noah <no**@noah.or g> wrote:
I would like to package my main script and all the
modules it imports into a single script that will
run just as the collection would. It should not
need to package standard Python lib modules -- just
my local modules. This is not the same question
that would be answered with py2exe or py2app. I


Make a zipfile with the .pyc of all the modules you want; put a small
text header right before it to import the mainscript and run its main
function; that's it, thanks to Python's ability to import from zipfiles.

There's a recipe for that in the Python Cookbook (2nd edition).

(I believe py2app doesn't need to package the standard library, either,
since it comes with MacOSX 10.3 and later, but I haven't checked).
Alex
Nov 3 '05 #3
I believe you are thinking of "freeze", which is part of the normal
python distro.

There is also cx_Freeze (
http://starship.python.net/crew/atuining/cx_Freeze/ )....

Bob

Nov 3 '05 #4
Freeze also packages the python interpreter into a binary.
I need a cross platform solution that just packages the scripts.
I expect the user to already have python installed.

Yours,
Noah

Nov 4 '05 #5
Bingo! That is what I was thinking of.
http://effbot.org/zone/squeeze.htm
It turns out that it doesn't quite do what I want
because the resulting script is tightly coupled
to the version of Python used to build the package.
It compiles the PYC byte-code into the package.
It's neat and I may be able to use the ideas to do
what I want.

Yours,
Noah

Nov 4 '05 #6
Would PythonEggs be close?

It's basically a zip format with all the Python files inside, it would
maintain the structure and not be a single script file, but would be a
single .egg file... the end user would just run the script and all
would be installed, essentially.

http://peak.telecommunity.com/DevCenter/PythonEggs

Nov 4 '05 #7
This is interesting, but requires two separate files -- the ZIP file
and the boot script.
This is because zipimport can only import from file paths.
It can't import from a string or an IOString (as far as I can tell).
If only they would let you import from an already open file. Damn!

Squeeze is 99% of what I want except for the fact that it precompiles
the py files to bytecode
which locks you to the version of python you compiled on. Python byte
code is not guaranteed
to be portable across different versions of Python. Squeeze relies on
ihooks, which is
a standard module, but it is undocumented.

Anders Hammarquist has a Python Cookbook example that seems to do what
I want.
"Importing a Dynamically Generated Module". So far so good. I have to
check it out a bit more.
It relies on the "imp" standard library module.

With a bit of inspiration from "Squeeze" and help from the "imp" module
I might have what I want.

Yours,
Noah

Nov 4 '05 #8
Noah <no**@noah.or g> wrote:
This is interesting, but requires two separate files -- the ZIP file
and the boot script.
No, it doesn't.
This is because zipimport can only import from file paths.
It can import from a file, and the file (like all zipfiles) can have a
prefix. That prefix is where you put the "boot" script.

See http://aspn.activestate.com/ASPN/Coo.../Recipe/215301
(though I believe I did some minor enhancements, as well as adding
useful discussion, in the 2nd edition of the O'Reilly printed version of
the cookbook, Raedler's basic idea is presented here; you can get all
the sources as they appear in the printed version as an archive, a
zipfile I believe, on the O'Reilly site for the book).
Anders Hammarquist has a Python Cookbook example that seems to do what
I want.
"Importing a Dynamically Generated Module". So far so good. I have to
check it out a bit more.


I know Anders (known to his friends as "Iko") quite well, worked at his
side in Sweden in past years, and I selected his recipe for the first
edition of the O'Reilly printed version of the Cookbook -- but it does
not address the specific problem you desire. Raedler's recipe does.
Alex
Nov 4 '05 #9
Alex Martelli wrote:
This is because zipimport can only import from file paths.
It can import from a file, and the file (like all zipfiles) can have a prefix
That prefix is where you put the "boot" script.
See http://aspn.activestate.com/ASPN/Coo.../Recipe/215301


Unfortunately, Raedler's boot script closes stdin, which is a fairly
big limitation.
The herefile in the shell script (END_OF_PYTHON_ CODE) redirects stdin
before
python starts; stdin is closed at the end of the herefile. This
disables raw_input()
and anything else that reads sys.stdin. Enter the following at a shell
prompt
to demonstrate the problem:
python - << HEREFILE print raw_input("Ente r something")
HEREFILE

You will get the following error:
Enter somethingTraceb ack (most recent call last):
File "<stdin>", line 1, in ?
EOFError: EOF when reading a line

But all is not lost. The "zipheader.unix " script can be rewritten to
use the '-c' option of python
instead of stdin and a herefile. This also handles the case where
Python may be even less
than version 2.0. This works the same. Just "cat zipheader.unix
main.zip > main" then run "main".

---- 8< ---- 8< ---- save as zipheader.unix ---- 8< ---- 8<
--------------
#!/bin/sh
# This is a self-extracting executable.
# Execute this like any normal executable.
# You may need to "chmod a+x" this file.
# This is a binary ZIP file with a Python loader header.
#
# Bourne shell loader:
PYTHON=$(which python 2>/dev/null)
if [ ! -x "$PYTHON" ] ; then
echo "Python not found!"
exit 1
fi
exec $PYTHON -c "
# Python loader:
import sys, os
if int(sys.version[0])<2:
print 'Python version 2.3 final or greater is required.'
print 'Your version is', sys.version
os._exit(1)
major = sys.version_inf o[0]
minor = sys.version_inf o[1]
releaselevel = sys.version_inf o[3]
if (major==2 and minor<3) or (major==2 and minor==3 and
releaselevel!=' final'):
print 'Python version 2.3 final or greater is required.'
print 'Your version is', sys.version
os._exit(1)
sys.path.insert (0, sys.argv[1])
del sys.argv[0:1]
print sys.argv[1]
import main
main.main()
" $0 $@

# Zip file:
---- end of zipheader.unix
-----------------------------------------------

Yours,
Noah

Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
97199
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I have written a theme in python for the superkaramba theme engine on kde (see http://netdragon.sourceforge.net - if you are a kde/linux user, it is a great visual applet engine). I have uploaded it to www.kdelook.org for others to download and...
4
4198
by: Robert Ferrell | last post by:
I have a question about how to organize modules in a package. I think there is something fundamental that I am missing. Say I'm creating a package, GreatPackage, which has three sub-packages, myUtilities, GoodStuff, MoreGoodStuff. The directories look like: GreatPackage |
10
3690
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
5
1542
by: Stuart D. Gathman | last post by:
The pyspf package http://cheeseshop.python.org/pypi/pyspf/] can use either pydns, or dnspython. The pyspf module has a simple driver function, DNSLookup(), that defaults to the pydns version. It can be assigned to a dnspython version, or to a test driver for in memory DNS. Or you can modify the source to "from drivermodule import DNSLookup". What is the friendliest way to make this configurable? Currently, users are modifying the...
0
971
by: krishnakant Mane | last post by:
hello all, I am struggling a bit in making python packages. when I am doing a project I want to create all my python modules inside a single package. I want to know when I make a package, what are the basic things I must generally do in the __init__.py file? and how do I link all the modules in my project with each other? like when I make a compilation unit in java under a package, the first like of that file will read "package...
12
7934
by: Chris Allen | last post by:
Hello fellow pythoneers. I'm stumped on something, and I was hoping maybe someone in here would have an elegant solution to my problem. This is the first time I've played around with packages, so I'm probably misunderstanding something here... Here's what I'd like to do in my package. I want my package to use a configuration file, and what I'd like is for the config file to appear magically in each module so I can just grab values from...
7
2193
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try it complains that I am attempting a relative import in a non-package. eg ~/python/testpackage$ ls config.py importer.py __init__.py
1
2521
by: Steven Samuel Cole | last post by:
Hi all, I am trying to build a debian package for my python modules using stdeb and dpkg-buildpackage. The package building itself works, I also managed to have an entry point created and I can use my python modules on the Ubuntu virtual machine I use to test the package. The problem is that my modules require the psycopg2 python package and the entry point seems to require setuptools. I can't figure out how to declare a dependency...
0
1922
by: Steven Samuel Cole | last post by:
Hi Stephane, thanks for your reply! :-) I do not get any notification or warning or whatever from dpkg, all output I get when running # sudo dpkg -i python-<package name>_0.0.1-4927-1_all.deb is Selecting previously deselected package python-<package name>. (Reading database ... 15026 files and directories currently installed.)
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.