austinsymbolofquality.com

Mastering Python Installation: A Comprehensive Guide

Written on

Chapter 1: Introduction to Managing Python Versions

This article stems from my earlier piece titled "Read This Before You Install Python3.12 On Linux." In that discussion, I recounted my experience of unintentionally disrupting my Ubuntu Operating System while attempting to set Python3.12 as my default version.

I'm grateful that the article resonated with many readers, resulting in valuable feedback that has enriched my understanding. This new article aims to delve deeper into the subject of installing and managing various Python versions, regardless of the operating system you use.

I also want to extend my gratitude to Tony Csoka for introducing me to Pyenv, and to Tom Harrison and H. Habighorst for their insightful contributions in the comments. Engaging with a community that shares knowledge is always rewarding.

Let's jump into the heart of the matter.

Section 1.1: Installing Python with Anaconda

I want to start with Anaconda, as it was my initial choice when considering the installation of Python3.12. Anaconda is a Python distribution primarily designed for data scientists. It includes a package management tool known as conda, which allows for package installation and virtual environment management in a manner comparable to pip:

conda install

Creating a virtual environment with conda is straightforward. You simply execute the following command:

conda create -n <env_name>

This command generates a conda environment using the system's Python version. If you wish to install a specific version, it can be done like this:

conda create -n <env_name> python=3.12

— So, why did I move away from this method?

To clarify my choice, we need to discuss the differences in how pip and conda manage virtual environments. Pip relies on PyPi to download libraries, whereas conda uses a repository called conda-forge. In my experience, conda-forge often lacks the latest versions of libraries compared to pip. The way dependencies are handled by conda also differs from pip, which is the standard in my team.

In our workflow, we have specific tasks to complete before deploying a project, and switching to a different tool like conda could lead to confusion during deployment. Therefore, I prefer to use a single tool for both development and deployment to minimize unexpected issues.

Section 1.2: Exploring Pyenv

For those familiar with Node.js, you're likely aware of Node Version Manager (nvm), which simplifies the installation and switching between Node.js versions. Likewise, Python has its own version manager called pyenv.

Pyenv enables effortless switching between Python versions on your machine. It does not install Python globally; instead, it stores versions in a dedicated folder within the pyenv installation.

To install pyenv, visit this link and choose the method that suits your operating system. In my case, using MacOS, I ran the following commands:

  • Install pyenv:

brew install pyenv

  • Set up pyenv and enable shims:

Pyenv utilizes the shim design pattern, meaning it intercepts Python commands and directs them to the selected version set within pyenv. To configure it on MacOS (assuming you use zsh), execute:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc

echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc

echo 'eval "$(pyenv init -)"' >> ~/.zshrc

After executing these commands, reload your .zshrc file:

source ~/.zprofile

  • Installing a Specific Python Version:

Now that you're set up, you can install any version of Python. To see available versions, run:

pyenv install -l

You'll be presented with a long list of versions. To install a desired version:

pyenv install 3.12.2

Now, set Python 3.12 as your global version:

pyenv global 3.12.2

Your default Python is now 3.12.2.

While there is much more to learn about pyenv, I'll save that for a dedicated article.

Chapter 2: Utilizing asdf for Multi-Language Version Management

Unlike pyenv, asdf is a versatile version management tool that supports multiple programming languages, including Node.js, Python, and Ruby. Each language is treated as a plugin, which you can manage through asdf.

Like pyenv, asdf is also based on the shim design pattern, allowing for smooth execution handling across different languages. To install it, follow the installation instructions relevant to your operating system. For MacOS, you can run:

brew install coreutils curl git

You have two methods for installation:

  • Official Download:

This method clones the repository into a folder named .asdf in your home directory:

  • Community Supported Downloads:

For my installation, I used:

brew install asdf

After installing asdf, configure it according to your shell and operating system.

Using asdf with Python:

As previously mentioned, each language in asdf is treated as a plugin. To add a plugin, use:

asdf plugin add <plugin_name>

To see the list of available plugins, execute:

asdf plugin list all

To add the Python plugin, run:

asdf plugin add python

Now, you can install any version of Python using asdf:

asdf install python 3.12.2

And set a preferred version as follows:

asdf global python 3.12.2

Conclusion

Reflecting on my earlier experiences, I recognize that I had numerous options to avoid disrupting my computer, as discussed in my previous article. My eagerness to make Python 3.12 my default system version clouded my judgment, leading to mistakes that turned into valuable learning opportunities.

Mistakes are an integral part of life, and I embrace them as chances for growth. From this experience, I’ve learned several key lessons:

  • Avoid tampering with the system's Python, especially on Linux.
  • There are numerous ways to manage Python versions, including tools like asdf that I recently discovered.
  • I was unaware of the shim design pattern.

While there are other methods to install and manage Python versions, I currently prefer pyenv and am exploring asdf for other programming languages like Node.js.

Thank you for taking the time to read my article! If you found it valuable and want to receive similar content directly in your inbox, join the In Plain English community! 🚀

Before you go, please consider following me and engaging with my work.

Follow us: X | LinkedIn | YouTube | Discord | Newsletter

Explore more at: Stackademic | CoFeed | Venture | Cubed

Find additional content at PlainEnglish.io

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Steal These 9 Effective Strategies to Gather Email Subscribers Now

Discover proven techniques for building your email list to grow your online business.

Discovering the Length of a Number in JavaScript

Learn how to determine the length of a number in JavaScript using various methods, including converting to a string and logarithmic calculations.

Romanticism and Science: Insights from 19th Century Literature

Exploring the interplay between Romanticism and science, this piece delves into how 19th-century literature reflects contemporary themes and ideas.