Explain Codes LogoExplain Codes Logo

Unable to set default python version to python3 in ubuntu

python
prompt-engineering
update-alternatives
python-versions
Anton ShumikhinbyAnton Shumikhin·Oct 4, 2024
TLDR

To swiftly switch your Ubuntu system's default Python version to Python 3, pop this single command in the terminal:

sudo ln -s /usr/bin/python3 /usr/bin/python # Who ya gonna call? Python3!

This generates a symbolic link (quick shortcut) linking python to python3, effectively prompting Python 3 to execute when you call python.

Harnessing the power of update-alternatives

Ubuntu's update-alternatives system is a prime tool crafted to maintain symbolic links dictating default commands. It's a safer and more systematic strategy than manually tinkering with system files.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 # No priority issues like in a supermarket queue!

This command heralds Python 3 as the default for the python command with a priority of 10. Remember, keeping the correct syntax "<link> <name> <path> <priority>" is crucial to avoid crashing into error walls. The priority value proves the pecking order if multiple versions fight for prominence. After setting up, execute:

sudo update-alternatives --config python

to pick your favorite from the available alternatives.

System-wide python3 declaration

On Ubuntu 20.04 LTS and later versions, python-is-python3 package serves as a compass guiding the path to set Python 3 as the default version:

sudo apt install python-is-python3 # more like "python-is-now-python3", eh?

This package erases and replaces the symlink at /usr/bin/python to directly point to the Python 3 interpreter. It's neat, quick, and aligned with Ubuntu's move to treat python as Python 3 by default.

Stepping around the pitfalls

  • Incorrect permissions: Don't forget your 'magic word' – sudo when revising system-level changes to warrant adequate permissions.
  • Overwrite mishaps: Avoid spilling ink onto crucial system files while forming links or using update-alternatives.
  • Compatibility: Conduct a system health-check for your Ubuntu version's compatibility before marching with specific command solutions.
  • Result Verification: Call python --version to validate the version alteration. Remember, the system has no room for assumptions that might unleash unforeseen behaviors.

Juggling with multiple python versions

Sometimes you need to be a juggler, handling different Python versions. In such cases, you might prefer a temporary solution:

Temporary command prompt aliasing

Forge a temporary alias in your current terminal session without heralding a system-wide change:

alias python=python3 # My python, my rules!

To persist this alias, pour it into your .bashrc file and execute source ~/.bashrc to plug in the changes instantly.

Pyenv: The Python version handler

Pyenv acts as a loyal aide for installing and handling multiple Python versions:

curl https://pyenv.run | bash # "Knock, knock. - Who's there? - pyenv!"

Pyenv lets you install multiple Python versions and switch between them like flipping channels on a TV.

Direnv and pyenv: Perfect Python pair

Direnv and pyenv orchestrate to automatically switch Python versions on a per-directory basis:

# .envrc file in your project directory use python 3.8.6 # "3.8.6? That's my stop!"

Entering this directory will put the Python version on auto-pilot, cruising through project consistency.

Ensuring safe passage and error handling

Fostering safe practices and understanding how to wrestle with errors is crucial:

  • When you hit a syntax error message, treat it as a helpful signboard, guiding you to the correct command path.
  • Resort to update-alternatives and refrain from manually tampering with system files.
  • Simultaneously, ensure you select the appropriate priority value for your preferred Python versions.