Learn how to create a Python3 alias and use Python as Python3 in your terminal. This tutorial will show you how to check your Python version and set up an alias
So now we are creating an alias (nickname) ‘Python’ for ‘Python3’
On Windows, you can achieve an alias by creating a batch file that acts as an alias. Here are the steps:
@echo off
python %*
‘@echo off‘ this line turns off the echoing of each command to the console, making the script cleaner and ‘python %*’ calls the python3 executable with any additional arguments passed to the batch file. The ‘%*‘ in the batch file passes any arguments provided to python.bat to the python command.
python hello.py
Here you can replace hello.py with the actual python file you want to run.
Let’s now edit the bashrc to add an alias for python3 letting us call it using python.
To create an alias for Python3 we have to follow some steps:
nano ~/.bashrc
‘nano’ is a command-line text editor in Unix-like operating systems. This user-friendly text editor provides basic text editing capabilities through a command-line interface. ‘~/.bashrc’ is the file being opened for editing. ‘~’ is a shorthand representation of the user’s home directory. So ‘~/.bashrc’ refers to a file located in the user’s home directory.
Then press enter and it will open a text editor.
In the nano text editor add the ‘alias python=python3’ at the end of the text as shown in the above figure. Then save (ctrl+o and enter) the file and exit (ctrl+x) the text editor.
source ~/.bashrc
‘source’ is used to execute the commands in the specified file. ‘source ~/.bashrc’ reads and executes the commands in the .bashrc file.
python --version
Here you can see Python refers to Python3.
This second method is similar to method 1, in this method, we are using a ‘vi’ text editor. Let’s see how we are creating an alias using a vi text editor.
vi ~/.bashrc
This command opens the vi editor with a .bashrc file. You can now navigate to the end of a file by using arrow keys.
alias python=python3
Then press ‘Esc‘ to exit insert mode. To save and exit type ‘:wq‘ and press enter. This writes changes and quits the vi editor.
source ~/.bashrc
python
Here by running a Python command. It displays the version of Python3.