3

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).

mmnormyle
  • 763
  • 1
  • 8
  • 18
  • Are you looking to do this only for starting an interactive session, or no . matter _how_ Python is started (so every time you run a script or `-m` a module, it'll happen there as well)? – abarnert Jun 27 '18 at 21:31
  • 1
    Why not alias the python binary and be done with it? – zwer Jun 27 '18 at 21:36
  • @zwer But alias it to _what_? If you write, say, a bash script that runs Python, feeds in a heredoc, and then drops to real stdin, you can't use that as an executable in a shbang line on most platforms. – abarnert Jun 27 '18 at 21:54

3 Answers3

6

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 the site-packages directory.

After this, an attempt is made to import a module named usercustomize, which can perform arbitrary user-specific customizations, if ENABLE_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.

Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 2
    `sitecustomize.py` doesn't seem to print anything when I add prints to it. – Velkan Mar 25 '22 at 16:52
  • usercustomize.py runs automatically but does not store variables and functions, can you help me?: https://stackoverflow.com/questions/76438582 – Mario Palumbo Jun 09 '23 at 09:09
2

The Python mechanism is called... site. It is a module that is automatically imported and sets up your environment. If it finds modules sitecustomize.py and usercustomize.py, it will import them as well. So these two are where you would put site-wide and personal customizations that you want to be a permanent part of your Python environment. See the site documentation for more details.

alexis
  • 48,685
  • 16
  • 101
  • 161
0

File pointed by your environmental variable PYTHONSTARTUP would be run on starting an interactive python session

And USER_SITE/usercustomize.py will be run for non-interactive python session.

Run python -c "import site; print(site.USER_SITE)" to find the USER_SITE dir location

Sunitha
  • 11,777
  • 2
  • 20
  • 23