Setting up pyenv
This guide will help you set up pyenv
on your Mecha Comet. We are told that Python developers love it 😍.
pyenv
is a lightweight, flexible tool for managing multiple Python versions. It lets you -
- Easily switch between Python versions
- Create and manage isolated Python environments for different projects
- Avoid conflicts between system Python and custom-installed Python versions
Installing pyenv
List all available python versions using the command below.
$ sudo apt update
$ sudo apt curl git
Run the below command to install pyenv
$ curl https://pyenv.run | bash
Now lets configure the default shell to include pyenv
in the PATH
. Add the following lines to your shell configuration file (e.g., ~/.bashrc
, ~/.zshrc
):
# Assuming your shell is bash
$ nano ~/.bashrc
Add the following lines at the end of the file.
# ~/.bashrc
...
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Restart your shell for the changes to take effect -
$ exec "$SHELL"
To confirm that pyenv
is installed correctly, run the below command.
$ pyenv --version
Setting up a project to test pyenv
Follow the quick guide below to test pyenv
by setting up a test project at a certain python version.
List all available Python versions using the command below and lets install a specific version, for example 3.11.0
.
$ pyenv install --list
$ pyenv install 3.11.0
Create a project directory and set Python version for the project.
$ mkdir my_project
$ cd my_project
Now to create a virtual environment for your project
$ pyenv virtualenv 3.11.0 my_env
To activate the environment`
$ pyenv activate my_env
You should see the command prompt change to reflect the environment name my_env
. You are now all set!
In a new terminal when you resume your work, you will only need to run the above command that activates the virtual environment.
Now lets confirm if python is running at this version in the virtual environment.
In the same folder, create a new main.py
$ nano main.py
Add the following code that prints the python version.
import sys
print(sys.version)
Now lets run this program, while in an activate virtual environment`
$ python main.py
3.11.0 (main, Feb 16, 18:14:09) [GCC 11.4.0]
You have successfully configured and tested pyenv
and virtualenv
now!
Some useful commands
Here are some common commands that you will need at some point when using pyenv
.
List all available Python versions
$ pyenv install --list
Install a specific Python version
$ pyenv install 3.11.4
Set a global Python version
$ pyenv global 3.11.4
Set a local Python version for a project
$ pyenv local 3.10.8
View the current Python version
$ pyenv version
List all versions installed
$ pyenv versions