Skip to content

Commit

Permalink
replace the version function with an import, and defer the discussion…
Browse files Browse the repository at this point in the history
… to the Python Packaging User Guide
  • Loading branch information
qwcode committed May 15, 2014
1 parent 44b62e5 commit 335acdc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
1 change: 1 addition & 0 deletions sample/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# The version should comply with PEP440
__version__ = '1.2.0'

def main():
Expand Down
26 changes: 7 additions & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@
import os
import re

here = os.path.abspath(os.path.dirname(__file__))

# Read the version number from a source file.
# Why read it, and not import?
# see https://groups.google.com/d/topic/pypa-dev/0PkjVpcxTzQ/discussion
def find_version(*file_paths):
# Open in Latin-1 so that we avoid encoding errors.
# Use codecs.open for Python 2 compatibility
with codecs.open(os.path.join(here, *file_paths), 'r', 'latin1') as f:
version_file = f.read()

# The version line must have the form
# __version__ = 'ver'
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
import sample

here = os.path.abspath(os.path.dirname(__file__))

# Get the long description from the relevant file
with codecs.open('DESCRIPTION.rst', encoding='utf-8') as f:
long_description = f.read()

setup(
name="sample",
version=find_version('sample', '__init__.py'),

# There are various approaches to referencing the version. For a discussion,
# see http://packaging.python.org/en/latest/tutorial.html#version
version=sample.__version__,

description="A sample Python project",
long_description=long_description,

Expand Down

0 comments on commit 335acdc

Please sign in to comment.