Skip to content

Instantly share code, notes, and snippets.

@qin-yu
Last active April 6, 2020 23:31
Show Gist options
  • Save qin-yu/bf628fc414c27d05b82cfb9ab8e4e3ad to your computer and use it in GitHub Desktop.
Save qin-yu/bf628fc414c27d05b82cfb9ab8e4e3ad to your computer and use it in GitHub Desktop.
Reinstall & Manage Multiple Python(s)

How to Reinstall & Manage Multiple Pythons

Qin Yu, Apr 2020

How to Install & Manage Multiple Pythons (on Ubuntu 18.04)

Qin Yu, Apr 2020

Here I present how to choose and install Python 3 on Ubuntu, and manage virtual environments and packages using.

Install pyenv

Important: Please make sure eval "$(pyenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH during the initialization.

Auto-installation (not as auto as it sounds)

$ curl https://pyenv.run | bash
$ exec $SHELL
$ pyenv update

If cannot install, see prerequisites for pyenv.

Manual installation

This works best for our Ubuntu 12.04 workstations which default to a .bashrc configuration file in lieu of .bash_profile.

$ cd
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ source ~/.bashrc

Some features of pyenv

  • pyenv does:

    • Let you change the global Python version on a per-user basis.
    • Provide support for per-project Python versions.
    • Allow you to override the Python version with an environment variable.
    • Search commands from multiple versions of Python at a time. This may be helpful to test across Python versions with tox.
  • In contrast with pythonbrew and pythonz, pyenv does not:

    • Depend on Python itself. pyenv was made from pure shell scripts. There is no bootstrap problem of Python.
    • Need to be loaded into your shell. Instead, pyenv's shim approach works by adding a directory to your $PATH.
    • Manage virtualenv. Of course, you can create virtualenv yourself, or pyenv-virtualenv to automate the process.
  1. pyenv commands lists all available pyenv commands.

  2. Firstly, pyenv global reports the currently configured global version of Python. To list all the versions available through pyenv on your machine using pyenv versions. Note that version and versions are two commands.

    Usually you will only have one version of python installed, the system-wide version. That’s what’s shown in the above command. pyenv now allows you to expand upon this version. Let’s start by installing another python version.[*]

  3. Install another Python with specified version:

    1. list all versions available:
      $ pyenv install --list
    2. install the version:
      $ pyenv install anaconda3-2019.10
  4. cd to your working directory

  5. Check current the currently configured local version:

    $ pyenv local

    There should be pyenv: no local version configured for this directory.

  6. Sets a local application-specific Python version by writing the version name to a .python-version file in the current directory:

    $ pyenv local anaconda3-2019.10

    Now one should see the shell prompt with additional text (anaconda3-2019.10).

Check or change the global version the same way.

Reference

How to Reinstall & Manage Multiple Pythons (on macOS 10.14 Mojave)

Qin Yu, Jun 2019

It drove me crazy when various frameworks each requires a specific version of Python...

Remove All Current Python

  1. Firstly, get rid of the official ones:

    $ sudo rm -rf '/Applications/Python X.Y' #replace X.Y with the version number on the folder
    $ sudo rm -rf /Library/Frameworks/Python.framework
    $ sudo rm -rf /usr/local/bin/python
    $ sudo rm -rf /usr/local/bin/python3
  2. Then I shall delete the Homebrew installed Python:

    $ brew list | grep 'python'
    $ brew uninstall -f python python@2  # this means Python2
  3. Now I kill my least favourite Anaconda Installations:

    $ rm -rf ~/anaconda2
    $ rm -rf ~/anaconda3
    $ rm -rf ~/miniconda2
    $ rm -rf ~/miniconda3
    $ rm -rf ~/.condarc ~/.conda ~/.continuum
  4. Uninstall Python from Pyenv:

    $ pyenv versions

    After obtaining all the python versions, do, e.g.:

    $ pyenv uninstall 3.5.0
    $ pyenv uninstall 3.6.0

Reinstall via pyenv & Anaconda

Look, it took me hours to be aware of the integration of Anaconda into pyenv... Once pyenv is brew-ed into my macOS you can pyenv install --list and see all the Anaconda versions including anaconda3-2019.03, which fails because of the exeptionally slow access to the open internet from China.

Download the required files into ~/.pyenv/cache makes things easier.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment