how to access python from command line using py instead of python
Question:
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?
Answers:
At a command line type:
doskey py=python
This would create a Windows alias so using py would be the same as python.
py
command comes with Python3.x and allow 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
.
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!
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?
At a command line type:
doskey py=python
This would create a Windows alias so using py would be the same as python.
py
command comes with Python3.x and allow 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
.
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!