Set Up Python on Windows to Not Type "Python" in Cmd

Set up Python on Windows to not type python in cmd

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.

py' works but not 'python' in command prompt for windows 10

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. Just use py for launching consistently, and stuff will just work. Similarly, if py.exe was associated with the .py extension at installation time, a standard shebang line (details in PEP linked above) will let you run the script without even typing py.

I don't know precisely what VSCode uses to find Python (using py.exe directly, using a copy of Python that ships with the editor, performing registry lookup, a config file that just says where to find it, etc.), but that's not really relevant to running your scripts yourself.

CMD opens Windows Store when I type 'python'

Use the Windows search bar to find "Manage app execution aliases". There should be two aliases for Python. Unselect them, and this will allow the usual Python aliases "python" and "python3". See the image below.

Sample Image

I think we have this problem when installing Python because in a new Windows installation the aliases are in the ON position as in image below. When turned on, Windows puts an empty or fake file named python.exe and python3.exe in the directory named %USERPROFILE%\AppData\Local\Microsoft\WindowsApps. This is the alias.

Sample Image

Then Microsoft put that directory at the top of the list in the "Path" environment variables.

Sample Image

When you enter "python" in cmd, it searches the directories listed in your "Path" environment variables page from top to bottom. So if you installed Python after a new Windows 10 install then get redirected to the Windows Store, it's because there are two python.exe's: The alias in the App Execution Alias page, and the real one wherever you installed Python. But cmd finds the App execution, alias python.exe, first because that directory is at the top of the Path.

I think the easiest solution is to just check the python.exe and python3.exe to OFF as I suggested before, which deletes the fake EXE file files. Based on this Microsoft Devblog, they stated they created this system partially for new Python users, specifically kids learning Python in school that had trouble installing it.

Creating this alias was to help kids just starting Python to install it and focus on learning to code. I think Windows probably deletes those aliases if you install Python from the Windows App Store. We are noticing that they do not get deleted if you manually install from another source.

(Also, the empty/fake python.exe is not really empty. It says 0 KB in the screenshot, but entering "start ms-windows-store:" in cmd opens the Windows App Store, so it probably just has a line with that and a way to direct it to the Python page.)

Finally, as Chipjust suggested, you can create a new alias for Python using something like DOSKEY as explained in this article for example:
How to set aliases for the command prompt in Windows

Python command not working in command prompt

It finally worked!!!

I needed to do things to get it to work

  1. Add C:\Python27\ to the end of the PATH system variable
  2. Add C:\Python27\ to the end of the PYTHONPATH system variable

I had to add these to both for it to work.

If I added any subdirectories, it did not work for some reason.

Thank you all for your responses.

How do I test if Python is installed on Windows (10), and run an exe to install it if its not installed?

All of the comments guided me to the right way to do it.

I used this great working code:

:: Check for Python Installation
python --version 3>NUL
if errorlevel 1 goto errorNoPython

:: Reaching here means Python is installed.
:: Execute stuff...

:: Once done, exit the batch file -- skips executing the errorNoPython section
goto:eof

:errorNoPython
echo.
echo Error^: Python not installed
"C:\Program Files\used\systems\innoventiq\accumanager\required\excutables\python-3.7.3-amd64.exe"

Typing python in CMD opens up the Microsoft store download for Python 3.8

One can use a old version of python, you can use a 2.x version also which is much older. It sounds like python has been removed from your PATH or got uninstalled.

Try add your python installation to the PATH and it should work again:
Set python path

Update:
Windows have added app execution aliases for the python keyword, see below answer for more details and how to solve it
https://stackoverflow.com/a/58773979/5719145



Related Topics



Leave a reply



Submit