Python Forum

Full Version: PYTHONSTARTUP not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
(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!
Either that or set the variable on the command line at the same time as running the program (e.g. PYTHONSTARTUP=init.py python ...)
i would soon get tired of doing that and make it be more permanent.
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.
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.
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)
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.