hbutils.system.python¶
python_version¶
is_cpython¶
is_ironpython¶
is_jython¶
is_pypy¶
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, returnNone
.
- 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
andsys.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
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]¶ - 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 isFalse
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 inpip
.
- 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 isFalse
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 inpip
.
- Examples::
>>> from hbutils.system import pip_install_req_file >>> pip_install_req_file('requirements.txt') # pip install -r requirements.txt