avatarYanick Andrade

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4947

Abstract

etween Node Js versions easily.</p><p id="e2c6">It happens that Python also has its own version manager — pyenv.</p><p id="9e37">Pyenv allows us to switch between Python versions easily on our machine. It does not install Python globally on your computer but inside a folder — <i>versions — </i>located inside the <i>pyenv </i>installation on your machine.</p><figure id="c8c9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*3w0W_SDdxwyr6JhzThDt0Q.png"><figcaption>Screenshot by the Author</figcaption></figure><p id="3c6a">To install pyenv go to this <a href="https://github.com/pyenv/pyenv?tab=readme-ov-file#installation">link</a> and choose the preferable method according to your OS. In my case is MacOS, so the commands I used are the following:</p><p id="c417"><b>— Install pyenv</b></p><div id="89b0"><pre>brew install pyenv</pre></div><p id="8c1c"><b>— Setup pyenv and enable shims</b></p><p id="50a7">Pyenv uses the <a href="https://en.wikipedia.org/wiki/Shim_(computing)">shim</a> design pattern under the hood. What this means is that when we execute any Python command on our computer, it intercepts it and directs us to the selected Python version we set with it.</p><p id="0a4b">To set pyenv and enable shim on MacOS (if your OS is <a href="https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH"><i>zsh</i></a> and not <i>bash</i>), run the following commands:</p><div id="44e9"><pre><span class="hljs-built_in">echo</span> <span class="hljs-string">'export PYENV_ROOT="HOME/.pyenv"'</span> &gt;&gt; ~/.zshrc <span class="hljs-built_in">echo</span> <span class="hljs-string">'[[ -d PYENV_ROOT/bin ]] && export PATH="PYENV_ROOT/bin:PATH"'</span> >> ~/.zshrc <span class="hljs-built_in">echo</span> <span class="hljs-string">'eval "$(pyenv init -)"'</span> >> ~/.zshrc</pre></div><p id="87ac">After running all these commands, reload the <i>.zshrc</i> file.</p><div id="1f76"><pre>. ~/.zprofile</pre></div><p id="d5c6"><b>— Installing a specific Python version</b></p><p id="fc85">Now that you’re all set, you can go ahead and install any available Python version you want. To see all versions available run the following command:</p><div id="b258"><pre>pyenv install -l</pre></div><p id="efa3">You should see a long list of available versions like this one:</p><figure id="d27e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*qlss3okRCWHJgdCFrCo9og.png"><figcaption>Screenshot by the Author</figcaption></figure><p id="6c05">We can install the desired version as follows:</p><div id="1d2b"><pre>pyenv install <version> <span class="hljs-comment"># 3.12.2</span></pre></div><p id="8173">This will install Python 3.12.2 inside our versions folder as I explained earlier:</p><figure id="fb0a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nCPtV4OTa579j5ED-GPZAg.png"><figcaption>Screenshot by the Author</figcaption></figure><p id="37a9">Now we can set Python 3.12 as default using pyenv:</p><div id="b61b"><pre>pyenv global 3.12.2</pre></div><p id="7df4">Now your default Python is 3.12.2:</p><figure id="ee56"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pOlMvmRIT98afhYI8yflLA.png"><figcaption>Screenshot by the Author</figcaption></figure><p id="72ce">There are lots of things I would like to cover about <i>pyenv</i>, but this has to be in another article entirely dedicated to it.</p><p id="c96a">Things like managing a virtual environment with <i>pyenv</i>, using <i>pip</i>, using <i>local</i> <i>pyenv</i> instead of <i>global</i> (like we did here), and so on.</p><h2 id="e3f1">Using asdf</h2><p id="14d1">Unlike pyenv, <a href="https://asdf-vm.com/"><i>asdf</i></a> is a multi-language tool for version management. It can be used with Node Js, Python, Ruby, etc.</p><p id="c355">Each programming language is considered to be a <a href="https://asdf-vm.com/manage/plugins.html">plugin</a> that you can add to <i>asdf</i> and manage its version.</p><p id="3934">Like <i>pyenv</i>, it is based on <a href="https://en.wikipedia.org/wiki/Shim_(computing)">shims</a> design pattern. Each plugin (programming language) will have its <a href="https://en.wikipedia.org/wiki/Shim_(computing)">shim</a> to ‘intercept’ and handle executions.</p><p id="8b0b">To install it, go ahead and choose the proper installation instructions according to your OS. If you’re using MacOS, you can download and install it by running the following commands:</p><div id="109f"><pre><span class="hljs-comment"># first install dependencies if not installed already</span> brew install coreutils curl git</pre></div><p id="54da">You have two installation options.</p><p id="4e53"><b>— Official download</b></p><p id="fc6d">This is the recommended method of installation. It clones the repository inside a folder named <i>.asdf</i> in your root directory:</p><div id="ba3a"><pre>git <span class="hljs-built_in">clone</span> https://github.com/asdf-

Options

