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
pyenvto shim in py 3.x as global, andpyenv virtualenvsas dev project environments. - I use
pipxto install matrix.dot.filebinsand inject libs for those bins that are python based likepowerline-statusinto avenv. - I use
pipto install global libraries that aren't bin based. - I use
poetryto manage python developed sandbox projects.
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!
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 PREREQs first
curl https://pyenv.run | bashDon't use system natives like brew or apt
$> pyenv versions
system
* 3.9.0 (set by /Users/XXXX/.pyenv/version)
$> python --version
Python 3.9.0
$> python3 --version
Python 3.9.0pyenv updatepyenv install 3.8.4
pyenv global 3.8.4Close and Restart the shell before continuing.
python -m pip install pipxpyenvhas 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 (e) myworkspace > main > ~\sandbox\projectA\ python --version
Python 3.11.10
shadowbq (e) myworkspace > main > ~\sandbox\projectA\ pyenv deactivate
shadowbq > main > ~\sandbox\projectA\ python --version
Python 3.12.6
shadowbq (e) myworkspace > main > ~\sandbox\projectA\ pyenv activate myworkspace
Python 3.11.10Note: pyenv-doctor may not be available as pipx/pip
git clone git://github.com/pyenv/pyenv-doctor.git $(pyenv root)/plugins/pyenv-doctorYou might want to build and publish to pypi.
pip install setupext-janitor
pip install wheel
pipx install twineView Implementation Examples:
- https://raw.githubusercontent.com/d2iq-shadowbq/powerkube-fork/v0.2.1/setup.py
- https://raw.githubusercontent.com/d2iq-shadowbq/powerkube-fork/v0.2.1/Makefile
Publish using twine via makefile:
make clean
make uploadPIPX can use multiple different versions of the python on your system!
$> 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!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 poetrypoetry init # creating your pyproject.toml config
poetry build
poetry publish- I NO longer use py 2.x as the assumed os-distro therefor -
virtualenvwrapper&virtualenvare gone. - I NO longer use
virtualenv-burritoasvirtualenvwrapperis no longer used. (it served py 2.x very well, farewell)