How to create Python egg file
Asked Answered
T

3

99

I have questions about egg files in Python.

I have much Python code organized by package and I'm trying to create egg files. I'm following instructions, but they are very common.

According to that, it seems I need to have a setup.py file.

  1. Would you please tell me what I need to put into setup.py file and where it should reside?
  2. I suppose it's enough to create setup.py and then start "setup.py bdist_egg" for getting egg file. Could you please confirm?
  3. Is it possible to include only .pyc files into egg file?
  4. Having .egg file how I can just start the code from it without unpacking like java -jar <jar file> does?
Tamayo answered 8/1, 2010 at 8:46 Comment(2)
What should I use instead eggs then? Here's my question about eggs #14903232Lainelainey
@YauhenYakimovich: that link is now a parked spam domain. Maybe worth editing to use packaging.python.org/en/latest/wheel_egg?Ellett
D
79

You are reading the wrong documentation. You want this: https://setuptools.readthedocs.io/en/latest/setuptools.html#develop-deploy-the-project-source-in-development-mode

  1. Creating setup.py is covered in the distutils documentation in Python's standard library documentation here. The main difference (for python eggs) is you import setup from setuptools, not distutils.

  2. Yep. That should be right.

  3. I don't think so. pyc files can be version and platform dependent. You might be able to open the egg (they should just be zip files) and delete .py files leaving .pyc files, but it wouldn't be recommended.

  4. I'm not sure. That might be “Development Mode”. Or are you looking for some “py2exe” or “py2app” mode?

Didache answered 8/1, 2010 at 10:54 Comment(2)
+1: Never include the .pyc files. They are totally confusing. We accidentally copied windows .pyc files onto a linux box and had the most confusing error traceback messages ever. Because the filenames where the original Windows filenames. It was terrifying to see those windows filenames on the production server.Eben
just use pyinstallerStipulate
S
30

For #4, the closest thing to starting java with a jar file for your app is a new feature in Python 2.6, executable zip files and directories.

python myapp.zip

Where myapp.zip is a zip containing a __main__.py file which is executed as the script file to be executed. Your package dependencies can also be included in the file:

__main__.py
mypackage/__init__.py
mypackage/someliblibfile.py

You can also execute an egg, but the incantation is not as nice:

# Bourn Shell and derivatives (Linux/OSX/Unix)
PYTHONPATH=myapp.egg python -m myapp
rem Windows 
set PYTHONPATH=myapp.egg
python -m myapp

This puts the myapp.egg on the Python path and uses the -m argument to run a module. Your myapp.egg will likely look something like:

myapp/__init__.py
myapp/somelibfile.py

And python will run __init__.py (you should check that __file__=='__main__' in your app for command line use).

Egg files are just zip files so you might be able to add __main__.py to your egg with a zip tool and make it executable in python 2.6 and run it like python myapp.egg instead of the above incantation where the PYTHONPATH environment variable is set.

More information on executable zip files including how to make them directly executable with a shebang can be found on Michael Foord's blog post on the subject.

Stomodaeum answered 28/10, 2011 at 14:55 Comment(1)
Alternatively one can use virtual environment guide.python-distribute.org/installation.html#upgradingHedron
M
13

I think you should use python wheels for distribution instead of egg now.

Wheels are the new standard of python distribution and are intended to replace eggs. Support is offered in pip >= 1.4 and setuptools >= 0.8.

Melancholia answered 5/2, 2015 at 5:49 Comment(1)
You can't import a wheel, as far as I can tell. Sometimes I just want to deploy my script along with an egg to make it more portable.Kamila

© 2022 - 2024 — McMap. All rights reserved.