In the R programming language, there is a site.profile file that defines some code that R processes execute on start up. Is there similar functionality in Python?
Edit: to clarify, this script should be executed if the user calls python from the command line, but also if python is spawned from another process (e.g. if the user's script uses subprocess to spawn another python).
If you only want this for interactive sessions (as opposed to also happening every time you run a script with python myscript.py
or ./myscript
or a module with python -m mymodule
), what you want is the environment variable PYTHONSTARTUP
:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session…
If you want this to always happen forever, of course, you need to set this environment variable in some appropriate global place—e.g., your shell profile on most *nix platforms, or both your shell profile and your launchd profile on macOS, or the appropriate part of the Control Panel on Windows (the appropriate part changes with almost every new version of Windows, but it usually has "System" in the name).
If you want this to happen for all users, not just the current user… the details for how to set a system-wide environment variable are more platform-specific, but otherwise the idea is the same.
If you want this to happen for every Python session, even when some other program is running a Python script and you didn't even know it was doing that… what you want is either usercustomize
or sitecustomize
, as documented in the site
documentation:
This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s -S option.
…
After these path manipulations, an attempt is made to import a module named
sitecustomize
, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in thesite-packages
directory.After this, an attempt is made to import a module named
usercustomize
, which can perform arbitrary user-specific customizations, ifENABLE_USER_SITE
is true. This file is intended to be created in the user site-packages directory (see below), which is part of sys.path unless disabled by-s
…
So, you want to find an appropriate place to override this. First try this:
python3 -m site
Then, if this didn't give you sys.path
(probably only on pretty old Python, but just in case…), also do this:
python3 -c "import sys; print('\n'.join(sys.path))"
If you want this customization to happen only for the current user, you want to create a usercustomize.py
file in the USER_SITE
directory listed by python3 -m site
. If the directory doesn't exist, create it.
If you want it to happen for all users, you want a sitecustomize.py
file in one of the sys.path
directories. The problem is that there may already be one. For example, most linux distros' builtin Python packages have their own sitecustomize
modules. If there is, python3 -c 'import sitecustomize; print(sitecustomize.__file__)
will tell you where it is. Then, you can edit, or you can copy it, edit that copy, and place that copy somewhere that comes earlier in sys.path
than the original. As a general rule, /usr/local
is probably better than /usr
, and site-packages
is probably better than dist-packages
is probably better than anything else.