-
Notifications
You must be signed in to change notification settings - Fork 2
Tutorial: python_utils
This tutorial introduces the key utilities provided in the python_utils module and how to use them.
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
Scriptsdirectory.
This function retrieves the full path to the currently running Python executable.
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}")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
The function is tested with the following assertions:
- The result is a string.
- The result ends with
pythonorpython.exe. - The result points to an existing file.
This function retrieves the path to the Scripts directory, commonly used in virtual environments or Python installations for storing executable scripts.
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}")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
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.
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()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.