aspose file tools*
The moose likes Jython/Python and the fly likes What is the python equivalent to a Java .jar file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "What is the python equivalent to a Java .jar file?" Watch "What is the python equivalent to a Java .jar file?" New topic
Author

What is the python equivalent to a Java .jar file?

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

I've got a python application that is composed of dozens of source files. It works well if you connect into the directory with the source code and execute:



But this is pretty user hostile and its fragile. If the user deletes one of the source code files, it breaks.

If it were a java app, I'd create a jar file and just put the jar file in a good place and tell the user to do something like:



Since I don't know python, I don't even know if they have an equivalent, or what it is called.

How does one package a bunch of python code?
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

You may want to use py2exe.

Edit: There is one guide to packaging: http://guide.python-distribute.org/quickstart.html. Try this out.


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1772
    
  14

I think the equivalent format to Java's .jar is the Python "egg" file. As you say, there seem to several different ways to skin this particular cat but one option seems to be to use setuptools/easy_install to create/install eggs. Haven't used these myself though. More on setuptools here, and some stuff about various approaches to packaging on the Python 3 docs.

Maybe post the same question on StackOverflow as well?


No more Blub for me, thank you, Vicar.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Palak Mathur wrote:You may want to use py2exe.

Thanks, but I really don't want to use py2exe as my target machines are not Windows. They are Linux and OS-X.
I assume that somewhere there is a solution that is able to run on anything python runs on, which includes microcontrollers that
don't even run an operating system.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

chris webster wrote:Maybe post the same question on StackOverflow as well?

Here is an old question and answer from stackoverflow

http://stackoverflow.com/questions/1264447/how-to-run-python-egg-files-directly-without-installing-them

It says you need
a module named "_main_".


Which raises the question, for me: what is a module, and how is it named _main_?
Is a module just a source file?
Does this mean that the file name needs to be _main_.py?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1772
    
  14

Pat Farrell wrote:It says you need
a module named "_main_".


Which raises the question, for me: what is a module, and how is it named _main_?
Is a module just a source file?
Does this mean that the file name needs to be _main_.py?

I'm about 20 minutes ahead of you in learning Python, Pat, so I don't know about the module named _main_ stuff or how to make an egg executable. I suspect it's basically another layer on top of making a particular module executable.

AFAIK, a module is just a source file that can contain multiple classes etc. You can make a module executable by telling it how to respond if called (implicitly) as "__main__". This is the equivalent of a main() method in Java. In the example below, my module file contains two classes Foo and Bar, a "main()" method (which is not necessary but is handy for those of us who expect such things), and some code to spot if the module is being executed directly. Python will execute any code in the module that is not inside a function/class, but you usually only want it to do this if you're calling it directly e.g. from the command line. When the Python interpreter gets to the "if __name__..." bit, it executes it, knows it's being called directly as "__main__", and is then re-directed to my main() method.

This runs and produces output just as you would expect:

My guess is that you make an egg executable in an analogous fashion but I don't know how. Maybe just try creating a module called __main__.py containing some code like this, packaging it in an egg and then executing it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the python equivalent to a Java .jar file?