Standard way to embed version into Python package?
Integrate a version number into your Python package by assigning __version__ in your package's __init__.py. Adhere to the semantic versioning format (MAJOR.MINOR.PATCH). Example:
Access it using:
Utilizing this versioning strategy is key for healthy dependency management and sustaining upgrade sanity across subsequent roll-outs of your package.
Versioning 101: The Essentials
The PEP triumvirate
Embarking on a Python project? Let the "PEP triumvirate" be your stead. PEP 440 offers clear guidelines on version format compliance, PEP 396 champions the use of __version__, while PEP 8 emphasizes the ideal placement of module-level "dunder" names.
The single source of truth
All for one, one for all! Prefer storing your version info in _version.py file located in the package directory - the one-stop source for versioning.
Universal version format
Adhere to semantic versioning with a string for __version__; it's crystal clear and aligns with PEP 440, making it universally compatible.
Synchronized versions
Ensure harmonious synchronization between setup.py and _version.py. Tip: Avoid importing the package directly into your setup.py, instead, fetch version info using regex:
Automated versioning
Never underestimate a developer's love for automation! Tools like python-versioneer or pbr automate version management tied to git tags - goodbye manual updates!
Visualising versioning: A postal analogy
Think of versioning like the postal system:
Consistent retrieval
Promote uniformity by following __version__ universal terminology, keeping parity between standard libraries and 3rd-party modules.
Quick and efficient comparison
Prefer tuples of integers to enable easy version comparisons when in code.
Mitigating build-time snafus
In setup.py, dodge importing your package. Not only does it ward off side effects during installation, but it also prevents any tantrums during the build process.
Was this article helpful?