How to assign version number for Python package using SVN and distutils?

I am writing a Python package. The package must know its version number inside, and also include this version in the setup.py script for distutils .

What is the best way to do this so that the version number is not supported in two separate places? I don't want to import setup.py script from the rest of my library (which seems pretty dumb), and I don't want to import my library from setup.py script (similarly). Ideally, I just set the keyword in svn and automatically replace it with files, but this is not possible in svn . I could read a plain text file containing the version number in both places - is this the best solution?

To clarify : I want to keep the version number in one place. Yes, I can put the variable in the package and again in the setup.py . But then they will inevitably fail.

+4
source share
4 answers

Inside the main package, you probably have __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 will call packageTest), you can do this:

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

Importing script settings inside your package is stupid (especially since it can no longer be present after installing your library), but importing your library inside setup.py should be great. A separate text file will also work, but the problem is that you have to install a text file with your package if you want to access the version number at run time.

0
source

I had a similar problem with my project. I wrote a top level test / build / package / deploy script called build.py (not based on distutils, mind you). It reads a properties file that contains the centralized version number. Instead of directly supporting the setup.py file, instead, I save the file and my build.py script file reads it, replaces the version variable with the value from the props file, and then writes the setup.py script. Then my script file will execute it to generate the package or upload to pypi.

0
source

See also Standard way to embed version in python package? , which talks about how to distribute version information from one place (a file named _version.py ) to places where another code is looking for it (for example, an attribute with metadata __version__ and PEP 0345 ).

Then see Brian Warner's new versioneer tool , which creates the _version.py file from version control history.

Also, please see your answer on the Standard way to embed a version in a python package? , the more people realize that there is a way to do this, which avoids the problem of importing your module from your installation script, the better.

0
source

All Articles