How to Use "Py" Instead of "Python" at the Command Line in Linux

how to access python from command line using py instead of python

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.

Is it possible to use py instead of python at the command line in Linux?

Sure. You can use the bash alias command to do something like this:

alias py=python3

Put it somewhere like .bashrc and IIRC it will be set up for each (interactive) session.

Cannot run py as command

You could always add alias py='python3' to your .bashrc file.

How can I make the python command in terminal, run python3 instead of python2?

If you're using Windows then you can use the Python Launcher For Windows.

This will allow you to use the py command to select different python installations such as:

py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)

Similarly you can set a shebang in your python files as demonstrated below:

#! python3
print('Hello World!')

If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.

What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.

How to open python in cmd with the 'python' command in stead of py -3

As, others have said -- you could just add the python directory containing the executable to the PATH... I just want to give this as an extra option.

This is a general solution for any type of program or executable you'd like to run with a custom name.

I created an "Aliases" folder in my account C:\Users\{Your Account Name}\Aliases.

In that folder, I keep useful .bat scripts.

Just make a script called "python" and, in that script, put the path of your python.exe.

So, for example, this would be your entire bat file (named python.bat):

@echo off
echo .
C:\Python36\python.exe

if you want to be able to add args to your command, do the same but replace the execution line with the follows:

C:\Python36\python.exe %1 %2 %3 %4 %5 %6

That will give you the ability to add up to 6 arguments. This is helpful if you're doing python pip commands directly from the command line.

After you create this file, simply add the folder to your PATH variable
(Start -> Type Edit the Environment Variables for your account -> Click PATH -> click Edit -> Append the location of your "Aliases" folder to the PATH, using a semi-colon to separate it from the rest. -> Click OK and OK once again.) From now on, you don't have to worry about the PATH variable, just add more bat files and you're good to go!

How to run python script in command line

to correct it:

os.system(path + "/update.py")

so basically it will be like - os.system(/home/user/code/update.py)

How to change python version in command prompt if I have 2 python version installed

Both Python2 and Python3 is installed
As you can see, I have both Python2 and Python3 installed.

Windows Path variable
I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.

How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM.
Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.

Both Python2 and Python3 executables have the same name in Windows so it never runs python3.

What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.

How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.

.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.

Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.

python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.

Python and Python3
python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.

I hope this solves your problem.

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.



Related Topics



Leave a reply



Submit