mypythontools.system package

Add some stuff to python subprocess, sys, os and platform libraries.

You can find here formatting errors, resolving venv script paths, finding script path, unifying syntax for linux and windows etc. Most used function is ‘terminal_do_command’ comunicating with system shell. Use strings as input and if there can be space in some command, use ‘get_console_str_with_quotes’.

mypythontools.system.check_library_is_available(name, message='default', extras: str | None = None)[source]

Make one-liner for checking whether some library is installed.

If running on venv, it checks only this venv, no global site packages.

Note

This function has much worse performance, than simple try with ModuleNotFoundError error. This is beneficial only if don’t know the name of a package.

Parameters
  • name (str) – Name of the library.

  • message (str, optional) – Message that will be printed when library not installed. Defaults to “default”.

  • extras (str, optional) – Sometimes there are sections of requirements that are installable with extra pip command e.g. pip install mypythontools[plots]. If you define a name of a library with name of extras, for example ‘mypythontools[plots]’, it will be printed to the user, so he is able to install all libraries at once. Defaults to None.

Raises

ModuleNotFoundError – If module is installed, error is raised.

Example

>>> check_library_is_available("typing_extensions")
>>> check_library_is_available("not_installed_lib")
Traceback (most recent call last):
ModuleNotFoundError: ...
mypythontools.system.check_script_is_available(name, install_library=None, message='default')[source]

Check if python script is available.

This doesn’t need to be installed in current venv, but anywhere on computer.

Parameters
  • name (str) – Name of the script. E.g “black’.

  • install_library (str, optional) – Install script with this library added to default message. Defaults to None.

  • message (str, optional) – Message that will be printed when library not installed. Defaults to “default”.

Raises

RuntimeError – If module is installed, error is raised.

Example

>>> check_script_is_available("pip")
>>> check_script_is_available("not_existing_script")
Traceback (most recent call last):
RuntimeError: ...
mypythontools.system.get_console_str_with_quotes(string: Union[Path, str])[source]

In terminal if value or contain spaces, it’s not taken as one param.

This wraps it with quotes to be able to use paths and values as needed. Alternative to this function is to use python shlex library, list of commands and ‘shlex.join’ to get the command string.

Parameters

string (str, Path) – String to be edited.

Returns

Wrapped string that can be used in terminal.

Return type

str

Example

>>> get_console_str_with_quotes("/path to file/file")
'"/path to file/file"'
mypythontools.system.is_wsl()[source]

Check whether script runs in Windows Subsystem for Linux or not.

Returns

True or False based on using wsl.

Return type

bool

mypythontools.system.terminal_do_command(command: str, shell: bool = True, cwd: None | PathLike = None, verbose: bool = True, error_header: str = '', with_wsl: bool = False, input_str: None | str = None) str[source]

Run command in terminal and process output.

Parameters
  • command (str) – Command to run.

  • shell (bool, optional) – Same meaning as in subprocess.run(). Defaults to False.

  • cwd (None | PathLike, optional) – Same meaning as in subprocess.run(). Defaults to None.

  • verbose (bool, optional) – Whether print output to console. Defaults to True.

  • error_header (str, optional) – If meet error, message at the beginning of message. Defaults to “”.

  • with_wsl (bool, optional) – Prepend wsl prefix and edit command so it works. Path needs to be edited manually (for example with wsl_pathlib package). Defaults to False

  • input_str (None | str) – Input inserted into terminal. It can be answer y for example if terminal needs an confirmation. Defaults to None.

Raises

RuntimeError – When process fails to finish or return non zero return code.

Example

>>> res = terminal_do_command("python --version")

Python ...
mypythontools.system.which(name) None | Path[source]

Return path of defined script.

It’s similar to whichcraft.which or shutil.which with the difference that it will work also if calling with wsl python code_with_which.py. If script not available, None is returned. If it’s found, pathlib Path is returned, not string.

Parameters

name (str) – Name of the script. For example python, ‘black’, or ‘pytest’.

Returns

Path to script or None if it’s not available.

Return type

None | Path

Example

>>> which("pip").exists()
True