Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 259
orschiro Avatar asked Jul 13 '12 14:07

orschiro


People also ask

Why does py work in cmd but not Python?

py is itself located in C:\Windows (which is always part of the PATH ), which is why you find it. When you installed Python, you didn't check the box to add it to your PATH , which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal.

How do I stop Python from command line?

To stop a python script, just press Ctrl + C.

Can not use Python in CMD?

The “Python is not recognized as an internal or external command” error is encountered in the command prompt of Windows. The error is caused when Python's executable file is not found in an environment variable as a result of the Python command in the Windows command prompt.


2 Answers

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.

like image 180
selllikesybok Avatar answered Sep 24 '22 00:09

selllikesybok


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 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:

  • 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

like image 35
Nuno André Avatar answered Sep 24 '22 00:09

Nuno André