Set up Python on Windows to not type "python" in cmd
How do I have to configure so that I don't have to type python script.py
but simply script.py
in CMD on Windows?
I added my python directory to %PATH% that contains python.exe but still scripts are not run correctly.
I tried it with django-admin.py Running django-admin.py startproject mysite
gives me
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Using python in front of it processes the command correctly.
What's the problem here?
C:\> assoc .py=Python
C:\> ftype Python="C:\python27\python.exe %1 %*"
Or whatever the relevant path is - you can also set command line args using ftype.
In order to make a command recognized without having to give the suffix (.py
), similar to how it works for .exe
files, add .py
to the semi-colon separated list of the (global) PATHEXT
variable.
ETA 2017-07-27
Seems like this is still getting eyeballs, wanted to elevate a useful comment for Win10 users (from @shadowrunner):
For me to get it work under Win10 the actual command was (note the placement of the quotes):
C:\> ftype Python="c:\Anaconda2\python.exe" "%1" %*
ETA 2019-02-01
Talk about evergreen!
First of all, if you're newly installing Python, I highly recommend reviewing the answer by @NunoAndré .
Secondly, to clarify something from a recent comment, please note: you must do both parts (assoc
and ftype
), or use a pre-existing association label in the ftype
command.
By default, at least for Python 3.7 under Windows 8.1, the association for .py
is Python.File
, so performing the ftype
command I wrote above will not work correctly unless the association is first changed. Or you can just use ftype
and give the default association instead. Up to you.
From Python 3.3 a launcher for Windows is included: py
(and pyw
for GUI or non-UI applications)
which aids in locating and executing of different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and will locate and execute that version.
Unlike the PATH variable, the launcher will correctly select the most appropriate version of Python. It will prefer per-user installations over system-wide ones, and orders by language version rather than using the most recently installed version.
Python installer links Python's file extensions to open
verb by default, so you can run a python file simply by typing its name (and args if needed).
-
py
:.py
,.pyc
and.pyo
(byte-compiled), and.pyz
(zip-compressed). -
pyw
:.pyw
and.pyzw
(zip-compressed).
Caveat: be aware of the differences between python.exe and pythonw.exe
Among other advantages, Windows launcher reads 'nix shebangs, so you can specify Python version or python.exe
's command line arguments
You can check this running this script (supposing py3 as default):
#! /usr/bin/python2.7 -i
import sys
print(sys.version)
-
myscript.py
: runs withpy
, launchespython2.7
and enters in interactive mode after finished (-i
, great option for testing and debugging). -
myscript.py -3
: runs withpy
, launchespython3
and keeps interactive mode. -
python myscript.py
: runs with default python runtime, no interactive mode.
You can change this default association with ftype
, but I would strongly recommend:
- Nirsoft's File Types Manager utility.
- Taking a look at msdn docs about file extensions and associations: Implementing a Custom File Format.
You can easily associate other verbs (like edit
, test
, debug
...) to these files.
In addition, you can omit Python's extensions to run a file in a terminal by adding them to PATHEXT
environment variable ordered by preference. (You must re-open the terminal for the change to take effect).
setx PATHEXT %PATHEXT%;.PYC;.PYZ;.PY
I also had the same issue... I could fix it by re-associating *.py files with the python launcher.
- Right click on a *.py file and open its properties.
- Click on the Change button of the "Opens with..." section
- Select More apps -> Look for another app on this PC.
- Then browse to your windows folder (by default: "C:\Windows")
- Select "py.exe"
i did a try with :
C:\> assoc .py=Python
C:\> ftype Python="C:\tools\python\python.exe %1 %*"
It didnt work for me. so i did a ftype search :
ftype | find "Python"
Python="c:\tools\python\python.exe" %1 %*
Python.ArchiveFile="C:\Windows\py.exe" "%L" %*
Python.NoConArchiveFile="C:\Windows\pyw.exe" "%L" %*
Solved my problem with a slightly other ftype command :
ftype Python="c:\tools\python\python.exe" "%L" %*