Using egg files python

How do I create and load an egg-file in Python?

eggs are just a way of putting together a bunch of python files, data files, and metadata, somewhat similar to Java JARs — but python packages can be installed from source even without en egg (which is a concept which does not exist in the standard distribution). There were few attempts before, which don’t seem to be full solutions: solution to move the file inside the package solution to read as zip accessing meta info via get_distribution The task at hand is to read the information about the egg the program is running from.

How do I create and load an egg-file in Python?

I am researching for a way to distribute a python module as a single egg-file. Supposing I have a python module called my_module and I want to write a python script that generates an egg-file for my module. So I found setuptools .

from setuptools import setup setup( name="my_module", packages=[my_package], version="1.0", ) 

And I’ve had some disadvantages regarding these issues:

  1. This script should be run as python setup.py install . In other words, I need to specify command line arguments. Instead I want to generate my egg-file automatically during my python code, that has its own control over the command line arguments.
  2. The result files are outputed into the setup’s file directory. I would like to control the output directory path in the script.
  3. The script create build and dist folders that I don’t actually need. Probably, I could solve it by removing the folders after calling setup .
Читайте также:  Jpasswordfield java to string

How should I use setuptools for my purposes covering the issues above?

And also how can I load my module from given egg-file?

Supposing I have a following module:

# my_module.py class MyClass: def __init__(self): self._x = None def set_x(self, x): self._x = x def get_x(self): return self._x 

I wrote this script to create an egg-file:

# create_egg.py from setuptools import setup setup( name="my_module", packages=['my_module'], version="1.0", ) 

I get such error, when I run creage_egg.py :

$ python3 create_egg.py usage: create_egg.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] . ] or: create_egg.py --help [cmd1 cmd2 . ] or: create_egg.py --help-commands or: create_egg.py cmd --help error: no commands supplied 

First thing I found out is that a package must be a directory. So it’s necessary to keep such structure:

my_project/ my_module/ __init__.py output/ create_egg.py test_egg.py 

To skip the necessity of specifying the command line arguments there’s a special option script_args . I found it here: http://peak.telecommunity.com/DevCenter/setuptools

