I have followed the instructions in this topic: Is there a way to always execute a script when python starts? (similar site.profile in R)
Part of the output of python3 -m site
:
USER_SITE: '/home/<myuser>/.local/lib/python3.10/site-packages' (exists)
ENABLE_USER_SITE: True
I have this usercustomize.py:
variabile = 'Ciao Mondo.'
print(variabile)
Gnome terminal:
<myuser>@<myuser>-<mypc>:~$ python3
Ciao Mondo.
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(variabile)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'variabile' is not defined
>>>
As you can see, usercustomize.py
runs automatically and prints the value of variabile
but doesn't store it, neither when I run a script that requests it, nor from the console like in the example above.
The solution can't be in PYTHONSTARTUP
, because I need to run this file not only when I work in interactive mode but also from scripts.
How can I solve the problem?
To make a variable that is available to the rest of Python, you can add it to the builtins
module.
import builtins
builtins.variabile = variabile