vm/asdf.git ~/.asdf --branch v0.14.0</pre></div><p id="d1b3"><b>— Community Supported Downloads</b></p><p id="db46">In my case, I installed it using this command.</p><div id="b864"><pre>brew install asdf</pre></div><p id="f815">If you downloaded <i>asdf</i> using Git, finish it by running the following command you find the <a href="https://asdf-vm.com/guide/getting-started.html#_3-install-asdf">website</a> according to your Shell and OS.</p><p id="24e5"><b>— Using asdf with <a href="https://github.com/asdf-community/asdf-python">Python</a></b></p><p id="1cee">As I said earlier, each programming language and other tools in <i>asdf</i> is a plugin. A plugin can be added as follows:</p><div id="3db4"><pre>asdf plugin <span class="hljs-keyword">add</span> <plugin_name></pre></div><p id="4fcd">If you wish to see the available plugins you can install and use, run the the following command:</p><div id="c1fa"><pre>asdf plugin list <span class="hljs-attribute">all</span></pre></div><p id="617e">This will list all available plugins:</p><figure id="2401"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yM6zvKb9RzAS1jB6Xxn3EA.png"><figcaption>Screenshot by the Author</figcaption></figure><p id="39ea">Adding a Python <a href="https://github.com/asdf-community/asdf-python">plugin</a> can be easily done by running the following command:</p><div id="469c"><pre>asdf plugin add python</pre></div><p id="4f64">After adding a new plugin, you can now install any version of that plugin that you want. Installing a version using <i>asdf</i> is similar to the way we install using <i>pyenv</i>:</p><div id="5f21"><pre>asdf install python 3.12.2 <span class="hljs-comment"># install version 3.12.2 for Python plugin</span></pre></div><p id="18b5">Setting a preferable version using <i>asdf</i> is also very similar to the way we do using pyenv:</p><div id="2517"><pre>asdf <span class="hljs-keyword">global</span> python <span class="hljs-number">3.12</span><span class="hljs-number">.2</span></pre></div><h1 id="d784">Conclusion</h1><p id="e243">I guess I had multiple choices I could’ve made and avoid breaking my computer as I explained in my previous article.</p><p id="ebe1">The problem is that I was so obsessed with making Python 3.12 my default system’s Python that I ignored all these solutions. I made a mistake that led me to learn lots of things I didn’t know.</p><p id="6304">Mistakes are part of life. We learn and grow from our mistakes. I have no problem to admit that I made them every day.</p><p id="4c75">These are the lessons that I took from this particular mistake:</p><ul><li>You don’t mess with the System’s Python. Especially if you’re on Linux.</li><li>There are multiple ways of managing Python versions (like <i>asdf, </i>which<i> </i>is a new tool I learned)</li><li>I did not know about the shim design pattern.</li><li>And many more</li></ul><p id="c255">There are still other ways for you to install and manage Python versions, but I find these to be better, and currently, I’m favoring <i>pyenv </i>and using asdf for other Programming Languages like Node Js.</p><p id="619a"><i>Hi! Thank you for your time reading my article. If you enjoyed this article and would like to receive similar content directly to your inbox</i></p><div id="1397" class="link-block"> <a href="https://yanick-andrade.medium.com/subscribe"> <div> <div> <h2>Join me on this journey to discover Python and more about programming</h2> <div><h3>Join me on this journey to discover Python and more about programming Medium is a beautiful place. Plus, you'll be…</h3></div> <div><p>yanick-andrade.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*dyFQqDwm6FQdY7nt)"></div> </div> </div> </a> </div><h1 id="cd6c">In Plain English 🚀</h1><p id="656e"><i>Thank you for being a part of the <a href="https://plainenglish.io"><b>In Plain English</b></a> community! Before you go:</i></p><ul><li>Be sure to <b>clap</b> and <b>follow</b> the writer ️👏<b>️️</b></li><li>Follow us: <a href="https://twitter.com/inPlainEngHQ"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/inplainenglish/">LinkedIn</a> | <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a> | <a href="https://newsletter.plainenglish.io/">Newsletter</a></b></li><li>Visit our other platforms: <a href="https://stackademic.com/"><b>Stackademic</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a> | <a href="https://blog.cubed.run">Cubed</a></b></li><li>More content at <a href="https://plainenglish.io"><b>PlainEnglish.io</b></a></li></ul></article></body>

3 Ways To Install and Manage Multiple Python Versions

Works For any Operating System

AI-Generated Image by The Author

This article is a result of one of my previous articles: Read This Before You Install Python3.12 On Linux.

In the mentioned article I explained how I “broke” my Ubuntu Operating System (OS) while trying to install and make Python3.12 my default Python version.

I’m glad that the article reached many people and I received so much great feedback that I could honestly take a lot from them.

I decided to write this one because I feel that there’s still to say about installing Python or managing Python versions on whatever OS you’re working on.

But before I continue, I would like to take a moment to publicly say a thank you to Tony Csoka for telling me about Pyenv.

Thank you to Tom Harrison and H. Habighorst for taking the time to share valuable advice and personal insights with us in the comment section.

And everyone else who shared their point of view. It is always nice to learn and grow with people who want to genuinely share.

Without further ado, let’s get into what matters the most.

Installing Python Using Anaconda

I’m starting with this because this was my first choice when I was considering installing Python3.12.

