How to use the configparser module

Motivation Sometimes you need a configuration for your project to adapt to different environments. The easiest way is to alter some variables / constants in your program code. But Hold your horses. This isn’t always a good idea. What if you package your code into an executable with e.g. PyInstaller? Maybe the user of your…

How to use the sys module in Python

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,…

How to configure git

Motivation Working on different projects with different user info and settings you may find it necessary to dive deeper into the git configuration. First of all you need to know that there are at least three git configuration files system global local worktree Each level overrides values in the previous level, so local overwrites global,…

How to work with the Python os module

Motivation When you are dealing with files and directories or operating system version and environment variables the os module needs to become your friend os Get current working directory import os print(os.getcwd()) Get platform info os.name == “nt”   # Windows os.name == “posix” #  Linux and macOS Get environment variables assert os.environ.get(“FOO”) == “BAR” os.path…

Python Tips & Tricks for Junior Developers

Motivation Learning the programming language Python is easy but becoming a proficient developer can be quite cumbersome: Python has a complex ecosystem of package / dependency management, some finicky language details and of course you should learn some tricks of the trade as well. So here is the place to start your journey into the…

Running external commands from Python

Motivation Although Python has a ton of built-in features and the ecosystem is full of useful pip-installable packages sometimes you need to fire up some external tools os.system The simplest form of launching external tools is the os.system function import os os.system(“echo Hello from the other side!”)   subprocess module The subprocess module is Python’s…

What are Python properties?

In “What’s the difference between classmethod and staticmethod in Python?” we looked at static and class methods. The third big decorator is the @property decorator import pytest class Person: def __init__(self, name): self._name = name @property def name(self): print(‘Getting name’) return self._name @name.setter def name(self, value): print(f’Setting name to {value}’) self._name = value @name.deleter def…