hbutils.system.python

python_version

hbutils.system.python.python_version() → packaging.version.Version[source]
Overview:

Get version of python.

Returns:

Version of python.

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

is_cpython

hbutils.system.python.is_cpython() → bool[source]
Overview:

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

Returns:

Current python is CPython or not.

is_ironpython

hbutils.system.python.is_ironpython() → bool[source]
Overview:

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

Returns:

Current python is IronPython or not.

is_jython

hbutils.system.python.is_jython() → bool[source]
Overview:

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

Returns:

Current python is Jython or not.

is_pypy

hbutils.system.python.is_pypy() → bool[source]
Overview:

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

Returns:

Current python is PyPy or not.

package_version

hbutils.system.python.package_version(name: str) → Optional[packaging.version.Version][source]
Overview:

Get version of package with given name.

Parameters:

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

Returns:

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

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]
Overview:

Load requirements items from a requirements.txt file.

Parameters:

requirements_file – Requirements file.

Return requirements:

List of requirements.

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]
Overview:

Run pip command with code.

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

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

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]
Overview:

Check if the given requirements are all satisfied.

Parameters:

reqs – List of requirements.

Return satisfied:

All the requirements in reqs satisfied or not.

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

check_req_file

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

Check if the requirements in the given requirements_file is satisfied.

Parameters:

requirements_file – Requirements file, such as requirements.txt.

Return satisfied:

All the requirements in requirements_file satisfied or not.

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]
Overview:

Pip install requirements with code. Similar to pip install req1 req2 ....

Parameters:
  • reqs – Requirement items to install.

  • silent – Do not print anything. Default is False.

  • force – 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 – 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]
Overview:

Pip install requirements from file with code. Similar to pip install -r requirements.txt.

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

  • silent – Do not print anything. Default is False.

  • force – 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 – 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