views:

69

answers:

2

Is there a tool in python to rewrite some code which imports things such that it no longer has to import anything?

Take a library that draws a box called box.py

def box(text='Hello, World!')
    draw the box magic
    return

Now in another program (we'll call it warning.py) it says:

from box import box

box('Warning, water found in hard drive')

Is there a tool that I can use that will see it importing the box function (or a class for that matter) from box and insert that function (or class, or even variable definitions) into the warning.py script removing the import line (thus making it more portable)?

With thanks, Narnie

+6  A: 

It sounds like you're wanting to effectively copy-paste everything into one file to be able to distribute a single file instead of several?

In that case, look into using a zipped module instead of copy-pasting everything into one file... This is far more maintainable in the long run.

Python will execute a zip file if there's a __main__.py file inside of it.

E.g.:

echo "def foo(): print 'code inside a.py'" > a.py
echo "def bar(): print 'code inside b.py'" > b.py
echo "import a; import b; a.foo(); b.bar()" > __main__.py
zip test.zip a.py b.py __main__.py
rm a.py b.py __main__.py

python test.zip

This creates a zip file with 3 python files inside of it. It can be executed with python test.zip. Everything is completely self-contained. (I believe this requires python >= 2.6, by the way...)

Joe Kington
+1 I did not know that.
aaronasterling
I would like to do that, but they are in different directories (such as lib directories). That makes this a little more difficult. However, it is cool to know that if there is a __main__.py, it will execute it. That is quite neat!
narnie
@narnie - Well, you can still copy and import directories inside the zip file (as long as each directory has an `__init__.py` file inside it, as usual for any module). Of course, it becomes more difficult to track things down when you have several modules in several different places... Good luck, at any rate!
Joe Kington
A: 

If you're trying to make your scripts easier to distribute, this isn't the right way to go about doing it. For windows, look into py2exe which will convert all your files into one executable file. For linux, the preferred method is to use a package which the user's package manager will install. If you have your distutils bits properly set up, it should not be difficult at all to create a package for your software.

Daenyth
for quick, simple scripts that I just want to move to different computers in our household and friends, packing it in a deb file, etc, is overkill. We use Linux, so not as simple as just making a py2exe executable. I will try to education myself on distutils, tho as I'm always game for learning something new.
narnie
@narnie: In that case, just put them in a git repo and clone it over. You'll have all the files and you'll get source control as a bonus.
Daenyth
@Daenyth, I remember reading about distutils now. I was disappointed that it doesn't help make debs. However, I have written some extensive shell scripts to help me create debs in the past for shell scripts I've written. Always wanted for them what I've been asking for above with Python. Was just a hope, but it looks like one I will not realize. :)
narnie
@Daenyth, I have been thinking about using git. I'm very much a novice with it and not sure how to do this, but I'm game for learning it. I'll look for some git tutorials and look forward to learning something new (to me).
narnie
Daenyth