30

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
orschiro
  • 19,847
  • 19
  • 64
  • 95
  • 2
    possible duplicate of http://stackoverflow.com/questions/4235834/how-to-make-python-scripts-executable-on-windows-7-after-installing-python-3-1-2 – Gryphius Jul 13 '12 at 14:49

4 Answers4

42
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.

selllikesybok
  • 1,250
  • 11
  • 17
  • 1
    Thank you so much! I never knew about `assoc` or `ftype` before today. – Noctis Skytower Jul 13 '12 at 15:04
  • I set that in CMD with administrative rights but still I cannot execute `django-admin.py --version` without python as a prefix. Without the prefix I get shown the help command is if I had run `django-admin.py -h`. – orschiro Jul 13 '12 at 15:09
  • 1
    Neither did I. :) But I needed one more upvote for comment privs, so to the Windows docs I went. – selllikesybok Jul 13 '12 at 15:10
  • @orschiro - sorry, I think my last edit shows the right one. Need to at %0 (replaced by file) and %* (replaced by args). – selllikesybok Jul 13 '12 at 15:13
  • 1
    That was it! Thanks. I discovered the same just here: http://docs.python.org/faq/windows.html#how-do-i-make-python-scripts-executable – orschiro Jul 13 '12 at 15:16
  • 2
    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" %*``` – shadowrunner Feb 19 '16 at 22:10
  • 1
    1) I can execute directly a .py file from the cmd line (w/o the explicit use of python.exe), even if .py is not among the associations. (I don't know how this is done.) The only problem is that I cannot use arguments! 2) The solution given above -- which I tried -- didn't change anything. 3) What do you mean by "you can also set command line args using ftype"? How? Example? (I really wonder, since arguments are dynamic, whereas 'ftype' is a static thing. – Apostolos May 01 '18 at 16:21
  • @Apostolos - Still on Win8.1 here, so don't know if there's anything different happening on Win10. I've also since migrated to Py3. The new Windows installers actually handle a lot common cases, so you may want to try re-installing and paying attention to custom options/settings there? What I meant by setting command line options with ftype, is you can put anything you want in that string. So, you can add switches (-i, etc) and other parameters to pass by default. – selllikesybok May 01 '18 at 21:03
  • I still need an example about arguments. E.g. if a PY file requests a filename to be passed as an argument, how are you going to form `ftype Python="c:\Anaconda2\python.exe" "%1" %*` so that one can run `PYfile.py filename` from MSDOS prompt? In my Windows 7 and with Python 2.7, 'filename' is just ignored. See what I mean? – Apostolos May 03 '18 at 08:02
  • I tried to do this, but no luck.Despite what the manual says, mine didn't do this on Win 10. Maybe something else screwed it up - I had PyCharm for a while, which "grabbed" the association, but even uninstalling it and reinstalling Python didn't fix this issue. After running the commands above `ftype Python` returns `python="C:\Program Files\Python36\python.exe" "%1" %* ` but it doesn't work anyway. No error message, just nothing happens when I run `foo.py`. Running `python foo.py` works fine. – jgalak Jan 06 '19 at 16:06
  • @jgalak - sorry to hear! I still do not have Windows 10, but, is it possible that .py is not associated to Python? A default install of Python appears to create the association of .py=Python.File (view this via `assoc .py` from CMD). So you would need to change the association or set the Ftype Python.File = instead of Python = . – selllikesybok Feb 01 '19 at 19:45
  • 1
    @selllikesybok - `assoc .py` returns `.py=Python`. `ftype Python` returns `Python=C:\Program Files\Python36\python.exe" "%1" %*`. Still doesn't work though. – jgalak Feb 03 '19 at 16:23
  • 1
    Aha! I ran the utility suggested by @Nuno André below. That didn't fix it, but it seems to have reset the association, so the next time I tried to run a `.py` file, I got the Windows screen asking what program to open the file with. I was able to then set it to the python executable, and it's working now. Weird... – jgalak Feb 03 '19 at 16:36
5

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).

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 with py, launches python2.7 and enters in interactive mode after finished (-i, great option for testing and debugging).
  • myscript.py -3: runs with py, launches python3 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:

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

Nuno André
  • 4,739
  • 1
  • 33
  • 46
  • I have a problem on Windows 10 where running myscript.py in a terminal causes VS Code to open!. How do I reset this? – banjaxed Jan 19 '21 at 11:42
3

I also had the same issue... I could fix it by re-associating *.py files with the python launcher.

  1. Right click on a *.py file and open its properties.
  2. Click on the Change button of the "Opens with..." section
  3. Select More apps -> Look for another app on this PC.
  4. Then browse to your windows folder (by default: "C:\Windows")
  5. Select "py.exe"
Camille G.
  • 3,058
  • 1
  • 25
  • 41
  • 2
    This approach has additional advantages compared to the accepted answer: It will take the shebang line of the Python scripts into account and therefore select the correct Python interpreter in case you have e.g. virtual environments in use. – phispi Jun 18 '21 at 07:42
1

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" %*
Sloomy
  • 151
  • 1
  • 6