1

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.

Mario Palumbo
  • 693
  • 8
  • 32
  • 1
    This really sounds like a job for an `if`. – user2357112 Jan 27 '22 at 12:24
  • Nit: `sys` is imported. The only difference is that the *name* `sys` is bound in the function scope, not the global scope. – chepner Jan 27 '22 at 19:10
  • If you want Python-3-specific syntax, that will have to go into a separate module that is conditionally imported. A module or script has to be parsed as a whole before any part of it can be executed. – chepner Jan 27 '22 at 19:10
  • What is `Nit`?. – Mario Palumbo Jan 27 '22 at 19:18
  • A minor issue in something you said. If you `import sys`, no matter where you run it, the module is imported. (In fact, `sys` and others are *built-in* modules in CPython, meaning the module is available whether you use `import` or not.) – chepner Jan 27 '22 at 19:37
  • This question has already interesting solutions [here](https://stackoverflow.com/questions/7346984/pythonstartup-with-python-2-7-and-python-3-2), where the "isolating syntax" feature you require is handled either with a [sub-import](https://stackoverflow.com/a/7347047/7212665), or by a [wrapper alias](https://stackoverflow.com/a/7347161/7212665). – Demi-Lune Jan 28 '22 at 12:59
  • Alias is not a good solution while the import would be a solution appropriate. Thank you. – Mario Palumbo Jan 28 '22 at 15:52
  • The problem is that the import works only if file ending with .py extention and not begin with a point, then if in the startup file `.pythonrc` I put the code: `from .example import *`, with the script file called `.example.py`, the import doesn't work. – Mario Palumbo Jan 28 '22 at 16:18

1 Answers1

1

It's a python script, so simply check sys.version:

# ~/.pythonrc
import sys
if (sys.version_info.major < 3):
    print('py2, do nothing')
    quit()
    

print('do something')

Demi-Lune
  • 1,868
  • 2
  • 15
  • 26
  • This is the solution they already know about. They specifically asked for something else. – user2357112 Jan 27 '22 at 12:25
  • You're right, I misread the question. How about an early quit()? – Demi-Lune Jan 27 '22 at 12:30
  • We too are mystified why they don't want this, but they clearly say they don't. – tripleee Jan 27 '22 at 12:31
  • I would like to know if there is a way to prevent this outside of the script. Even an answer that says that this way does not exist (citing a source) is accepted. – Mario Palumbo Jan 27 '22 at 12:35
  • PYTHONSTARTUP is an envvar shared by all python versions. So your best option would be to have a "first entry" file, that imports your specific config files (call them e.g. ~/.python36rc, ~/.python39rc, ~/.python27rc, etc.) depending on sys.version_info – Demi-Lune Jan 27 '22 at 14:49
  • Maybe you should explain why you don't want to open the file in py2, what you plan to do in this file, etc. – Demi-Lune Jan 27 '22 at 14:51
  • Another (bash) solution is to alias : `alias mypython3="PYTHONSTARTUP=~/pythonrc /usr/bin/python3"`, there are plenty of such possible tricks. – Demi-Lune Jan 27 '22 at 14:53
  • Ok, I have explain the motivation. – Mario Palumbo Jan 27 '22 at 19:03