Skip to content

Instantly share code, notes, and snippets.

@zvodd
Last active December 3, 2018 11:49
Show Gist options
  • Save zvodd/e5d1cca9771584310e5c to your computer and use it in GitHub Desktop.
Save zvodd/e5d1cca9771584310e5c to your computer and use it in GitHub Desktop.
A batch script to setup python file association and all the required junk for using python on the command line in windows. Useful if you have a few different python installs e.g python 2 & python 3 and maybe a python 2 install in Xampp or Ampps. Simply put the batch file in C:\windows or any folder on you $PATH environment variable and modify th…
@echo off
REM "ftype /?" explains all of this assoc and ftype and PATHEXT usage
REM https://docs.python.org/2/using/windows.html for more info around the subject.
REM set PythonDIR to your python 2 or 3 install path; e.g. the folder with python.exe in it.
set PythonDIR=C:\Python27
set PATH=%PythonDIR%;%PythonDIR%\Scripts;%PATH%
set PYTHONPATH=%PythonDIR%\Lib;%PythonDIR%\Lib\site-packages;%PythonDIR%\DLLs;
set PATHEXT=%PATHEXT%;.PY;.PYW
assoc .py=Python.File>NUL
assoc .pyw=PythonW.File>NUL
ftype Python.File="%PythonDIR%\python.exe" %%1 %%*>NUL
ftype PythonW.File="%PythonDIR%\pythonw.exe" %%1 %%*>NUL
@zvodd
Copy link
Author

zvodd commented Feb 26, 2015

I saved this to 3 separate files envpython.bat, envpython3.bat and envpythonweb.bat.
Changing the "set PythonDIR=" line to the related python install.

Usage:
Running "envpython" on the command line will setup your environment in that command line.

Note:
The "assoc" and "ftype" lines affect the Registry and apply globally, e.g. outside the current command line context.
You can remove the "assoc" and "ftype" lines if your happy typing "python PythonFileToLaunch.py"; As you may want the default open action in windows to open py files in a text editor or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment