Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse setup.py without setuptools

I'm using python on my ipad and need a way to grab the name, version, packages etc from a packages setup.py. I do not have access to setuptools or distutils. At first I thought that I'd parse setup.py 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.

like image 371
briarfox Avatar asked Jan 06 '15 00:01

briarfox


People also ask

Do I need to install Setuptools?

you generally don't need to worry about setuptools - either it isn't really needed, or the high-level installers will make sure you have a recent enough version installed; in this last case, as long as the operations they have to do are simple enough generally they won't fail.

Is setuptools deprecated?

setuptools in Python setuptools is a library which is built on top of distutils that has been deprecated (and up for removal as of Python 3.12).

Does pip require setuptools?

Majority of users who install pip will get setuptools by default. Users can explicitly uninstall setuptools after installing pip or exclude setuptools when installing pip.


3 Answers

No kidding. This worked on python 3.4.3 and 2.7.6 ;)

export VERSION=$(python my_package/setup.py --version)

contents of setup.py:

from distutils.core import setup

setup(
    name='bonsai',
    version='0.0.1',
    packages=['my_package'],
    url='',
    license='MIT',
    author='',
    author_email='',
    description='',
    test_suite='nose.collector',
    tests_require=['nose'],
)
like image 78
guneysus Avatar answered Oct 23 '22 03:10

guneysus


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 setup.py and gather the contents. That being said, in general this is a tricky approach as setup.py can contain any Python code with conditional imports and dynamic computations to pass values to setup().

like image 37
Simeon Visser Avatar answered Oct 23 '22 03:10

Simeon Visser


You could replace the setup method of the setuptools package like this

>>> import setuptools
>>> def setup(**kwargs):
        print(kwargs)
>>> setuptools.setup = setup
>>> content = open('setup.py').read()
>>> exec(content)
like image 37
Priyam Singh Avatar answered Oct 23 '22 04:10

Priyam Singh