0%

use pyenv to manage multiple versions of python

Here are some keynotes from python version management system - pyenv

installation

1
$ curl https://pyenv.run | bash

install necessary packages based on your linux distribution,
refer https://github.com/pyenv/pyenv/wiki#suggested-build-environment

mirror the download site

follow pyenv mirror guide to build the site, remember the filename of the archieves must be the md5sum of its contents.
Which means most of the public mirror sites does not work in pyenv need.

a solution for this case: download the archieves from the public mirror sites and place them under $(pyenv root)/cache,

customize the build

1
2
3
4
5
6
$ env PYTHON_CONFIGURE_OPTS="--enable-shared --enable-unicode=ucs4 --enable-optimizations" pyenv install 2.7.18
$ find $(pyenv root)/versions/2.7.18 -exec file {} + | grep -i 'elf.*not stripped' | awk -F: '{print $1}' | xargs strip
$ readelf -d $(pyenv root)/versions/2.7.18/bin/python
$ env PYTHON_CONFIGURE_OPTS="--enable-shared --enable-optimizations LDFLAGS=-s" pyenv install 3.7.9
$ pyenv SHELL 3.7.9
$ python -c "import sysconfig; abc=sysconfig.get_config_vars(); import pprint; pp=pprint.PrettyPrinter(indent=4); pp.pprint(abc);" | vi -
  • –enable-optimizations: expensive optimizations that slow down the build process but yield a significant speed boost
  • –enable-shared: some extension required python shared library. rpath will be set in this case.
  • –enable-unicode: python version 2.2 through 3.2 depends on this to representation of unicode strings. utf-16 UCS2/utf-32 UCS4
    python -c “import sys; print(sys.maxunicode)” UCS4: 1114111/UCS2: 65535
    python 3.7.9 will always returns 1114111
  • LDFLAGS=-s: strip all symbols

install specific version for docker image

use python-build plugin

windows version

  • clone pyenv-win and make symbol link C:\python using mklink /D

  • need script to generate regedit entry