Hi Pythonistas!
Today we will learn about sitecustomize in Python. sitecustomize.py is a Python module that allows you to customize the Python environment when it starts up. This module is automatically imported and executed by the Python interpreter each time it starts, providing an excellent opportunity to set up global configurations, imports, or other actions without needing to modify every script you write.
Let us create sitecustomize
Find the sitecustomize location using
python -m site --user-site
Create a file called sitecustomize.py
I have created another module called helloworld and contains a function called say_hello
def say_hello():
print("hello world")
My plan is to add the helloworld module to the sys.path always when Python gets started. For that, I need a sitecustomize like this
import sys
sys.path.append("helloworld")
This results in adding hellowolrd whenever Python started
Python 3.11.0 (main, Oct 26 2022, 14:44:06) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
>>> helloworld.say_hello()
hello world
>>>
Some practical situations
- Setting up the environment variable
- Adding Custom import paths (above example)
- Enforcing coding standards etc
If you need details example each of the use cases please let me know. We can make a post on this.
I hope you have learned something from this post. Please share your valuable suggestions with afsal@parseltongue.co.in