8

I have a very weird request. An executable I have has a system call to a python script which goes like py file1.py Now, in my system though py is shown as an unrecognized internal or external command. python file1.py works however.

is there some way I can get my windows command prompt to recognize that py and python refer to the same thing?

3

3 Answers 3

21

py command comes with Python3.x and allows you to choose among multiple Python interpreters. For example, if you have both Python 3.4 and 2.7 installed, py -2 will start python2.7, and py -3 will start python3.4. If you just use py it will start the one that was defined as default.

So the official way would be to install Python 3.x, declare Python 2.7 as the default, and the py command will do its job.

But if you just want py to be an alias of python, doskey py=python.exe as proposed by @Nizil and @ergonaut will be much simpler... Or copying python.exe to py.exe in Python27 folder if you do not want to be bothered by the limitations of doskey.

7
  • 2
    Generally a console alias (i.e. AddConsoleAlias)will disappoint you if you're expecting it to behave like a bash alias. It's matched at the beginning of a line of input in the console host process (conhost.exe), so cmd.exe will literally read "python.exe" in place of "py". Each executable that's attached to a given console defines its own set of aliases (e.g. you can add aliases specific to python.exe). The let-down is that they're just a simple input translation; they won't work elsewhere on the command line or in a batch file.
    – Eryk Sun
    Sep 23, 2015 at 17:41
  • You inadvertently answered one of related doubts as well. On another machine when I installed Python 2.7, i could use the py command without any aliasing, which was confusing. Reading your answer I realised that Python 3 was already installed on that machine. Sep 24, 2015 at 3:09
  • @eryksun comment on the input translation is also very relevant. the alias is only for that cmd session. Since I only had the executable file, I just used Alex's solution and made a copy of python.exe, renaming it to py.exe. I just wanted to get this working for now, so this hack works. Sep 24, 2015 at 3:33
  • @TheBlueNotebook, the alias is set in the attached conhost.exe instance. cmd.exe isn't even aware that it exists. You can start a cmd subshell in the console, define an alias, exit that subshell, and the alias persists. It's gone once conhost.exe exits, but the output of the doskey.exe /macros:all option can be redirected to a file and reloaded in another console via the /macrofile=filename option. The big problem with a console alias is that it just replaces console input text at the start of a line, e.g. you can't do someapp.exe | py file1.py with a py alias.
    – Eryk Sun
    Sep 24, 2015 at 4:12
  • 2
    @holdenweb: exactly. Unix-like systems have other ways to choose the correct python version when more than one are installed. py is just the Windows way. Nov 22, 2018 at 22:46
7

At a command line type:

doskey py=python

This would create a Windows alias so using py would be the same as python.

1
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. Sep 23, 2015 at 16:46
2

I know this doesn’t answer the question since the question is CMD specific, but since Google searches for “using py instead of python” lead to this question, I will write the solution for zsh on macOS

For zsh:

(default shell built-in in macOS Catalina and above, and can also be installed in Windows and more)

Edit ~/.zshrc file:

$ nano ~/.zshrc

and add the following line:

alias py=python

To translate the command py to python, or

alias py=python3

To translate to python3

>> Reopen the terminal window after making the change!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.