Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a specific Python version WITHIN a script.py -- Windows

What line of text should I place at the top of a script.py to invoke the specific version of Python that I need to use?

I have two versions of Python on Windows XP, 2.6.5 and 2.7.2. They each have their own special modules and were installed by separate applications. My scripts are placed on the desktop so that I can double-click and run them conveniently.

The problem is that all my scripts invoke 2.6.5, which is fine for the scripts that use the modules installed with 2.6.5, but my scripts intended for 2.7.2 don't run. They invoke the Python 2.6.5 without the modules I need to import.

I've tried typing various headers without and without the #! to invoke 2.7.2 when I need to, but either my syntax is wrong or it just isn't possible to specify under Windows. Could anyone tell me the precise syntax of the line I need to add to my script. The python.exe for 2.7.2 is stored under C:\OSGeo4W\bin

Thanks for letting me know what line to place at the top of a script.py to invoke the exact version of Python I need to use.

like image 538
user12711 Avatar asked Mar 07 '13 19:03

user12711


2 Answers

If you have installed Python 3.3 on your system it added a new launcher for Python scripts which means you can use a shebang line:

#!python2.7
print "Hello"

or

#!python3.3
print("World")

will both work and run the appropriate python, or you can specify a full path to a Python interpreter or create an ini file that defines abbreviations for specific Python interpreters.

See PEP 397 for the different options available in Windows shebang lines.

If you don't want to install Python 3.3 then you can also install the launcher separately.

like image 118
Duncan Avatar answered Oct 26 '22 01:10

Duncan


Instead of putting the script itself on the desktop, place a shortcut on the desktop. The process is described in a techrepublic.com article. Specify the appropriate interpreter as the program to run, and list one of your .py files as a parameter in the same field.

like image 32
James Waldby - jwpat7 Avatar answered Oct 26 '22 01:10

James Waldby - jwpat7