Python Forum
initializing interactive mode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
initializing interactive mode
#2
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__']))
« We can solve any problem by introducing an extra level of indirection »
Reply


Messages In This Thread
initializing interactive mode - by Skaperen - May-29-2024, 07:20 PM
RE: initializing interactive mode - by Gribouillis - May-29-2024, 07:58 PM
RE: initializing interactive mode - by Skaperen - May-29-2024, 11:48 PM
RE: initializing interactive mode - by Gribouillis - May-30-2024, 06:25 AM
RE: initializing interactive mode - by Skaperen - Jun-02-2024, 03:29 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020