7.9 C
New York
Sunday, November 24, 2024

Too Many Python Variations to Handle? Pyenv to the Rescue


Too Many Python Versions to Manage? Pyenv to the Rescue
Picture by Creator

 

Need to spend your morning attempting out the brand new options within the newest Python model…And your lunch break sifting by a legacy Python codebase—all with out breaking your improvement atmosphere?

Sure, it’s doable. And Pyenv is right here to assist. With Pyenv, you possibly can set up Python variations, change between variations, and take away variations that you simply now not want.

This tutorial is a fast introduction to organising and utilizing Pyenv. So let’s get began!

 

 

Step one is to put in Pyenv. I exploit Linux: Ubuntu 23.01. So, in case you are on a Linux machine, the best approach to set up Pyenv is by operating the next curl command:

$ curl https://pyenv.run | bash

 

This installs Pyenv utilizing the pyenv-installer

After the set up is full, you’ll be prompted to complete organising your shell atmosphere to make use of Pyenv. To take action, you possibly can add the next command to your ~/.bashrc file:

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

echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

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

 

And also you’re all set to begin utilizing Pyenv!

 

Be aware: If you’re on a Mac or a Home windows machine, take a look at these detailed directions on learn how to set up Pyenv. On Home windows, you have to set up Pyenv within the Home windows Subsystem for Linux (WSL).

 

 

Now that you simply’ve put in Pyenv, you possibly can set up particular Python variations by operating the pyenv set up command like so:

 

To verify the record of put in Python variations, run the next command:

$ pyenv variations
* system (set by /dwelling/balapriya/.pyenv/model)

 

I haven’t put in any new model but, so the one model of Python is the system model. Which is Python 3.11 in my case:

$ python3 –model

Python 3.11.4

 

Let’s attempt to set up Python 3.8 and three.12. Strive operating this command to put in Python 3.8:

 

The primary time you attempt to set up a selected model of Python with Pyenv, you might in all probability run into errors. Due to some lacking construct dependencies. No worries. It’s straightforward to repair!

 

⚙️ Some Troubleshooting Ideas

 

When attempting to put in Pyenv on my Linux distro utilizing the pyenv set up command, I bumped into errors due to lacking construct dependencies.

This StackOverflow thread accommodates useful info on putting in the required construct dependencies for Pyenv. Run the next command to put in the lacking dependencies:

$ apt-get set up build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev

 

You need to now be capable to set up the Python variations with none errors:

 

Be aware: If you set up Python 3.x, by default, the latest launch is put in. However you even have extra granular management and may specify 3.x.y for putting in a selected launch of a Python model. You can even run pyenv set up --list to get an inventory of all out there Python variations to put in. This, nevertheless, is a really lengthy record.

 

Equally run pyenv set up to put in Python 3.12:

 

Now in the event you run pyenv variations you’ll see Python 3.8 and three.12 along with the system model:

$ pyenv variations
* system (set by /dwelling/balapriya/.pyenv/model)
3.8.18
3.12.0

 

 

With Pyenv, you possibly can set a international Python model. Because the identify suggests, the worldwide model is the model of Python that’s used anytime you employ Python on the command line.

However watch out to set it to a comparatively current model to keep away from errors when operating initiatives that use newer Python variations.

For instance, let’s have a look at what occurs if we set the worldwide model to Python 3.8.18.

 

Create a undertaking folder. In it, create a important.py file with the next code:

# important.py

def handle_status_code(status_code):
    match status_code:
        case 200:
             print(f"Success! Standing code: {status_code}")
        case 404:
            print(f"Not Discovered! Standing code: {status_code}")
        case 500:
            print(f"Server Error! Standing code: {status_code}")
        case _:
            print(f"Unhandled standing code: {status_code}")

status_code = 404  # oversimplification, sure. handle_status_code(status_code)

 

As seen, this code makes use of the match-case assertion which was launched in Python 3.10. So that you want Python 3.10 or a later model for this code to run efficiently. If you happen to attempt operating the script, you’ll get the next error:

File "important.py", line 2
	match status_code:
      	^
SyntaxError: invalid syntax

 

In my case, the system Python is model 3.11 which is sort of current. So I can set the worldwide model to the system Python model like so:

 

If you now operating the identical script, you need to get the next output:

Output >>>
Not Discovered! Standing code: 404

 

In case your system Python is an older model, say Python 3.6 or earlier, it is useful to put in a more moderen model of Python and set it as the worldwide model.

 

 

If you need to work on initiatives that use earlier variations of Python, you’ll need to set up that model to keep away from any errors (like technique calls which might be now not supported).

Say you need to use Python 3.8 when engaged on undertaking A, and Python 3.10 or later when engaged on undertaking B.

 

Too Many Python Versions to Manage? Pyenv to the Rescue
Picture by Creator

 

In such instances, you possibly can set the native Python model within the undertaking A’s listing like so:

 

You possibly can run python --version to verify the Python model within the undertaking listing: 

$ python --version
Python 3.8.18

 

That is particularly useful when engaged on older Python codebases.

 

 

If you happen to now not want a Python model, you possibly can uninstall it by operating the pyenv uninstall command. Say we don’t want Python 3.8.18 anymore, so we are able to uninstall it by operating the next command:

 

You need to see an identical output on the terminal:

pyenv: take away /dwelling/balapriya/.pyenv/variations/3.8.18? [y|N] y
pyenv: 3.8.18 uninstalled

 

 

I hope you discovered this introductory tutorial on Pyenv useful. Let’s evaluate a number of the commonest instructions for fast reference:

Command Operate
pyenv variations Lists all Python variations at present put in
pyenv set up --list Lists all Python variations out there to put in
pyenv set up 3.x Installs the most recent launch of Python 3.x 
pyenv set up 3.x.y Installs launch y of Python 3.x 
pyenv international 3.x Units Python 3.x as the worldwide Python model
pyenv native 3.x Units the native Python model on your undertaking to three.x
pyenv uninstall 3.x.y Uninstalls launch y of Python 3.x

 

In case you’re questioning. Sure, you possibly can use Docker which is a wonderful choice to make native improvement a breeze—with out worrying about dependency conflicts. However you’d in all probability really feel it’s overkill to make use of Docker or different containerization options everytime you have to work on a brand new undertaking.

So I feel it’s nonetheless useful to have the ability to set up, handle, and change between Python variations on the command-line. You can even discover the pyenv-virtualenv plug-in to create and handle digital environments. Completely happy coding!
 
 

Bala Priya C is a developer and technical author from India. She likes working on the intersection of math, programming, information science, and content material creation. Her areas of curiosity and experience embrace DevOps, information science, and pure language processing. She enjoys studying, writing, coding, and occasional! At the moment, she’s engaged on studying and sharing her data with the developer neighborhood by authoring tutorials, how-to guides, opinion items, and extra.



Related Articles

Latest Articles