Level up your python development environment
It's all about Python and virtual environments. The "old way" is you fiddle:ing with [virtualenv](https://docs.python.org/3/library/venv.html) so you can separate your python development setup from your systems. But if you are like me, I always forget that `source venv/bin/activate` and suddenly you're messing up your system with all those `pip install random-package`.
Hence Direnv ...
From the manual: "direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory".
sudo apt-get install direnv
or curl -sfL https://direnv.net/install.sh | bash
Once direnv
is installed you need to configure your $SHELL
in order to hook it with your default shell.
It supports bash
, zsh
, fish
, tcsh
and elvish
. I prefer Z-Shell with Oh My Zsh (since it looks cool and have a bunch of plugins like git, kubectl etc) so I proceed with examples for zsh, but the procedures are the same for those other shells
echo 'eval "$(direnv hook zsh)"' >> ~/.zshrc
source ~/.zshrc
Thanx to direnv
we now can load a virtualenv as soon as we enter a specific folder.
- All you need to to is to specify that layout command in a
.envrc
file located at the root of your project.
git clone [email protected]:j0nix/j0nix.git && cd j0nix
echo 'layout python3' > .envrc
direnv allow
Now you have a virtual python environment, like doing that
source venv/bin/activate
but with automagic as soon as you enter thatgit
repository folder.So when you leave your project folder, you automagicly leave that virtualenv. Pretty neat!
pyenv lets you easily switch between multiple versions of Python. We want the latest greatest so lets go get that into our system.
curl -L https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Before proceeding getting a new cool python version, fire away this command
sudo apt install curl libncursesw5-dev libxml2-dev libxmlsec1-dev llvm make wget xz-utils build-essential tk-dev zlib1g-dev zlibc libbz2-dev libncurses-dev libffi-dev libreadline-dev libssl-dev liblzma-dev libsqlite3-dev
Above will hopefully prevent you from failing for hours when installing python via pip
pyenv install 3.11.2
(latest 3.11 when this guide was created)
Now, in that git
repo folder, instead instruct direnv to load pyenv 3.11.2
echo 'layout pyenv 3.11.2' > .envrc
direnv allow
If everything went according to plan, direnv
will automagicly load python 3.11.2.
- verify using
which python
ex)
project git:(master) ✗ which python
/home/j0nix/j0nix/.direnv/python-3.11.2/bin/python
finish up installing ansible
pip install ansible ansible-lint
yum install zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
sudo yum install direnv
echo 'eval "$(direnv hook zsh)"' >> ~/.zshrc
source ~/.zshrc
sudo yum install zlib-devel bzip2-devel bzip2-libs ncurses-devel ncurses-libs libffi-devel openssl-devel readline-devel tk-devel libsqlite3x-devel xz-devel
git clone [email protected]:j0nix/platform-services.git && cd platform-services
curl -L https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
pyenv install 3.11.2
echo 'layout pyenv 3.11.2' > .envrc
direnv allow
j0nix