Pergunta

i created an executable egg to make it as an single file executable.

setup.py:

#!/usr/bin/env python
from setuptools import setup, find_packages

setup(
    name='app',
    version='0.5',
    description='foo',
    author='microo8',
    author_email='xxx@email.com',
    packages=["foo", "bar"],
    install_requires=["sqlalchemy>=0.7", "paramiko>=1.7.7.1"],
    entry_points = {
        'setuptools.installation': [
            'eggsecutable = foo.module:main',
        ]
    }
)

I can now call it: ./app-0.5-py2.7.egg, but the relative paths are all in the egg. when I call print __file__ in the main function it prints /home/user/app-0.5-py2.7.egg/foo/module.py. I want to read an config file that must be in the same dir as the egg. And the same script must be executable also as "non-egg", so the config file will be in the dir with the script.

how can i find out that the script is executed from an egg and also the egg path?

Foi útil?

Solução

So I dont know how to check that the running script is in an egg, but the config file path can be obtained with this: os.path.realpath('config.cfg').

This suits me and when I have a dir with the executable egg and an config file this is a correct path.

Also when I have just the script in the dir with the config file it also is correct.

And also when i use pyinstaller to create an singlefile exe.

Outras dicas

You should load your configuration file using the ResourceManager API. That way your eggified code can load the configuration file even when zipped.

import pkg_resources
configdata = pkg_resources.resource_string(__name__, "myconfigfile.cfg")

The API can check for existence, test for directories, read data or provide you with an open file object for any resource that is part of your egg.

The API takes care of all the details; it'll extract files to a temporary directory for example, if you absolutely must have a filename for a resource but are running from a zipped egg.

Most of all, it'll work regardless of whether or not you are in fact installed as an egg or stand-alone.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top