briarfox briarfox - 1 year ago 189
Python Question

Parse without setuptools

I'm using python on my ipad and need a way to grab the name, version, packages etc from a packages I do not have access to setuptools or distutils. At first I thought that I'd parse but that does not seem to be the answer as there are many ways to pass args to setup(). I'd like to create a mock setup() that returns the args passed to it, but I am unsure how to get past the import errors. Any help would be greatly appreciated.

Answer Source

You can dynamically create a setuptools module and capture the values passed to setup indeed:

>>> import imp
>>> module = """
... def setup(*args, **kwargs):
...     print(args, kwargs)
... """
>>> setuptools = imp.new_module("setuptools")
>>> exec module in setuptools.__dict__
>>> setuptools
<module 'setuptools' (built-in)>
>>> setuptools.setup(3)
((3,), {})

After the above you have a setuptools module with a setup function in it. You may need to create a few more functions to make all the imports work. After that you can import and gather the contents. That being said, in general this is a tricky approach as can contain any Python code with conditional imports and dynamic computations to pass values to setup().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download