Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Is there a standard way to associate version string with a python package in such a way that I could do the following?

import foo 

print foo.version

I would imagine there's some way to retrieve that data without any extra hardcoding since minor/major strings are specified in setup.py already. An alternative solution that I found was to have import __version__ in my foo/__init__.py and then have __version__.py generated by setup.py.

1 Answer

0 votes
by (106k points)

The standard way to embed version into python package is to use the below-mentioned code:-

import re 

VERSIONFILE="myniftyapp/_version.py" 

verstrline = open(VERSIONFILE, "rt").read() 

VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]" 

mo = re.search(VSRE, verstrline, re.M) 

if mo: 

verstr = mo.group(1) 

else: 

raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer

Browse Categories

...