Python3 Alias: How to Use Python as Python3

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

Setting Up Python3 Aliases on Windows and Ubuntu/Linux

So now we are creating an alias (nickname) ‘Python’ for ‘Python3’

Creating a Python3 Alias in Windows

On Windows, you can achieve an alias by creating a batch file that acts as an alias. Here are the steps:

  • Open Notepad in your system. Press ‘Win + R’ to open the run dialog. Then type Notepad and press Enter. Now Notepad will open on your screen.
  • In Notepad you have to write the following batch commands:
@echo off

python %*

BatchFile

‘@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.

  • Save your batch file with the .bat extension. For example, python.bat. Click on File in Notepad select Save As and save it with the .bat extension.
  • Now test the alias. Open the command prompt and navigate to a directory containing Python script or use the cd command to change directory where the your batch file (‘python.bat‘) is located.
  • Now you can run Python using the alias. Now you can use the python command as an alias for python3. For example:
python hello.py

Here you can replace hello.py with the actual python file you want to run.

Creating a Python3 Alias in Ubuntu/Linux

Let’s now edit the bashrc to add an alias for python3 letting us call it using python.

Simple Alias Creation in Ubuntu/Linux Using Nano

To create an alias for Python3 we have to follow some steps:

  • Open a shell configuration file eg. ‘~/.bashrc’ for a Bash or ‘~/.zshrc’ for Zsh in a text editor.
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.

Alias For Python3 Nano

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.

  • Now reload the shell configuration to apply changes by using the following command:
source ~/.bashrc

‘source’ is used to execute the commands in the specified file. ‘source ~/.bashrc’ reads and executes the commands in the .bashrc file.

  • Now when you type Python it will refer to Python3. You can check by running the following command:
python --version

Alias Output

Here you can see Python refers to Python3.


Using Vi Editor for Python3 Alias in Ubuntu/Linux

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.

  • Open text editor. To open the text editor use the following command:
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.

  • Now press i to enter insert mode. Then add the following line to create an alias in the .bashrc file
alias python=python3

Alias For 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.

  • Now reload the shell configuration to apply changes by using the following command:
source ~/.bashrc
  • Now you can use Python for Python3. You can check by using the following command:
python

Vi Output

Here by running a Python command. It displays the version of Python3.

#python 

Python3 Alias: How to Use Python as Python3
3.75 GEEK