Python Forum
initializing interactive mode - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: initializing interactive mode (/thread-42216.html)



initializing interactive mode - Skaperen - May-29-2024

i recall reading that interactive mode could be initialized with either a command line option or the file .init.py instead. i tried the latter but it seems to have no effect. is that the correct name to do this?


RE: initializing interactive mode - Gribouillis - May-29-2024

You could start the interpreter with the -i option
Output:
λ echo 'x = 1009' > spam.py λ λ python -i spam.py >>> x 1009 >>>
Second way, using an environment variable
Output:
λ PYTHONSTARTUP=spam.py python Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x 1009 >>>
Note that you cannot combine the two ways because with the PYTHONSTARTUP is not read if you start python with -i
Output:
λ echo 'y = 9000' > eggs.py λ PYTHONSTARTUP=spam.py python -i eggs.py >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined >>> y 9000 >>>
Here is a third untested way, if you don't want to define PYTHONSTARTUP (I never define permanently python-related environment variables). In file usercustomize.py write this
# in usercustomize.py
import sys
if hasattr(sys, 'ps1'): # we are in interactive mode
    with open('spam.py') as ifh:
        exec(ifh.read(), vars(sys.modules['__main__']))



RE: initializing interactive mode - Skaperen - May-29-2024

(May-29-2024, 07:58 PM)Gribouillis Wrote: Here is a third untested way, if you don't want to define PYTHONSTARTUP (I never define permanently python-related environment variables). In file usercustomize.py write this
# in usercustomize.py
import sys
if hasattr(sys, 'ps1'): # we are in interactive mode
    with open('spam.py') as ifh:
        exec(ifh.read(), vars(sys.modules['__main__']))

could i just put some other code in place of open+exec lines, under the test for interactive, to do what i want to happen in interactive?

can file usercustomize.py be put in the user's home directory?


RE: initializing interactive mode - Gribouillis - May-30-2024

(May-29-2024, 11:48 PM)Skaperen Wrote: can file usercustomize.py be put in the user's home directory?
usercustomize.py needs to be importable, that is to say on the python path. Typically, the right place would be the directory returned by site.getusersitepackages().
(May-29-2024, 11:48 PM)Skaperen Wrote: could i just put some other code in place of open+exec lines, under the test for interactive, to do what i want to happen in interactive?
I tested the above code and it doesn't work! It seems that sys.ps1 is set after usercustomize is imported. Here is an alternative that does what you're asking.
# in usercustomize.py
import sys
import __main__
if (not sys.argv[0]) or sys.flags.interactive:
    # sys.argv[0] is '' in interactive mode
    # sys.flags.interactive is True if -i flag is present
    print('We are in interactive mode')
    exec('x =  3.14', vars(__main__))
The interactivity test may not work on all python interpreters (such as embedded interpreters in IDEs or files compiled to .exe etc). At least it works well in the python interpreter installed in Linux.

Notice I exec the code in the namespace of the __main__ module because I want to make an initialized variable x available at the interpreter prompt.


RE: initializing interactive mode - Skaperen - Jun-02-2024

in user phil where i am doing this with a preset PYTHONSTARTUP=/home/phil/.init.py environment variable, file /home/phil/.init.py does preset a lot of variables including x. it also imports a few things i usually use in interactive mode.

your example does work, so i do have a choice. Thanks.