简体   繁体   English

$ PYTHONSTARTUP与python 2.7和python 3.2

[英]$PYTHONSTARTUP with python 2.7 and python 3.2

I finally started using python 3 alongside python 2.7 on Linux. 我终于开始在Linux上使用python 3和python 2.7。

I setup my python shell using a startup-script defined by $PYTHONSTARTUP. 我使用$ PYTHONSTARTUP定义的启动脚本设置我的python shell。 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? 为python 2.7获取一个脚本的最简单方法是什么,另一个用于python 3.2的脚本是什么?

If you use Python 2 for some projects and Python 3 for others, then change the environment variable when you change projects. 如果您将Python 2用于某些项目而将Python 3用于其他项目,则在更改项目时更改环境变量。

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 并将您的真实启动代码拆分为startup2.pystartup3.py

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: 将$ PYTHONSTARTUP设置为指向这样的脚本,该脚本检查正在使用的Python版本,然后委托给另一个启动脚本:

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

Define an alias for one of the python versions. 为其中一个python版本定义别名。 In the alias, reset PYTHONSTARTUP as appropriate for that python version: 在别名中,根据该python版本重置PYTHONSTARTUP:

alias py3='PYTHONSTARTUP=/path/to/startup.py /other/path/to/python3.2'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM