Skip to content Skip to sidebar Skip to footer

Shebang Line For Python 2.7

I have installed Python2.7 in my Linux Centos which comes with a default Python2.6 installation, Default Python [root@linuxhost PythonProjects]# python -V Python 2.6.6 Default Pyt

Solution 1:

Shebang will be:

#!/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).

If you really want to:

python2.7 -m compileall .

This command will compile .py files in the current directory to .pyc:

Solution 2:

I don think a sheebang line alone would do it.

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.

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.

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).

Solution 3:

You could change the shebang to #!/usr/bin/env python2.7.

Or you could use environment modules and let the shebang be #!/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.

On this box I have python 2.6 and 2.7 installed. Depending on the module that is loaded, the selected version of python is run. 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.

Post a Comment for "Shebang Line For Python 2.7"