class MyClass: def __init__(self): self._x = None def set_x(self, x): self._x = x def get_x(self): return self._x 
import os import shutil from setuptools import setup OUTPUT_DIR = 'output' if __name__ == "__main__": setup( name="my_module", packages=['my_module'], version="1.0", script_args=['--quiet', 'bdist_egg'], # to create egg-file only ) egg_name = os.listdir('dist')[0] os.rename( os.path.join('dist', egg_name), os.path.join(OUTPUT_DIR, egg_name) ) shutil.rmtree('build') shutil.rmtree('dist') shutil.rmtree('my_module.egg-info') 
import sys sys.path.append('my_project/output/my_module-1.0-py3.5.egg') import my_module obj = my_module.MyClass() obj.set_x(29) print(obj.get_x()) 
~/Stuff/my_project $ python3 create_egg.py zip_safe flag not set; analyzing archive contents. 
~/Stuff $ python3 test_egg.py 29 

Access a file from a python egg, Contents of file can be read by myfile.read() itself but we lose myfile handle as soon as we exit the context. So contents of file are copied into a temporary file if it needs to be passed around for other operations. Solution 2 : Extract the member of egg locally. zipfile provides an API for extracting the specific member

View code within Python egg files in PyDev

One of the nice features of working in Eclipse with PyDev is that clicking F3 you can browse into almost anything. However, if the package you’re using is contained in a Python egg, that doesn’t work.

Is it possible to make it work?
If not, would it work to extract the egg’s contents into site-packages and delete the egg? Wouldn’t some metadata be lost?

Actually, what you’re saying should work (i.e.: doing F3 on a reference to a file within a zip should open the file properly).

So, this was actually a rather critical bug when dealing with zip files in PyDev (which I’ve just fixed and is already available in the current nightly build — it’ll be released for PyDev 2.2.3).

For getting the nightly build see instructions at: http://pydev.org/download.html

You can unzip the egg’s contents into site-packages and it will work.

Packaging — How to create Python egg file, 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. Code samplerem Windowsset PYTHONPATH=myapp.eggpython -m myappFeedback

How do I turn a python program into an .egg file?

How do I turn a python program into an .egg file?

Setuptools is the software that creates .egg files. It’s an extension of the distutils package in the standard library.

The process involves creating a setup.py file, then python setup.py bdist_egg creates an .egg package.

Also, if you need to get an .egg package off a single .py file app, check this link: EasyInstall — Packaging others projects as eggs.

Python has its own package for creating distributions that is called distutils. However instead of using Python’s distutils’ setup function, we’re using setuptools’ setup. We’re also using setuptools’ find_packages function which will automatically look for any packages in the current directory and add them to the egg. To create said egg, you’ll need to run the following from the command line:

c:\Python34\python.exe setup.py bdist_egg 

PyCharm resolve .egg file, I am having a similar / identical issue to adding .egg files to the path , where in the PyCharm IDE it says: However, from the console I have: In[2]: from scipydirect import minimize In[3]: import

Accessing files in python egg from inside the egg

The question is an attempt to get the exact instruction on how to do that. There were few attempts before, which don’t seem to be full solutions:

solution to move the file inside the package

accessing meta info via get_distribution

The task at hand is to read the information about the egg the program is running from. There are few ways as i understand:

  1. hard code the location of the egg and treat it as zip archive — will work, but not flexible enough, because it will need to be edited and recompiled in case if file is moved to another location
  2. use ResourceManager().resource_filename(__name__, filename) — this seems to be limited in the fact that i cannot access the file that is inside the egg, but not inside the package. notations like «../../EGG-INFO/PKG-INFO» in filename don’t work giving KeyError. So no good either.
  3. use dist = pkg_resources.get_distribution(«dist_name») and then use dist object to get information, but I cannot understand from the docs how should i specify my distribution name? It can’t find it.

So, i’m looking for correct solution about using pkg_resources.get_distribution plus it would be nice to finally have a full solution to read any file from inside the egg.

Setuptools/distribute/pkg_resources is designed to be a sort of transparent overlay to standard Python distutils, which are pretty limited and don’t allow a good way of distributing code.

eggs are just a way of putting together a bunch of python files, data files, and metadata, somewhat similar to Java JARs — but python packages can be installed from source even without en egg (which is a concept which does not exist in the standard distribution).

So there two scenarios here: either you’re a programmer which is trying to use some file inside a library, and in such case, in order to read any file from your distribution, you don’t need its full path — you just need an open file object with its content, right? So you should do something like this:

from pkg_resources import resource_stream, Requirement resource_stream(Requirement.parse("restez==0.3.2"), "restez/httpconn.py") 

That will return an open, readable file of the file you requested from your package distribution. If it’s a zipped egg, it will be automatically be extracted.

Please note that you should specify the package name inside (restez) because the distribution name may be different from the package (e.g. distribution Twisted then uses twisted package name). Requirements parsing use this syntax: http://setuptools.readthedocs.io/en/latest/pkg_resources.html#requirements-parsing

This should suffice — you shouldn’t need to know the path of the egg once you know how to fetch files from inside the egg.

If you really want the full path and you’re sure your egg is uncompressed, use resource_filename instead of resource_stream.

Otherwise, if you’re building a «packaging tool» and need to access the contents of your package, be it an egg or anything, you’ll have to do that yourself by hand, just like pkg_resources does (pkg_resources source) . There’s not a precise API for «querying an egg content» because there’s no use case for that. If you’re a programmer just using a library, use pkg_resources just like I suggested. If you’re building a packaging tool, you should know where to put your hands, and that’s it.

The zipimporter used to load a module can be accessed using the __loader__ attribute on the module, so accessing a file within the egg should be as simple as:

__loader__.get_data('path/within/the/egg') 

Microsoft Apps, Utilities & tools. |. (11) Free. Get in Store app. Description. It is an integrated compression management program that provides a convenient interface for quick and easy use, supports more than 40 compression formats including the new compression format EGG, and provides a variety of additional functions. Report …

Источник

Оцените статью