unicode - check python version(s) from within a python script and if/else based on that -
basically i'd know if there way know version of python script using within script? here's current example of i'd use it:
i make python script use unicode if using python 2, otherwise not use unicode. have python 2.7.5 , python 3.4.0 installed, , running current project under python 3.4.0. following scirpt:
_base = os.path.supports_unicode_filenames , unicode or str
was returning error:
_base = os.path.supports_unicode_filenames , unicode or str nameerror: name 'unicode' not defined
so changed in order work:
_base = os.path.supports_unicode_filenames , str
is there way change effect:
if python.version == 2: _base = os.path.supports_unicode_filenames , unicode or str else: _base = os.path.supports_unicode_filenames , str
you define unicode
python 3:
try: unicode = unicode except nameerror: # python 3 (or no unicode support) unicode = str # str type unicode string in python 3
to check version, use sys.version
, sys.hexversion
, sys.version_info
:
import sys if sys.version_info[0] < 3: print('before python 3 (python 2)') else: # python 3 print('python 3 or newer')
Comments
Post a Comment