10 examples of 'how to check python version in cmd' in Python

Every line of 'how to check python version in cmd' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
282def check_python_version():
283 if sys.version_info < (3, 5):
284 stderr.write("DistAlgo requires Python version 3.5 or newer.\n")
285 return False
286 else:
287 return True
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

18def check_python(version):
19 required_version = tuple(int(i) for i in version.split('.'))
20 actual_version = sys.version_info[:2]
21
22 assert actual_version >= required_version, (
23 'Must use at least Python %s but have version %d.%d' %
24 (version, actual_version[0], actual_version[1]))
17def check_py():
18
19 if os_system() == "Windows":
20 try:
21 py_ver = subprocess.check_output(
22 "py -3 -V").decode('UTF-8').splitlines()[0]
23 print("Script has detected " + py_ver +
24 " version present on the machine.")
25 except:
26 print("Script could not detect Python 3 on the machine.\n"
27 "Go to https://www.python.org/downloads/ page and download latest Python 3 version.")
28 else:
29 try:
30 py_ver = subprocess.check_output(["python3", "-V"]).decode('UTF-8').splitlines()[0]
31 print("Script detected " + py_ver +
32 " version present on the machine.")
33 except:
34 print("Script could not detect Python 3 on the machine.\n"
35 "Go to https://www.python.org/downloads/ page and download latest Python 3 version.")
43def check_pyinstaller_version():
44 """Using is_module_satisfies() for pyinstaller fails when
45 installed using 'pip install develop.zip' command
46 (PyInstaller Issue #2802)."""
47 # Example version string for dev version of pyinstaller:
48 # > 3.3.dev0+g5dc9557c
49 version = PyInstaller.__version__
50 match = re.search(r"^\d+\.\d+(\.\d+)?", version)
51 if not (match.group(0) >= PYINSTALLER_MIN_VERSION):
52 raise SystemExit("Error: pyinstaller %s or higher is required" % PYINSTALLER_MIN_VERSION)
15def version_cmd(i):
16
17 ck=i['ck_kernel']
18
19 fp=i['full_path']
20
21 ver=''
22
23 p0=os.path.basename(fp)
24 lib_prefix = '.lib'
25 if p0.endswith(lib_prefix):
26 # windows naming: libboost_system-vc140-mt-s-1_62.lib
27 last_dash_index = p0.rfind('-')
28 if -1 < last_dash_index:
29 ver = p0[last_dash_index+1:-len(lib_prefix)].replace('_', '.')
30 else:
31 p1=os.path.dirname(fp)
32 lst=os.listdir(p1)
33 for fn in lst:
34 if fn.startswith(p0):
35 x=fn[len(p0):]
36 if x.startswith('.'):
37 ver=x[1:]
38 break
39
40 return {'return':0, 'cmd':'', 'version':ver}
37def check_cython_version(self, version=None, minver=None, maxver=None):
38 log_s = []
39 if version:
40 log_s.append("version=%r"%version)
41 minver=version
42 maxver=version
43 else:
44 if minver:
45 log_s.append("minver=%r"%minver)
46 if maxver:
47 log_s.append("maxver=%r"%maxver)
48 if isinstance(minver, str):
49 minver = minver.split('.')
50 if isinstance(maxver, str):
51 maxver = maxver.split('.')
52 if minver:
53 minver = tuple(map(int, minver))
54 if maxver:
55 maxver = tuple(map(int, maxver))
56
57 # Trick to be compatible python 2.x and 3.x
58 try:
59 u = unicode
60 except NameError:
61 u = str
62
63 cmd = [self.env.CYTHON[0], '--version']
64 o = u(check_output(cmd, stderr=STDOUT), 'UTF-8').strip()
65 m = cython_ver_re.match(o)
66 self.start_msg('Checking for cython version')
67 if not m:
68 self.end_msg('Not found', 'YELLOW')
69 self.fatal("No version found")
70 else:
71 v = m.group(1)
72 ver = tuple(map(int, v.split('.')))
73 check = (not minver or minver <= ver) and (not maxver or maxver >= ver)
74 self.to_log(' cython %s\n -> %r\n' % (" ".join(log_s), v))
75 if check:
76 self.end_msg(v)
77 self.env.CYTHON_VERSION = ver
78 else:
79 self.end_msg('wrong version %s' % v, 'YELLOW')
80 self.fatal("Bad cython version (%s)"%v)
81 return True
38def python(command, env=None, version='2.7'):
39 if env:
40 env = ' '.join(
41 '{}={}'.format(k, v) for k, v in env.items()
42 ) + ' '
43 else:
44 env = ''
45
46 return '{}{}{} {}'.format(env, PYTHON_BIN, version, command)
463@conf
464def check_python_module(conf, module_name, condition=''):
465 """
466 Check if the selected python interpreter can import the given python module::
467
468 def configure(conf):
469 conf.check_python_module('pygccxml')
470 conf.check_python_module('re', condition="ver > num(2, 0, 4) and ver <= num(3, 0, 0)")
471
472 :param module_name: module
473 :type module_name: string
474 """
475 msg = 'Python module %s' % module_name
476 if condition:
477 msg = '%s (%s)' % (msg, condition)
478 conf.start_msg(msg)
479 try:
480 ret = conf.cmd_and_log(conf.env['PYTHON'] + ['-c', PYTHON_MODULE_TEMPLATE % module_name])
481 except Exception:
482 conf.end_msg(False)
483 conf.fatal('Could not find the python module %r' % module_name)
484
485 ret = ret.strip()
486 if condition:
487 conf.end_msg(ret)
488 if ret == 'unknown version':
489 conf.fatal('Could not check the %s version' % module_name)
490
491 from distutils.version import LooseVersion
492 def num(*k):
493 if isinstance(k[0], int):
494 return LooseVersion('.'.join([str(x) for x in k]))
495 else:
496 return LooseVersion(k[0])
497 d = {'num': num, 'ver': LooseVersion(ret)}
498 ev = eval(condition, {}, d)
499 if not ev:
500 conf.fatal('The %s version does not satisfy the requirements' % module_name)
501 else:
502 if ret == 'unknown version':
503 conf.end_msg(True)
504 else:
505 conf.end_msg(ret)
32def python_version() -> None:
33 """
34 Check that the Python version is supported
35 """
36 py_version = sys.version.split()[0]
37 if py_version < "3.5":
38 print(error_message(), "Unsupported Python version")
39 sys.exit(1)
14def find_version():
15 version_file = 'torchreid/__init__.py'
16 with open(version_file, 'r') as f:
17 exec(compile(f.read(), version_file, 'exec'))
18 return locals()['__version__']

Related snippets