hbutils.system.python

Python system utilities module.

This module provides utilities for working with Python implementation details, package management, and version information. It aggregates functionality from three main submodules:

  • implementation: Python implementation detection and information

  • package: Package management and introspection utilities

  • version: Python version parsing and comparison utilities

All public functions and classes from these submodules are re-exported at the module level for convenient access.

Example::
>>> from hbutils.system.python import get_python_version
>>> version = get_python_version()
>>> print(version)
3.8.10

python_version

hbutils.system.python.python_version() Version[source]

Get version of python.

This function retrieves the current Python interpreter version and returns it as a Version object from the packaging library, which allows for easy version comparison and manipulation.

Returns:

Version of python.

Return type:

Version

Examples::
>>> from hbutils.system import python_version
>>>
>>> python_version()
Version('3.8.1')  # for example

is_cpython

hbutils.system.python.is_cpython() bool[source]

Check if the current Python implementation is CPython.

Overview:

Return True if current python is CPython, otherwise return False.

Returns:

Current python is CPython or not.

Return type:

bool

Example::
>>> is_cpython()
True  # When running on CPython

is_ironpython

hbutils.system.python.is_ironpython() bool[source]

Check if the current Python implementation is IronPython.

Overview:

Return True if current python is IronPython, otherwise return False.

Returns:

Current python is IronPython or not.

Return type:

bool

Example::
>>> is_ironpython()
False  # When running on CPython

is_jython

hbutils.system.python.is_jython() bool[source]

Check if the current Python implementation is Jython.

Overview:

Return True if current python is Jython, otherwise return False.

Returns:

Current python is Jython or not.

Return type:

bool

Example::
>>> is_jython()
False  # When running on CPython

is_pypy

hbutils.system.python.is_pypy() bool[source]

Check if the current Python implementation is PyPy.

Overview:

Return True if current python is PyPy, otherwise return False.

Returns:

Current python is PyPy or not.

Return type:

bool

Example::
>>> is_pypy()
False  # When running on CPython

package_version

hbutils.system.python.package_version(name: str) Version | None[source]

Get version of package with given name.

Parameters:

name (str) – Name of the package, case is not sensitive.

Returns:

A packaging.version.Version object. If the package is not installed, return None.

Return type:

Optional[Version]

Examples::
>>> from hbutils.system import package_version
>>>
>>> package_version('pip')
<Version('21.3.1')>
>>> package_version('setuptools')
<Version('59.6.0')>
>>> package_version('not_a_package')
None

load_req_file

hbutils.system.python.load_req_file(requirements_file: str) List[str][source]

Load requirements items from a requirements.txt file.

Parameters:

requirements_file (str) – Requirements file path.

Returns:

List of requirements.

Return type:

List[str]

Examples::
>>> from hbutils.system import load_req_file
>>> load_req_file('requirements.txt')
['packaging>=21.3', 'setuptools>=50.0']

pip

hbutils.system.python.pip(*args, silent: bool = False)[source]

Run pip command with code.

Parameters:
  • args – Command line arguments for pip command.

  • silent (bool) – Do not print anything. Default is False, which means print the output to sys.stdout and sys.stderr.

Raises:

AssertionError – If pip command returns non-zero exit code.

Examples::
>>> from hbutils.system import pip
>>> pip('-V')
pip 22.3.1 from /home/user/myproject/venv/lib/python3.7/site-packages/pip (python 3.7)
>>> pip('-V', silent=True)  # nothing will be printed

check_reqs

hbutils.system.python.check_reqs(reqs: List[str]) bool[source]

Check if the given requirements are all satisfied.

Parameters:

reqs (List[str]) – List of requirements.

Returns:

All the requirements in reqs satisfied or not.

Return type:

bool

Examples::
>>> from hbutils.system import check_reqs
>>> check_reqs(['pip>=20.0'])
True
>>> check_reqs(['pip~=19.2'])
False
>>> check_reqs(['pip>=20.0', 'setuptools>=50.0'])
True

Note

If a requirement’s marker is not satisfied in this environment, it will be ignored instead of return False.

check_req_file

hbutils.system.python.check_req_file(requirements_file: str) bool[source]

Check if the requirements in the given requirements_file is satisfied.

Parameters:

requirements_file (str) – Requirements file, such as requirements.txt.

Returns:

All the requirements in requirements_file satisfied or not.

Return type:

bool

Examples::
>>> from hbutils.system import check_req_file
>>>
>>> check_req_file('requirements.txt')
True
>>> check_req_file('requirements-test.txt')
True

pip_install

hbutils.system.python.pip_install(reqs: List[str], silent: bool = False, force: bool = False, user: bool = False)[source]

Pip install requirements with code.

Similar to pip install req1 req2 ....

Parameters:
  • reqs (List[str]) – Requirement items to install.

  • silent (bool) – Do not print anything. Default is False.

  • force (bool) – Force execute the pip install command. Default is False which means the requirements will be checked before installation, and the installation will be only executed when some requirements not installed.

  • user (bool) – User mode, represents --user option in pip.

Examples::
>>> from hbutils.system import pip_install
>>> pip_install(['scikit-learn'])  # not installed
Looking in indexes: https://xxx/simple
Collecting scikit-learn
  Using cached https://xxx/scikit_learn-1.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (24.8 MB)
Installing collected packages: threadpoolctl, scipy, joblib, scikit-learn
Successfully installed joblib-1.2.0 scikit-learn-1.0.2 scipy-1.7.3 threadpoolctl-3.1.0
>>> pip_install(['numpy>=1.10.0'])  # installed
>>> pip_install(['numpy>=1.10.0'], force=True)  # force execute
Looking in indexes: https://xxx/simple
Requirement already satisfied: numpy>=1.10.0 in ./venv/lib/python3.7/site-packages (1.21.6)

pip_install_req_file

hbutils.system.python.pip_install_req_file(requirements_file: str, silent: bool = False, force: bool = False, user: bool = False)[source]

Pip install requirements from file with code.

Similar to pip install -r requirements.txt.

Parameters:
  • requirements_file (str) – Requirements file, such as requirements.txt.

  • silent (bool) – Do not print anything. Default is False.

  • force (bool) – Force execute the pip install command. Default is False which means the requirements will be checked before installation, and the installation will be only executed when some requirements not installed.

  • user (bool) – User mode, represents --user option in pip.

Examples::
>>> from hbutils.system import pip_install_req_file
>>> pip_install_req_file('requirements.txt')  # pip install -r requirements.txt