grigoryvp
grigoryvp

Reputation: 42483

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?

Upvotes: 1

Views: 431

Answers (2)

codeape
codeape

Reputation: 100776

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()

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

Adding the egg to PYTHONPATH or to sys.path will allow you to access the modules and packages within.

Upvotes: 1

Related Questions