Thanks to this assignment:
export PYTHONSTARTUP=~/.pythonrc
I can start an initialization script as soon as I run Python, a bit like the .bashrc file. The problem is that this works on both Python2 and Python3, instead I want to prevent Python2 from doing this, due to hassles with code in .pythonrc.
I know I can put an If in the .pythonrc file to block its execution in Python2, but I would like to avoid doing this and stop it from running the script in the bud.
Is there a possible way to do this or an alternative variable that works only with Python3 equivalent to PYTHONSTARTUP
(eg. PYTHON3STARTUP
)?
All solutions that allow me not to insert code inside the .pythonrc file are accepted.
Even an answer that says that this way does not exist (citing a source or providing evidence) is accepted.
Why do I want a solution that is outside of the script? I'll explain it to you with an example:
def python_major_version():
import sys
return sys.version_info.major
python_major_version = python_major_version()
if python_major_version == 3:
# Incompatible syntax block with Python2
The scope of the sys module is only inside the function, as I want.
python_major_version = python_major_version()
: This is my method of assigning the Python version number to a variable, without importing the sys
module.
When I run Python2 from the terminal, despite the If
that prevents the block from being executed, the syntax of all the code is still checked, giving me the error of SyntaxError: invalid syntax
.