Reputation: 90231
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.
Upvotes: 4
Views: 1059
Reputation: 2412
See also Standard way to embed version into python package? which is about how to propagate the version information from the one location (a file named _version.py
) to the places where other code looks for it (e.g. an attribute named __version__
and PEP 0345
metadata).
Then see Brian Warner's new "versioneer" tool, which produces the _version.py
file from revision control history.
Also, please check out my answer to Standard way to embed version into python package?, the more people that realize there is a way to do it which avoids the problems of importing your module from your setup script, the better.
Upvotes: 0
Reputation: 2693
I had a similar problem with my project. I have written a top-level, test/build/package/deploy script called build.py (not distutils-based, mind you). It reads a properties file, which contains the centralized version number. Instead of maintaining a setup.py file directly, I instead keep a template file, and my build.py script reads it in, substitutes the version variable with the value from the props file, and then writes out a setup.py script. Then my script file will execute it, to generate a package or upload to pypi.
Upvotes: 0
Reputation: 2879
Importing the setup script inside your package is silly (especially since it may no longer be present after your library is installed), but importing your library inside setup.py should be fine. A separate text file would work too, but has the problem that you must install the text file with your package if you want to access the version number at runtime.
Upvotes: 0
Reputation: 18431
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
Upvotes: 5