What is Anaconda? Anaconda is a distribution of Python aimed mostly at Data Scientists.

It comes with its package management tool called conda. With conda, you can install packages, and manage a virtual environment similar to the way we do with pip:

conda install <package>

Creating a virtual environment with conda is very easy to do, you just run the following command:

conda create -n <env_name>

Running a command like this will create a conda environment using the system's Python version.

If you want to install and use a specific version, that can be easily done as follows:

conda create -n <env_name> python=3.12 # the version you want to install

— So why didn’t I stick with this?

To explain my decision we would need to cover the difference between pip and conda on how they manage a virtual environment and everything.

Pip uses PyPi as a repository to download a library when you want to install some on your machine.

Conda uses a repository named conda-forge which is owned by Anaconda.

From my experience, you might find libraries that are not in the most recent versions in conda-forge, unlike pip.

The way dependencies are managed in conda is different from the way it’s managed in pip, which is the default used in my team.

In my team we have a set of tasks we perform before deploying a project, and using different tools — conda —, could have confused when trying to deploy.

So one tool to develop and deploy. This way I reduce the probability of having surprises later on.

Using pyenv

If you are familiar with Node Js you probably know Node Version Manager (nvm) that allows you to install and switch between Node Js versions easily.

It happens that Python also has its own version manager — pyenv.

Pyenv allows us to switch between Python versions easily on our machine. It does not install Python globally on your computer but inside a folder — versions — located inside the pyenv installation on your machine.

Screenshot by the Author

To install pyenv go to this link and choose the preferable method according to your OS. In my case is MacOS, so the commands I used are the following:

— Install pyenv

brew install pyenv

— Setup pyenv and enable shims

Pyenv uses the shim design pattern under the hood. What this means is that when we execute any Python command on our computer, it intercepts it and directs us to the selected Python version we set with it.

To set pyenv and enable shim on MacOS (if your OS is zsh and not bash), run the following commands:

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 running all these commands, reload the .zshrc file.

. ~/.zprofile

— Installing a specific Python version

Now that you’re all set, you can go ahead and install any available Python version you want. To see all versions available run the following command:

pyenv install -l

You should see a long list of available versions like this one:

Screenshot by the Author

We can install the desired version as follows:

pyenv install <version> # 3.12.2

This will install Python 3.12.2 inside our versions folder as I explained earlier:

Screenshot by the Author

Now we can set Python 3.12 as default using pyenv:

pyenv global 3.12.2

Now your default Python is 3.12.2:

Screenshot by the Author

There are lots of things I would like to cover about pyenv, but this has to be in another article entirely dedicated to it.

Things like managing a virtual environment with pyenv, using pip, using local pyenv instead of global (like we did here), and so on.

Using asdf

Unlike pyenv, asdf is a multi-language tool for version management. It can be used with Node Js, Python, Ruby, etc.

Each programming language is considered to be a plugin that you can add to asdf and manage its version.

Like pyenv, it is based on shims design pattern. Each plugin (programming language) will have its shim to ‘intercept’ and handle executions.

To install it, go ahead and choose the proper installation instructions according to your OS. If you’re using MacOS, you can download and install it by running the following commands:

# first install dependencies if not installed already
brew install coreutils curl git

You have two installation options.

— Official download

This is the recommended method of installation. It clones the repository inside a folder named .asdf in your root directory:

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0

— Community Supported Downloads

In my case, I installed it using this command.

brew install asdf

If you downloaded asdf using Git, finish it by running the following command you find the website according to your Shell and OS.

— Using asdf with Python

As I said earlier, each programming language and other tools in asdf is a plugin. A plugin can be added as follows:

asdf plugin add <plugin_name>

If you wish to see the available plugins you can install and use, run the the following command:

asdf plugin list all

This will list all available plugins:

Screenshot by the Author

Adding a Python plugin can be easily done by running the following command:

asdf plugin add python

After adding a new plugin, you can now install any version of that plugin that you want. Installing a version using asdf is similar to the way we install using pyenv:

asdf install python 3.12.2 # install version 3.12.2 for Python plugin

Setting a preferable version using asdf is also very similar to the way we do using pyenv:

asdf global python 3.12.2

Conclusion

I guess I had multiple choices I could’ve made and avoid breaking my computer as I explained in my previous article.

The problem is that I was so obsessed with making Python 3.12 my default system’s Python that I ignored all these solutions. I made a mistake that led me to learn lots of things I didn’t know.

Mistakes are part of life. We learn and grow from our mistakes. I have no problem to admit that I made them every day.

These are the lessons that I took from this particular mistake:

  • You don’t mess with the System’s Python. Especially if you’re on Linux.
  • There are multiple ways of managing Python versions (like asdf, which is a new tool I learned)
  • I did not know about the shim design pattern.
  • And many more

There are still other ways for you to install and manage Python versions, but I find these to be better, and currently, I’m favoring pyenv and using asdf for other Programming Languages like Node Js.

Hi! Thank you for your time reading my article. If you enjoyed this article and would like to receive similar content directly to your inbox

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

Python
Programming
Data Science
Technology
Machine Learning
Recommended from ReadMedium