13

I finally started using python 3 alongside python 2.7 on Linux.

I setup my python shell using a startup-script defined by $PYTHONSTARTUP. Due to incompatibilities I am unable to use the same script for both versions.

What is the easiest way to get one script for python 2.7, and another for python 3.2?

mirk
  • 5,302
  • 3
  • 32
  • 49

3 Answers3

14

If you use Python 2 for some projects and Python 3 for others, then change the environment variable when you change projects.

Or, have your startup script look like this:

import sys
if sys.version_info[0] == 2:
    import startup2
else:
    import startup3

and split your real startup code into startup2.py and startup3.py

nandhp
  • 4,680
  • 2
  • 30
  • 42
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I went for the startup-script option, and wrote something similar to your code. I picked this answer because it was the first to suggest this solution. – mirk Sep 08 '11 at 12:11
  • 1
    `AttributeError: 'module' object has no attribute 'versioninfo'`. It's supposed to be `version_info`. – 2rs2ts Jul 12 '13 at 16:01
7

Set your $PYTHONSTARTUP to point to a script like this, which checks the version of Python being used, and then delegates to another startup script:

import sys
if sys.version_info[0]==2:
    from startup2k import *
elif sys.version_info[0]==3:
    from startup3k import *
else:
    # logging.warn('Unsupported version of Python')
    pass
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
2

Define an alias for one of the python versions. In the alias, reset PYTHONSTARTUP as appropriate for that python version:

alias py3='PYTHONSTARTUP=/path/to/startup.py /other/path/to/python3.2'
Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88