如何告诉Python脚本使用特定版本 [英] How do I tell a Python script to use a particular version

查看:178
本文介绍了如何告诉Python脚本使用特定版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在main.py模块(大概)中告诉Python使用哪个解释器?我的意思是:如果我想让特定脚本使用Python版本3解释整个程序,该怎么做?

How do I, in the main.py module (presumably), tell Python which interpreter to use? What I mean is: if I want a particular script to use version 3 of Python to interpret the entire program, how do I do that?

奖金:这将如何影响virtualenv?我是否在想,如果我为程序创建一个virtualenv然后告诉它使用其他版本的Python,那么我可能会遇到一些冲突吗?

Bonus: How would this affect a virtualenv? Am I right in thinking that if I create a virtualenv for my program and then tell it to use a different version of Python, then I may encounter some conflicts?

推荐答案

您可以在脚本顶部添加一个shebang行:

You can add a shebang line the to the top of the script:

#!/usr/bin/env python2.7

但这仅在以./my_program.py执行时有效.

But that will only work when executing as ./my_program.py.

如果您以python my_program.py执行,则将使用which python返回的任何Python版本.

If you execute as python my_program.py, then the whatever Python version that which python returns will be used.

在re:中,要使用virtualenv:virtualenv -p /usr/bin/python3.2或将其设置为使用该Python可执行文件的任何方法.

In re: to virtualenv use: virtualenv -p /usr/bin/python3.2 or whatever to set it up to use that Python executable.

这篇关于如何告诉Python脚本使用特定版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