Skip to content

Tutorial: python_utils

Paul Alexander Bilokon edited this page Dec 15, 2024 · 1 revision

Python Utils Module Tutorial

This tutorial introduces the key utilities provided in the python_utils module and how to use them.

Overview

The python_utils module provides utilities for working with Python scripts and executables. The main functionalities include:

  • Getting the path to the Python executable.
  • Getting the path to the Scripts directory.

Functions

1. get_path_to_python_executable

Description

This function retrieves the full path to the currently running Python executable.

Example Usage

import thalesians.adiutor.python_utils as python_utils

path_to_python_executable = python_utils.get_path_to_python_executable()
print(f"Path to Python executable: {path_to_python_executable}")

Expected Output

The function returns the full path to the Python executable as a string. For example:

/usr/bin/python

or on Windows:

C:\Python39\python.exe

Unit Test

The function is tested with the following assertions:

  • The result is a string.
  • The result ends with python or python.exe.
  • The result points to an existing file.

2. get_path_to_scripts

Description

This function retrieves the path to the Scripts directory, commonly used in virtual environments or Python installations for storing executable scripts.

Example Usage

import thalesians.adiutor.python_utils as python_utils

path_to_scripts = python_utils.get_path_to_scripts()
print(f"Path to Scripts directory: {path_to_scripts}")

Expected Output

The function returns the full path to the Scripts directory as a string. For example:

/usr/local/bin/Scripts

or on Windows:

C:\Python39\Scripts

Unit Test

The function is tested with the following assertions:

  • The result is a string.
  • The result ends with Scripts.
  • The result points to an existing directory.

Running the Unit Tests

To verify the functionality of the python_utils module, you can run the following unit tests:

import unittest
import thalesians.adiutor.python_utils as python_utils

class TestPythonUtils(unittest.TestCase):
    def test_get_path_to_python_executable(self):
        path_to_python_executable = python_utils.get_path_to_python_executable()
        self.assertTrue(isinstance(path_to_python_executable, str))
        self.assertTrue(path_to_python_executable.endswith('python') or path_to_python_executable.endswith('python.exe'))
        self.assertTrue(os.path.exists(path_to_python_executable))

    def test_get_path_to_scripts(self):
        path_to_scripts = python_utils.get_path_to_scripts()
        self.assertTrue(isinstance(path_to_scripts, str))
        self.assertTrue(path_to_scripts.endswith('Scripts'))
        self.assertTrue(os.path.exists(path_to_scripts))

if __name__ == '__main__':
    unittest.main()

Summary

The python_utils module simplifies the process of locating important paths related to Python installations and environments. The functions are especially useful for scripting and automation in Python projects.

Clone this wiki locally