Search code examples
pythonsvndistutils

How do I assign a version number for a Python package using SVN and distutils?


I'm writing a Python package. The package needs to know its version number internally, while also including this version in the setup.py script for distutils.

What's the best way of doing this, so that the version number doesn't need to be maintained in two separate places? I don't want to import the setup.py script from the rest of my library (that seems rather silly) and I don't want to import my library from the setup.py script (likewise). Ideally, I'd just set a keyword in svn and have that automatically substituted into the files, but that doesn't seem to be possible in svn. I could read a common text file containing the version number in both places--is this the best solution?

To clarify: I want to maintain the version number in one place. Yes, I could put a variable in the package, and again in the setup.py file. But then they'd inevitably get out of sync.


Solution

  • Inside of your main package, you probably have an __init__.py, right?

    Directory structure:

    > ./packageTest
    >   ./packageTest/__init__.py
    >   ./packageTest/setup.py
    

    Inside the __init__.py file, add the following line:

    # package directory __init__.py
    __version__ = 1.0
    

    setup.py file:

    # setup.py
    from packageTest import __version__
    ...
    

    Now in any module that imports from the package directory (I'll call packageTest), you can do this:

    from packageTest import setup
    print 'Setup.py version:', setup.__version__  
    # prints Setup.py version: 1.0