How to extract dependencies information from a setup.py

You can use distutils.core's run_setup:

from distutils.core import run_setup

result = run_setup("./setup.py", stop_after="init")
result.install_requires
['spam==1.2.3', 'eggs>=4.5.6']

This way there is no need to mock anything and you can potentially extract more information about the project than would be possible by mocking the setup() call.

Note that this solution might be problematic as there apparently is active work being done to deprecate distutils. See comments for details.


It seems to me that you could use mock to do the work (assuming that you have it installed and that you have all the setup.py requirements...). The idea here is to just mock out setuptools.setup and inspect what arguments it was called with. Of course, you wouldn't really need mock to do this -- You could monkey patch setuptools directly if you wanted ...

import mock  # or `from unittest import mock` for python3.3+.
import setuptools

with mock.patch.object(setuptools, 'setup') as mock_setup:
    import setup  # This is setup.py which calls setuptools.setup

# called arguments are in `mock_setup.call_args`
args, kwargs = mock_setup.call_args
print kwargs.get('install_requires', [])