Skip to content

Latest commit

 

History

History
223 lines (163 loc) · 6.07 KB

File metadata and controls

223 lines (163 loc) · 6.07 KB

Python

Python can be a complete mess. Here are my 3.x best practices:

  • I believe in extending the os-distro system python as little as possible.
  • I believe in using a user-mode multi-version method for python similar but not identical for ruby and node.
  • Since 2020, I assume a py 3.x os-distro system python for preparing overlays exist. (ugh.. macOS)
  • I use pyenv to shim in py 3.x as global, and pyenv virtualenvs as dev project environments.
  • I use pipx to install matrix.dot.file bins and inject libs for those bins that are python based like powerline-status into a venv.
  • I use pip to install global libraries that aren't bin based.
  • I use poetry to manage python developed sandbox projects.

Import note on MacOS - Homebrew

Homebrew will jerk around your python isntallation and completely destroy your pips and libs associated during a 3.X->3.X+1 transition. Be aware!

PreRequisites

Ubuntu Server

sudo apt install -y build-essential checkinstall
sudo apt install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev zlib1g-dev
sudo apt install -y libffi-dev 

Install pyenv to manage Python Versions

# Install PREREQs first
curl https://pyenv.run | bash

Don't use system natives like brew or apt

Observe installed versions

$> pyenv versions
system 
* 3.9.0 (set by /Users/XXXX/.pyenv/version)
$> python --version
Python 3.9.0
$> python3 --version
Python 3.9.0

Upgrade Pyenv

pyenv update

Install a Sane Python

pyenv install 3.8.4
pyenv global 3.8.4

Restart your shell

Close and Restart the shell before continuing.

Install pipx to work with you

python -m pip install pipx

Optional: Install Pyenv-VirtualEnvs

pyenvhas built in capabilities to install virtualenv like environments

Create a new virtual env:

Note: you will want to set your Python Version pyenv shell 3.11.5 prior to Creating. Activating will automatically swap for you.

pyenv virtualenv myworkspace

List available virtual envs:

pyenv virtualenvs

There will be * next to an active virualenv. Also note there are autogenerated alias, not duplicates listed below.

pyenv virtualenvs
  3.11.10/envs/workspace (created from /home/shadowbq/.pyenv/versions/3.11.10)
  3.12.6/envs/playpen (created from /home/shadowbq/.pyenv/versions/3.12.6)
  playpen (created from /home/shadowbq/.pyenv/versions/3.12.6)
  workspace (created from /home/shadowbq/.pyenv/versions/3.11.10)

Use virtaul env:

pyenv activate myworkspace

Walk Through: Shell, CreateEnv, Activation, Disable, Reactivate - (Shell Prompt support):

shadowbq > main > ~\sandbox\projectA\ python --version
Python 3.12.6
shadowbq > main > ~\sandbox\projectA\ pyenv shell 3.11.10
Python 3.11.10
shadowbq > main > ~\sandbox\projectA\ pyenv virtualenv myworkspace
shadowbq > main > ~\sandbox\projectA\ pyenv activate myworkspace
shadowbq (emyworkspace > main > ~\sandbox\projectA\ python --version
Python 3.11.10
shadowbq (emyworkspace > main > ~\sandbox\projectA\ pyenv deactivate
shadowbq > main > ~\sandbox\projectA\ python --version
Python 3.12.6
shadowbq (emyworkspace > main > ~\sandbox\projectA\ pyenv activate myworkspace
Python 3.11.10

Optional: Enable Pyenv-doctor

Note: pyenv-doctor may not be available as pipx/pip

git clone git://github.com/pyenv/pyenv-doctor.git $(pyenv root)/plugins/pyenv-doctor

Optional: Building Python Packages for publication

You might want to build and publish to pypi.

pip install setupext-janitor
pip install wheel
pipx install twine

View Implementation Examples:

Publish using twine via makefile:

make clean
make upload

PIPX

PIPX can use multiple different versions of the python on your system!

Need NEW Python Version but not ready to swap Global/Default to NEW

$> python --version
Python 3.9.0

$> pyenv versions
  system
* 3.9.0 (set by /Users/XXXX/.pyenv/version)
  3.9.0/envs/myworkspace
  myworkspace --> /Users/XXXX/.pyenv/versions/3.9.0/envs/myworkspace
$> pyenv install 3.11.5
$> pyenv shell 3.11.5
$> pyenv versions
  system
* 3.9.0 (set by /Users/XXXX/.pyenv/version)
  3.9.0/envs/myworkspace
  3.11.5
  myworkspace --> /Users/XXXX/.pyenv/versions/3.9.0/envs/myworkspace
$> # Install pipx via pip3 without fear into each python version needed.
$> pip install pipx
$> pipx install abc-toolkit
$> pipx list
venvs are in /Users/XXXX/.local/pipx/venvs
apps are exposed on your $PATH at /Users/XXXX/.local/bin
   package abc-toolkit 3.3.1, Python 3.11.5
    - abc-tool
   package powerline-status 2.7, Python 3.9.0
    - powerline
    - powerline-config
    - powerline-daemon
    - powerline-lint
    - powerline-render
   package pygments 2.7.2, Python 3.9.0
    - pygmentize
$> pyenv shell --unset
$> python --version
Python 3.9.0
$> abc-tool
.... works!

Optional: Enable Poetry

poetry is a tool to handle dependency installation as well as building and packaging of Python packages. It only needs one file to do all of that: the new, standardized pyproject.toml. It can export requirements.txt for legacy usage.

poetry will also detect if you are inside a virtualenv and install the packages accordingly. So, poetry can be installed globally and used everywhere.

pipx install poetry

Example: Build Python Packages with Poetry

poetry init # creating your pyproject.toml config
poetry build
poetry publish

Notes on Legace Python 2.x

  • I NO longer use py 2.x as the assumed os-distro therefor - virtualenvwrapper & virtualenv are gone.
  • I NO longer use virtualenv-burrito as virtualenvwrapper is no longer used. (it served py 2.x very well, farewell)

Reference