When you run Python scripts on Linux, you may want to use a specific Python version like Python 3.6. This allows you to take advantage of particular features or make sure your scripts run properly. You can easily specify the Python version – just type a simple command! In this short guide, we’ll explain the basic steps for you to tell Linux which Python version to use when you run your awesome scripts!
Executing Specific Python Versions
The ‘python3‘ command executes a Python script with python3.x versions. If you have Python with multiple versions, specifying a version while executing a Python script is important. Here we will explore how to use the command ‘python3‘ to execute ‘/usr/local/bin/python3.6’.
Also read: Finding Where Python Is Installed (when it isn’t the default dir)
Let’s explore how to use an older version of Python, Python3.6.
Here are the steps for use of the command ‘python3‘ to execute ‘/usr/local/bin/python3.6’.
- First, check which Python version you have on your system. Here we have to execute with the python3.6 version. So it is necessary to have a python3.6 version installed on your system. Use the following command to check installed python:
ls /usr/local/bin/python3*
This command will list files in ‘/usr/local/bin‘ that start with the python3.
Here we have python3.6 installed on the system.
Setting an Alias in the Linux Terminal
- Now we will create an alias for python3. Alias is simply a short name or nickname for the ‘/usr/local/bin/python3.6‘. It is used to create a shortcut for lengthy commands. It makes it quicker and easier to execute. By creating an alias we can use ‘python3‘ to execute with the python3.6 version instead of ‘/usr/local/bin/python3.6‘. Use the following command to create an alias for python3:
alias python3='/usr/local/bin/python3.6'
This command will create a shortcut for ‘/usr/local/bin/python3.6‘ as ‘python3‘.
Here, we have created an alias to use Python 3.6 with the python3
command.
Creating a Persistent Alias with Shell Configuration
- Also, we can create an alias using a shell configuration file.
Open a shell configuration file like ‘.bashrc‘ and ‘.zshrc‘ in a text editor. You can use a nano or vi text editor. Here we will use a vi text editor. Use the following command to open a file:
vi ~/.bashrc
This command will open a ‘.bashrc‘ file.
- Press ‘i‘ for insert mode. Now add the following line in your file.
alias python3='/usr/local/bin/python3.6'
After adding the above line press ‘Esc‘ and type ‘:wq!‘ to save the file and exit the text editor.
- Now reload the shell configuration to apply changes by using the following command:
source ~/.bashrc
This command line is used to read and execute commands in the .bashrc file.
- Use the following command to check the python3 version:
python3 --version
Here we can see that the version of python3 is 3.6.8.
Also read: How to Create Python3 Aliases in Windows and Ubuntu/Linux
Conclusion
Picking which Python version to use lets you match your scripts with the right features. Choosing a certain release like Python 3.6 on Linux is easy – just make a shortcut name. This guide showed how to set up an alias so you don’t have to type long commands.
Using a specific version can be very helpful. Old code might only work properly on old Python. New code could use cool new tools from newer Python. No matter why, you can now easily choose which version runs your scripts.
With these methods, you can control Python versions for any project. Quickly switch to one that works best. It’s not hard – just remember the basic steps we showed you. Any Python user on Linux should know how to pick the version they need. L