35

Is it possible to run Python egg files directly as you can run jar files with Java?

For example, with Java you might dos something like:

$ java -jar jar-file
Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
Marko Kukovec
  • 691
  • 1
  • 6
  • 8

4 Answers4

18

A python egg is a "a single-file importable distribution format". Which is typically a python package.

You can import the package in the egg as long as you know it's name and it's in your path.

You can execute a package using the "-m" option and the package name.

However, python packages generally do not do anything when executed, and you may get an error. The -c option can be used to run code. (See http://docs.python.org/using/cmdline.html for details on command line options)

> python -m sphinx
sphinx is a package and cannot be directly executed


> python -c "import <package in an egg>; <function>();"



> python -c "import sphinx; print sphinx.package_dir"
C:\Python26\lib\site-packages\sphinx-0.6.1-py2.6.egg\sphinx
monkut
  • 42,176
  • 24
  • 124
  • 155
16

As of Python 2.6, you can use python some.egg and it will be executed if it includes a module named __main__.

For earlier versions of Python, you can use PYTHONPATH=some.egg python -m some module, and somemodule from the egg will be run as the main module. (Note: if you're on Windows, you'd need to do a separate SET PYTHONPATH=some.egg.)

Chris Krycho
  • 3,125
  • 1
  • 23
  • 35
PJ Eby
  • 8,760
  • 5
  • 23
  • 19
  • I'm using Python 3, I don't understand how to add a module named __main__ and where, do you have an example? – Eric Bellet Sep 03 '19 at 11:06
  • The same way you add any other module to a Python project: by adding a `.py` file with that name, and then listing it in the [setup arguments](https://docs.python.org/3/distutils/setupscript.html#listing-individual-modules) – PJ Eby Sep 04 '19 at 14:33
6

For example, if you want to import the suds module which is available as .egg file:

egg_path='/home/shahid/suds_2.4.egg'

sys.path.append(egg_path)

import suds
#... rest of code
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Mohammad Shahid Siddiqui
  • 3,730
  • 2
  • 27
  • 12
  • 5
    @Mark where does it say that? The question ask "How to run Python egg files directly without installing them?" This answer is valid. – Adham Apr 14 '14 at 15:04
  • @Adham - the Java example in the question is from the command line not from in Java – mmmmmm Apr 14 '14 at 15:26
  • 1
    @Mark You are inferring what the question is asking based on the example. There is no mention of "command line" in the question. – Adham Apr 15 '14 at 16:47
  • @Adham - which is the only useful interpretation - jar files are run from the command line – mmmmmm Apr 15 '14 at 17:11
  • 2
    @Mark Running from command line is YOUR interpretation of what the question is asking. Interpretation is not required when the question is explicitly asking "How to run Python egg files directly without installing them?" – Adham Apr 15 '14 at 17:15
1

Python Egg file direct execution steps

Suppose if you have egg file and driver file to run through below command.

PYTHONPATH=eggfilename.egg python driverfile.py

above command for without install egg file with python code.

Adeel
  • 2,901
  • 7
  • 24
  • 34