简体   繁体   English

Shebang系列为Python 2.7

[英]Shebang line for Python 2.7

I have installed Python2.7 in my Linux Centos which comes with a default Python2.6 installation, 我在我的Linux Centos中安装了Python2.7,它安装了默认的Python2.6,

Default Python 默认Python

[root@linuxhost PythonProjects]# python -V
Python 2.6.6

Default Python2.7 默认Python2.7

[root@linuxhost PythonProjects]# python2.7 -V
Python 2.7.3

Now I need to write programs based on python2.7 version...What would be the shebang line for python2.7 现在我需要编写基于python2.7版本的程序...... python2.7的shebang行是什么

Also, how will I compile using python2.7. 另外,我将如何使用python2.7进行编译。

Shebang will be: Shebang将是:

#!/usr/bin/env python2.7

I'm not sure why you want to compile Python files (Python will do it automatically when you import them). 我不确定你为什么要编译Python文件(Python会在你导入它们时自动完成)。

If you really want to: 如果你真的想:

python2.7 -m compileall .

This command will compile .py files in the current directory to .pyc: 此命令将当前目录中的.py文件编译为.pyc:

I don think a sheebang line alone would do it. 我不认为只有一个sheebang线就能做到。

You could try putting in 你可以试试投入

#! /usr/bin/env python2.7

though. 虽然。 - But the thing that really would be consistent for you would be to use virtual python environments through virtualenv. - 但真正与你保持一致的是通过virtualenv使用虚拟python环境。

Please, check http://www.virtualenv.org/en/latest/virtualenv.html - Otherwise you will risk having code running with the 2.7 Python but trying to load python 2.6 modules and libraries, and worse scenarios still. 请查看http://www.virtualenv.org/en/latest/virtualenv.html - 否则您将面临使用2.7 Python运行代码但尝试加载python 2.6模块和库的风险,以及更糟糕的情况。

Also, the recommendation for having two same-major-version Python in a system is to keep both in different prefixes, like the system one in /usr, and the other in /opt (/usr/local won't suffice for a clear separation). 此外,在系统中使用两个相同主要版本的Python的建议是将两者保持在不同的前缀中,例如/ usr中的系统,以及/ opt中的另一个(/ usr / local不足以清除分离)。

You could change the shebang to #!/usr/bin/env python2.7 . 您可以将shebang更改为#!/usr/bin/env python2.7

Or you could use environment modules and let the shebang be #!/usr/bin/env python . 或者你可以使用环境模块 ,让shebang成为#!/usr/bin/env python The when you load the python 2.7 module (which can be your default), the script is run with python 2.7, and when you load the python 2.6 module, the script is run with python 2.6. 当您加载python 2.7模块(可以是您的默认设置)时,脚本使用python 2.7运行,当您加载python 2.6模块时,脚本使用python 2.6运行。

On this box I have python 2.6 and 2.7 installed. 在这个盒子上我安装了python 2.6和2.7。 Depending on the module that is loaded, the selected version of python is run. 根据加载的模块,运行所选的python版本。 Libraries, modules and packages are consistently loaded for the correct version. 始终为正确的版本加载库,模块和包。

$ cat t.py 
#!/bin/env python
import sys
print(sys.version)
$ ./t.py 
2.6.6 (r266:84292, Jul 10 2013, 22:48:45) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
$ module load python/2.7.3 
$ ./t.py 
2.7.3 (default, Nov  7 2012, 16:29:59) 
[GCC 4.7.2]
$ 

Or you could use virtualenv as @jsbueno suggests. 或者你可以使用virtualenv作为@jsbueno建议。

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

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