Setting up Windows Shebang using Anaconda

Download and install Py Launcher (it will install py.exe to c:windows)

https://bitbucket.org/vinay.sajip/pylauncher/downloads/

^direct url

Download and install Anaconda (I install in c:PYTHON instead of default c:anaconda3)
Create two environments (using command prompt)
$ conda create name py27 python=2.7.11      # creates python 2 environment
$ conda create name py35 python=3.5.2       # creates python 3 environment
$ conda info envs      # lists currently installed environments
$ activate py27      # test python 2 environment
$ activate py35      # test python 3 environment
$ deactivate
Setting up Shebang for Python 3 and Python 2 environment:
(in command prompt, type the lines starting with $)

$  mkdir c:@CODE
$  cd @CODE
$  notepad what_version_py27.py
$  REM click yes on popup and paste text below in black into file and save

#!C:PYTHONenvspy27python.exe
import sys
version = sys.version.split()[0]
print version  #  python 2 print

$  notepad what_version_py35.py
REM click yes on popup and paste text below in black into file and save
#!C:PYTHONenvspy35python.exe
import sys
version = sys.version.split()[0]
print(version)  #  python 3 print
Test Shebang:
$  py c:@CODEwhat_version_p27.py
2.7.11
$  py c:@CODEwhat_version_py35.py
3.5.2
Note:
Can create py.ini file in c:windows to create aliases instead of writing entire path to python environment
; c:windowspy.ini
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don‘t edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that‘s shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; “c:Program FilesMyCustom.exe” c myscript.py
;
py27=“C:PYTHONenvspy27python.exe”
py35=“C:PYTHONenvspy35python.exe”

    Leave a Reply

    Your email address will not be published. Required fields are marked *