Table of Contents
Motivation
Sometimes you need information about your system e.g. which Python version your program is running on.
Enter the sys module
Import
import sys
How to find the currently active Python interpreter
current_interpreter = sys.executable
assert current_interpreter == "/Users/jb/PycharmProjects/blog_content_creatronix/venv311/bin/python"
How find out about your Python version
sys.version
assert sys.version == "3.11.0rc1 (v3.11.0rc1:41cb07120b, Aug 5 2022, 11:44:46) [Clang 13.0.0 (clang-1300.0.29.30)]"
sys.version_info
With version_info ou can access the semantic version independently
assert str(sys.version_info) == "sys.version_info(major=3, minor=11, micro=0, releaselevel='candidate', serial=1)"
assert sys.version_info.major == 3
assert sys.version_info.minor == 11
assert sys.version_info.micro == 0
How to exit a program gracefully
sys.exit(0)
Which gives you
Process finished with exit code 0
Sometimes you want to abort a program when certain conditions are not met.
e.g. a folder does not exist in this case you can use a non-zero exit code to issue that the program did not successfully terminate
sys.exit(1)
Further Reading
https://docs.python.org/3/library/sys.html
How to work with the Python os module
Python Tips & Tricks for Junior Developers
How to structure your Python project