Skip to content

Instantly share code, notes, and snippets.

@oleksis
Last active July 23, 2023 04:28
Show Gist options
  • Save oleksis/7cab1772862df71f73ce22b7515f6af3 to your computer and use it in GitHub Desktop.
Save oleksis/7cab1772862df71f73ce22b7515f6af3 to your computer and use it in GitHub Desktop.

Pyenv

Debian/Ubuntu: See Suggested build environment

cd $HOME/.pyenv/plugins/python-build/../.. && git pull && cd -
➜ PYTHON_LAST='3.10.12'
➜ OPENSSL_DIR=/usr/local/ssl
➜ pushd /tmp
➜ curl -fsSL -o openssl-1.1.1u.tar.gz https://www.openssl.org/source/openssl-1.1.1u.tar.gz
➜ tar -xzf  openssl-1.1.1u.tar.gz
➜ pushd openssl-1.1.1u
➜ ./config no-shared --prefix=${OPENSSL_DIR} --openssldir=${OPENSSL_DIR} \
    CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2" \
    CFLAGS="-g -O2 -Wall -fdebug-prefix-map=/=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC" \
    CXXFLAGS="-g -O2 -Wall -fdebug-prefix-map=/=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC" \
    LDFLAGS="-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC"
➜ make
➜ sudo make install_sw
➜ popd; popd
➜ CPPFLAGS="-O2 -I${OPENSSL_DIR}/include" CFLAGS="-I${OPENSSL_DIR}/include" \
    LD_FLAGS="-L${OPENSSL_DIR}/lib -Wl,-rpath,${OPENSSL_DIR}/lib" LD_RUN_PATH="${OPENSSL_DIR}/lib" \
    CONFIGURE_OPTS="--with-openssl=${OPENSSL_DIR}" PYTHON_CONFIGURE_OPTS="--enable-shared" \
    pyenv install ${PYTHON_LAST} 
echo $PYENV_ROOT
➜ pyenv local 3.10.12
➜ pyenv exec python3 -V
Python 3.10.12

Dont worry about Where is Python; use py launcher (for Unix)

...shorthand for launching python without having to think about which Python interpreter is the most desired.

py - launch a Python interpreter

➜ py -3.10 -c "import sys; print(sys.version, sys.executable, sep='\n')"
3.10.12 (main, Jul 22 2023, 00:37:31) [GCC 11.3.0]
/home/user/.pyenv/versions/3.10.12/bin/python3.10
➜ echo ${env:PYENV_ROOT}
➜ pyenv local 3.10.12
➜ pyenv exec python3 -V
Python 3.10.12

Python Launcher for Windows

Windows 11

Get-Command py

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     py.exe                                             2.0.0.0    C:\WINDOWS\py.exe

Customized Python

  • $env:LOCALAPPDATA\py.ini
[commands]
vpython=C:\Users\user\.pyenv\pyenv-win\versions\3.10.9\python.exe
  • Python Script Launching t.py
#! /usr/bin/vpython
import sys

print(sys.version, sys.executable, sep='\n')

Locating Pythons in 64bit registry

Install Python and add to the Register version for py launcher

➜ pyenv install --register 3.9.13
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Downloading] ::  3.9.13 ...
:: [Downloading] ::  From https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe
:: [Downloading] ::  To   C:\Users\oleksis\.pyenv\pyenv-win\install_cache\python-3.9.13-amd64.exe
:: [Installing] ::  3.9.13 ...
:: [Info] :: completed! 3.9.13

➜ py -0p
 -V:3.9.13        C:\Users\oleksis\.pyenv\pyenv-win\versions\3.9.13\python.exe
 
➜ py -3.9 -c "import sys; print(sys.version, sys.executable, sep='\n')"
3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
C:\Users\oleksis\.pyenv\pyenv-win\versions\3.9.13\python.exe
PythonCore.reg
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11]
"DisplayName"="Python 3.11"
"SupportUrl"="https://www.python.org/"
"SysArchitecture"="64bit"
"SysVersion"="3.11"
"Version"="3.11.1"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11\Help]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11\Help\Online Python Documentation]
@="https://docs.python.org/3.11"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11\Idle]
@="C:\\Users\\oleksis\\.pyenv\\pyenv-win\\versions\\3.11.1\\Lib\\idlelib\\Lib\\idlelib\\idle.pyw"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.11\InstallPath]
@="C:\\Users\\oleksis\\.pyenv\\pyenv-win\\versions\\3.11.1"
"WindowedExecutablePath"="C:\\Users\\oleksis\\.pyenv\\pyenv-win\\versions\\3.11.1\\pythonw3.11.exe"
"ExecutablePath"="C:\\Users\\oleksis\\.pyenv\\pyenv-win\\versions\\3.11.1\\python3.11.exe"

  • Run py with python from pyenv
$env:PYLAUNCHER_DEBUG=1
➜ py -V
launcher build: 32bit
launcher executable: Console
Using local configuration file 'C:\Users\user\AppData\Local\py.ini'
File 'C:\WINDOWS\py.ini' non-existent
Called with command line: -V
locating Pythons in 64bit registry
locate_pythons_for_key: unable to open PythonCore key in HKCU
locate_pythons_for_key: C:\Users\user\.pyenv\pyenv-win\versions\3.10.9\python3.10.exe is a 64bit executable
locate_pythons_for_key: : El sistema no puede encontrar la ruta especificada.
locate_pythons_for_key: : El sistema no puede encontrar la ruta especificada.
locate_pythons_for_key: : El sistema no puede encontrar la ruta especificada.
locating Pythons in native registry
locate_pythons_for_key: unable to open PythonCore key in HKCU
locate_pythons_for_key: unable to open PythonCore key in HKLM
locate_pythons_for_key: unable to open PythonCore key in HKLM
found no configured value for 'python'
search for default Python found version 3.10 at 'C:\Users\user\.pyenv\pyenv-win\versions\3.10.9\python3.10.exe'
run_child: about to run 'C:\Users\user\.pyenv\pyenv-win\versions\3.10.9\python3.10.exe -V'
Python 3.10.9
child process exit code: 0

Environment variable

$env:VIRTUAL_ENV

➜ pyenv install 3.11.1
:: [Info] ::  Mirror: https://www.python.org/ftp/python

➜ python311 -m venv venv
➜ $env:VIRTUAL_ENV="${PWD}/venv"  # ./venv/Scripts/Activate.ps1
➜ py -c "import sys; print(sys.version, sys.executable, sep='\n')"
3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)]
C:\Users\user\venv\Scripts\python.exe
  • Py Launcher no available in:
    • Windows 22H2 (OS Build 19045.2311) (Windows 10)
    • Windows 21H2 (OS Build 22000.1335) ( Windows 11)

Python from Windows Store

➜ winget install -q "Python 3.10" --accept-package-agreements --source msstore

Win10 / Win11: CPython launcher.c

  • Registry: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppModel\Lookaside\user\Software\Python\PythonCore\3.10\InstallPath

Links

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