Skip to content

Instantly share code, notes, and snippets.

@bracca95
Last active October 10, 2024 12:05
Show Gist options
  • Select an option

  • Save bracca95/194f7cf6d3d49d5e2a256b90a3cf222c to your computer and use it in GitHub Desktop.

Select an option

Save bracca95/194f7cf6d3d49d5e2a256b90a3cf222c to your computer and use it in GitHub Desktop.
A pipeline to configure pyenv (user level)

This guide takes for granted that pyenv has already been installed on your system. In this is not the case, ask an admin to refer to the pyenv guide. These instructions are meant to be copy-pasted: variables introduced by the symbol <> are those that might change from one system to another, so be carefull if you are moving to another platform or have different configurations.

Important!!

As a general guideline, we suppose that you are going to download only one python version but you might have more virtual environments. For these reason, all the commands, except the 5th point, must be run once. When you want to create a virtual enviroment, simply run the 5th point with a different virtualenv name.

1. Download pyenv

# copy paste
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

2. Link pyenv to your shell

The following commands must be run only once per user.

Variables:

  • <~/.bashrc>: could be ~/.bashrc, ~/.zshrc, ...
  • <~/.profile>: could be ~/.profile, ~/.bash_profile, ~/.zsh_profile, ...

Commands to run:

# copy paste
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
# copy paste
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile && \
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile && \
echo 'eval "$(pyenv init -)"' >> ~/.profile

Restart the shell:

# copy paste
source ~/.bashrc`

3. Install a python version in your local environment

The following commands must be run only once per user.

Variables:

  • <3.10.13>: any python version that you want
# copy paste
pyenv install 3.10.13

4. Locate a directory linked with the downloaded python version

We are going to locate a directory in which the command python returns the selected (downloaded) python version.

Variables:

  • <~/.python_versions>: any path in your $HOME
  • <3.10.13>: any python version

Create a directory for that version

# copy paste
mkdir -p ~/.python_versions/3.10.13

Use the directory to handle the python version

# copy paste
cd ~/.python_versions/3.10.13 && \
pyenv local 3.10.13

5. Create a virtual environment

Variables:

  • <venv_name>: any name
# copy paste
cd ~/.python_versions/3.10.13 && \
python -m venv venv_name

Activate the virtualenv

Whenever you want to use your virtualenv, run:

# copy paste
source ~/.python_versions/3.10.13/venv_name/bin/activate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment