Software & AppsOperating SystemLinux

How To Make Python Point to Python3 in Ubuntu 20.04

Ubuntu 15

In this article, we will delve into the specifics of how to make Python point to Python3 in Ubuntu 20.04. This is particularly useful if you are working on projects that require Python3, but your system defaults to using Python2 when you use the python command.

Quick Answer

To make Python point to Python3 in Ubuntu 20.04, you can use either the pyenv tool or the python-is-python3 package. With pyenv, you can manage multiple Python versions and set your desired version as the default. The python-is-python3 package allows you to globally set python to python3 in Ubuntu 20.04.

Understanding Python Versions

Before we dive into the process, it’s important to understand that Python2 and Python3 are different versions of the Python programming language. Python2 is an older version that is no longer actively maintained, while Python3 is the current version with ongoing support and updates.

In Ubuntu 20.04, Python3 comes pre-installed, but when you run the python command in the terminal, it may not be pointing to Python3. This is because some older scripts and applications still rely on Python2, so the python command is often linked to Python2 for compatibility reasons.

Checking Your Python Version

First, let’s check the current location of your python and python3 executables. Open your terminal and type the following commands:

which python
which python3

The which command in Linux is used to identify the location of executables. The output of these commands will give you the paths to your python and python3 executables.

Changing Python to Point to Python3

If your python command is not pointing to Python3, you can change it using one of the following methods:

Using pyenv

pyenv is a simple, powerful and cross-platform tool for managing multiple Python versions. If you have pyenv installed, you can change the default Python version as follows:

  1. List the available Python versions: Run the pyenv versions command to see a list of all Python versions installed on your system.
pyenv versions
  1. Change the default Python version: Use the pyenv global command followed by the version number to set your desired Python version as the default.
pyenv global 3.8.5

In this example, 3.8.5 is the version number. Replace it with the version you want to set as default.

Using the python-is-python3 package

Ubuntu 20.04 provides a convenient package called python-is-python3 that you can use to set python to point to python3 globally.

  1. Install the package: Run the following command to install the package:
sudo apt install python-is-python3

The sudo command is used to execute commands with root privileges. apt is the package handling utility in Ubuntu. install is the command to install a new package, and python-is-python3 is the name of the package.

Verifying the Changes

After making the changes, you can verify that python now points to python3 by running the which python command again. The output should now show the path to the python3 executable.

Conclusion

In this article, we have covered two methods to make Python point to Python3 in Ubuntu 20.04. The method you choose depends on your specific setup and requirements. If you are using pyenv, it provides more flexibility in managing different Python versions for different projects. On the other hand, the python-is-python3 package is a straightforward option for Ubuntu 20.04 users who want to globally set python to python3.

Remember, it’s always a good practice to keep your Python version up to date and use the version that is most suitable for your projects. Happy coding!

Why is it important to make Python point to Python3?

It is important to make Python point to Python3 because Python2 is no longer actively maintained and Python3 is the current version with ongoing support and updates. Using Python3 ensures that you have access to the latest features, bug fixes, and security patches.

How can I check my current Python version?

You can check your current Python version by opening your terminal and running the command python --version. This will display the version number of the Python interpreter currently being used.

What is the purpose of the `which` command?

The which command is used in Linux to identify the location of executables. In the context of this article, we use it to determine the paths to the python and python3 executables on your system.

Can I have both Python2 and Python3 installed on my system?

Yes, you can have both Python2 and Python3 installed on your system. Many systems, including Ubuntu 20.04, come with Python3 pre-installed. However, some older scripts and applications may still rely on Python2. It is important to ensure that your projects are using the correct Python version.

What is `pyenv` and why is it useful?

pyenv is a tool for managing multiple Python versions. It allows you to easily switch between different Python versions for different projects. This can be particularly useful when working on projects that require specific Python versions or when testing compatibility with different Python versions.

How do I install `pyenv`?

You can install pyenv by following the instructions provided in the official pyenv GitHub repository. The installation process usually involves cloning the repository, running a setup script, and adding the necessary configurations to your shell profile.

Can I use `pyenv` to install Python3 if it is not already installed?

Yes, pyenv allows you to easily install different Python versions, including Python3, if they are not already installed on your system. You can use the pyenv install command followed by the version number to install the desired Python version.

Are there any risks or considerations when changing the default Python version?

When changing the default Python version, it is important to consider the compatibility of your existing scripts and applications. Some older scripts may not be compatible with Python3, so it is advisable to test your projects thoroughly after making the change. Additionally, ensure that any dependencies or libraries used by your projects are compatible with the new Python version.

Is it necessary to use `sudo` when installing the `python-is-python3` package?

Yes, using sudo is necessary when installing the python-is-python3 package because it requires root privileges to make system-wide changes. sudo allows you to execute commands with administrative privileges, ensuring that the package is installed correctly.

Can I undo the changes and revert back to using Python2 as the default?

Yes, you can undo the changes and revert back to using Python2 as the default by following the same steps mentioned in this article, but setting the desired Python2 version instead. Alternatively, you can remove the python-is-python3 package or modify your pyenv configuration to switch back to Python2.

Leave a Comment

Your email address will not be published. Required fields are marked *