Search code examples
pythonegg

access eggs in python?


Is there any way to call an installed python egg from python code? I need to cal a sphinx documentation generator from within a python code, and currently i'm doing it like this:

os.system( "sphinx-build.exe -b html c:\\src c:\\dst" )

This works, but requires some additional configuration: 'scripts' folder inside a python installation folder need to be added to a system PATH ( i'm on Windows ). Is it any better, native way to call an installed python egg?


Solution

  • So basically, you want to use Sphinx as a library?

    Here is what sphinx-build does:

    from pkg_resources import load_entry_point
    
    load_entry_point('Sphinx==0.5.1', 'console_scripts', 'sphinx-build')()
    

    Looking at entry-points.txt in the EGG-INFO directory, notice that the sphinx-build entry point is the sphinx.main function (located in __init__.py).

    Have a look at that and duplicate what it does, and you can use sphinx as a library. I have not looked at the code in detail, but it seems that the bulk of the sphinx-build-command is done by the build method on a Sphinx object.

    In your code, you would have to do something like:

    from sphinx.application import Sphinx
    s = Sphinx(...)
    s.build(...)
    

    You need to have a look at the Sphinx source code to figure out the parameters to Sphinx.__init__() and Sphinx.build()