pywin is a lightweight python 2.5-3.3 launcher and switcher for Windows command line and MSYS/MINGW32. It's similar to the py.exe launcher/switcher available in python 3.3, but written with basic Windows batch scripts and a shell script for MSYS/MINGW32 support. I use bash and command line shell tools from msysgit, based on MSYS/MINGW32, to do most of my python development on Windows.
pywin supports several useful features defined in PEP 397, such as command line conventions and hash bang #! python version headers in source files. While pywin lacks some of py.exe's features, it has the basics and a few extras of its own.
- Windows >= XP for command prompt support
- Windows >= Vista for MSYS/MINGW support (requires
mklinkfor symbolic links)
- At least one installation of python 2.5 up to 3.3 (though it's not useful without at least two)
- easy_install, pip, or git
- Make sure the main python installation you want pywin to live under is in the PATH.
- With multiple python installations, it's recommended to have only the
main and Scripts directories from one installation in your PATH. For
pip install pywin
git clone git://github.com/davidmarble/pywin.git cd pywin python setup.py install
# Assuming pywin was installed with Python27 C:\>pyassoc HKEY_CURRENT_USER\Software\Classes python keys created successfully. .py files will launch with "C:\Python27\pywin.bat" C:\>pywin genlaunchers Generating launchers... C:\Python27\python2.7.bat -> C:\Python27\python.exe C:\Python27\python2.6.bat -> C:\Python26\python.exe C:\Python27\python2.5.bat -> C:\Python25\python.exe C:\Python27\python3.0.bat -> C:\Python30\python.exe C:\Python27\python3.1.bat -> C:\Python31\python.exe C:\Python27\python3.2.bat -> C:\Python32\python.exe C:\Python27\python3.3.bat -> C:\Python33\python.exe C:\>pywin # launch first python found in PATH C:\>pywin -2.7 # launch python 2.7 C:\>pywin setdefault 3.3 Setting default python for active session to: 3.3 C:\Python33;C:\Python33\Scripts -- now at front of PATH # Create a test file with a specified python version in the header C:\>echo #! /usr/bin/python2.7 > test.py C:\>echo import sys;print("\n %s"%sys.exec_prefix) >> test.py C:\>test.py C:\Python27 C:\>pywin -3.2 test.py # launch test.py with python 3.2 C:\Python32
pywin and its associated scripts are installed in the main directory
of the active python version (e.g. C:27). There's no need to install it
under more than one version of python (if you do, you'll have to run
pywin genlaunchers for each version you install it under). Make sure
that this main python directory is always on the path. Alternatively you
can move the included scripts to another directory in your path.
Auto-generate version-specific launchers
pywin can create individual launch scripts to directly access core python installations (e.g. python2.7). These can be called directly with or without arguments from the Windows command prompt and MSYS/MINGW bash prompt. To use this feature, call
Launchers are created for all machine-wide and user-specific python
installations found in the Windows registry. Windows batch files will be
added to the directory where pywin is located. MSYS/MINGW32 relies
on Windows links created programmatically with
mklink, which is why
you must have Windows >= Vista installed to make use of this project.
Launch a specific python version using pywin
pywin -2.7 # launch python 2.7 pywin -3.2 test.py # launch test.py with python 3.2
Automatically invoke the right python with a script header
Add a directive to the first or second line of a source file to have the
correct interpreter called. Currently this only supports python
launchers created by the
pywin genlaunchers command. To use this
feature, you must associate the .py extension with pywin.bat using the
pywin commands work from both Windows command line and
Search for python installations and create batch files in the same directory where pywin is installed.
Note if you're using MSYS/MINGW32 this must be run again in the MSYS/MINGW32 shell, and you must have Windows >= Vista.
pywin setdefault <version>
Set the default python to
<major>.<minor> version. Adds the right
python home directory and its associated Scripts directory to the front
of PATH. Removes any other existing reference to a python Scripts
directory in PATH.
# Existing PATH: C:\Windows\System32;C:\Windows;C:\Python27;C:\Python27\Scripts pywin setdefault 3.3 # New PATH: C:\Python33;C:\Python33\Scripts;C:\Windows\System32;C:\Windows;C:\Python27; # C:\Python27 is preserved so that pywin can be found.
pywin setdefault is only active for the current cmd.exe (or
MSYS/MINGW32 shell) session. If you want to change the
default python permanently, you need to change your system or user
PATH and make sure pywin is installed for that python version.
When calling this from MSYS/MINGW32, enter a dot first so the changes to
$PATH propagate to your active shell. E.g.
. pywin setdefault 3.3
launch with version and/or source
pywin [-<version>] [<source file>]
Launch either a specific
<major>.<minor> version of python, or a
source file, or both. Note that specifying a version of python on the
command line will override any version in the header of the source file.
# launch python 2.7 pywin -2.7 # launch test.py with python 3.2 pywin -3.2 test.py # launch test.py according to Version Search Order below pywin test.py
Version Search Order
pywin will launch the first version of python found among:
Any version specified after a #! in the first 2 lines of the source. The interpreter will be invoked with any additional parameters.
#! python3.3 #! /usr/bin/python2.7 -v
If the environment variable
VIRTUAL_ENVis set, use that virtualenv's
If the environment variable
PYTHONHOMEis set, use its
If none of the above, fall back to the first
python.exefound on the path.
pyassoc [original] [all_users]
With no arguments,
pyassoc will register the .py extension to run
%pyhome%\pywin.bat. This change is made in the registry to
HKEY_CURRENT_USER, so that when .py files are invoked, any
machine-wide setting is overridden.
Note that registry settings have no effect on launch .py files under MSYS/MINGW32.
- original - restore .py registry settings to launch with
- all_users - if administrator, apply changes to
HKEY_LOCAL_MACHINEand remove any
HKEY_CURRENT_USERpython keys. Note that users can override this with their own
%pyhome% used by
pyassoc is set in this manner:
- If the environment variable
PYTHONHOMEis set, use it.
- If the environment variable
DEFAULTPYTHONis set, use it. This is set when you call
pywin setdefault <version>, but only lasts for the current session.
- The path the
pyassoc.batscript is in.