Search code examples
pythondistutils

Access variables set in setup.cfg


I'm using distutils, and parts of my setup.py pulls a remote repository. I would like to be able to set the URL to this repository in the setup.cfg, but how do I access it from within setup.py?

Let's say that I have this in my setup.cfg:

[download]
repository = 'git://github.com/repo.git'

How would I access the repository variable from outside the cfg file?


Solution

  • Use ConfigParser

    import ConfigParser

    Config = ConfigParser.RawConfigParser()
    
    cfg = open('abcd.txt', 'r')
    Config.readfp(cfg)
    cfg.close()
    
    cfg = open('abcd.txt', 'w')
    Config.remove_option('download', 'repository')
    Config.set('download','repository', 'Some URL')
    Config.write(cfg)
    cfg.close()
    

    Update: Sorry I had thought you wanted to read the value and no set it. Note that this also assumes that setup.cfg aleady exists and there's a download option already