Qin Yu, Apr 2020
- How to Reinstall & Manage Multiple Pythons
- How to Install & Manage Multiple Pythons (on Ubuntu 18.04)
- How to Reinstall & Manage Multiple Pythons (on macOS 10.14 Mojave)
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.
$ curl https://pyenv.run | bash
$ exec $SHELL
$ pyenv updateIf cannot install, see prerequisites for pyenv.
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-
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.
-
pyenv commandslists all available pyenv commands. -
Firstly,
pyenv globalreports the currently configured global version of Python. To list all the versions available throughpyenvon your machine usingpyenv versions. Note thatversionandversionsare 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.[*]
-
Install another Python with specified version:
- list all versions available:
$ pyenv install --list
- install the version:
$ pyenv install anaconda3-2019.10
- list all versions available:
-
cdto your working directory -
Check current the currently configured local version:
$ pyenv localThere should be
pyenv: no local version configured for this directory. -
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.10Now one should see the shell prompt with additional text
(anaconda3-2019.10).
Check or change the global version the same way.
Qin Yu, Jun 2019
It drove me crazy when various frameworks each requires a specific version of Python...
-
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
-
Then I shall delete the Homebrew installed Python:
$ brew list | grep 'python' $ brew uninstall -f python python@2 # this means Python2
-
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
-
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
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.