Python Forum
PYTHONSTARTUP not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PYTHONSTARTUP not working
#1
on Xubuntu 18.04.6 LTS when i run Python 3.6.9 interactively (no command line options or arguments) with environment variable PYTHONSTARTUP set to ".init.py" it does not run the statements in that file. what am i doing wrong? i am reading here: https://docs.python.org/3/using/cmdline....HONSTARTUP
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
To temporarily set an environment variable useexport PYTHONSTARTUP='/home/user_name/.init.pywhereuser_nameis you. To permanently set it you'll need to put it in your.bashrcfile. It is important the you use the full path and not just the file name. I've tested this on Linux Mint 20.1 Cinamon and it works like a charm.
Reply
#3
(Dec-25-2021, 09:42 PM)BashBedlam Wrote: export PYTHONSTARTUP='/home/user_name/.init.py
i did all that, myself, plus the closing quote. but, alas! i now see my error. it's not even a Python error. i forgot the "export" word. so it would just set an ordinary shell variable. python would not get it. of course it's my mistake. your missing quote made me focus on this and that led to me seeing "export". thanks!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Either that or set the variable on the command line at the same time as running the program (e.g. PYTHONSTARTUP=init.py python ...)
BashBedlam likes this post
Reply
#5
i would soon get tired of doing that and make it be more permanent.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
i want it to always read it from the user's home directory, never from the current working directory, for security reasons. so, ultimately i would set in in my ~/.bashrc file so it gets set at login or shell startup.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
As PYTHONSTARTUP is only used in interactive mode, it seems to me that a better alternative is to use the IPython shell instead of the Python shell. Apparently, if you put .py files in the directory ~/.ipython/profile_default/startup/, these files will be executed every time you start the Ipython shell.

It looks to me like a much more flexible solution than a PYTHONSTARTUP variable.
Reply
#8
perhaps it is more flexible. but i still prefer to run CPython in case there are differences. i use interactive mode very often to ensure behavior for cases of documentation that is missing or ambiguous. today i did that to verify how instances of decimal.Decimal worked in the divmod() function.

do you (anyone) use IPython for a particular reason (if so, what is the reason, and in what cases)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
Usually I don't use IPython, but I never use PYTHONSTARTUP either. 'ipython' is only an interactive shell, it runs the same CPython interpreter than the 'python' command.
Reply


Forum Jump:

User Panel Messages

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