How to Create Python3 Aliases in Windows and Ubuntu/Linux

Create An Alias For Python3

Alias is a custom shorthand or nickname assigned to a longer command or sequence of commands. This allows users to create shortcuts for commonly used commands.

Creating a Python3 alias involves simple steps in both Windows and Ubuntu/Linux. In Windows, it requires a batch file named ‘python.bat’ with specific commands, whereas in Ubuntu/Linux, you add ‘alias python=python3’ to your shell configuration file, like ~/.bashrc, and reload it.

Setting Up Python3 Aliases on Windows and Ubuntu/Linux

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

Also read: How to Clear the PyCharm Run Window in Python?

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.

Also read: Installing the Latest Version of Python on Ubuntu VPS

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.


Summary

Alias allows users to create shorter and more memorable names for frequently used commands. It also makes it more efficient for users. Here we create an alias for python3 as python but you can use another shorter name. For example, py3. We create an alias on Ubuntu/Linux and windows with different methods. Creating an alias on Ubuntu/Linux is more straightforward than Windows.

Now that you’ve mastered creating aliases in both Windows and Ubuntu/Linux, think about other commands you frequently use. How can creating aliases streamline your workflow? What other shortcuts could you implement?


References: