10 examples of 'python check version of module' in Python

Every line of 'python check version of module' 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
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]))
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

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)
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
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)
328def check(modname):
329 """Check if required dependency is installed"""
330 for dependency in DEPENDENCIES:
331 if dependency.modname == modname:
332 return dependency.check()
333 else:
334 raise RuntimeError("Unkwown dependency %s" % modname)
102def print_py_pkg_ver(pkgname, modulename=None):
103 if modulename is None:
104 modulename = pkgname
105 print
106 try:
107 import pkg_resources
108 out = str(pkg_resources.require(pkgname))
109 print pkgname + ': ' + foldlines(out)
110 except (ImportError, EnvironmentError):
111 sys.stderr.write("\nGot exception using 'pkg_resources' to get the version of %s. Exception follows.\n" % (pkgname,))
112 traceback.print_exc(file=sys.stderr)
113 sys.stderr.flush()
114 pass
115 except pkg_resources.DistributionNotFound:
116 print pkgname + ': DistributionNotFound'
117 pass
118 try:
119 __import__(modulename)
120 except ImportError:
121 pass
122 else:
123 modobj = sys.modules.get(modulename)
124 print pkgname + ' module: ' + str(modobj)
125 try:
126 print pkgname + ' __version__: ' + str(modobj.__version__)
127 except AttributeError:
128 pass
196def check_11_28(self):
197 self.check_version('cpanel-lve', '0.2')
198 self.check_version('cpanel-lvemanager', '0.2')
199 self.check_version('lve-cpanel-plugin', '0.1')
200 if self.apache.check_version():
201 if not self.apache.isModule('hostinglimits'):
202 print_error(3011, "hostinglimits module not installed", (), \
203 "Recompile Apache via EasyApache. You can do it either through WHM, or by running /scripts/easyapache --build")
8def checkPython(context):
9 context.Message("Check for python..")
10 context.Result(0)
11 return False
109def test_pypy(self):
110 self.assertEqual(0, self._execute_code('pypy'))

Related snippets