Add Python virtual environment to iTerm Status Bar
Which requires changes/additions to the Zshell world.
This commit is contained in:
parent
6da8591a1d
commit
c7049e12e0
2 changed files with 97 additions and 6 deletions
|
@ -44,6 +44,7 @@ While Emacs supplies a Python editing environment, we’ll still use =use-packag
|
||||||
(flycheck-add-next-checker 'python-pylint 'python-pycompile 'append))
|
(flycheck-add-next-checker 'python-pylint 'python-pycompile 'append))
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
** Keybindings
|
** Keybindings
|
||||||
Instead of memorizing all the Emacs-specific keybindings, we use [[https://github.com/jerrypnz/major-mode-hydra.el][major-mode-hydra]] defined for =python-mode=:
|
Instead of memorizing all the Emacs-specific keybindings, we use [[https://github.com/jerrypnz/major-mode-hydra.el][major-mode-hydra]] defined for =python-mode=:
|
||||||
|
|
||||||
|
@ -136,11 +137,64 @@ Next, after reading David Vujic’s [[https://davidvujic.blogspot.com/2025/03/ar
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
** Virtual Environment
|
** Virtual Environment
|
||||||
When you need a particular version of Python, use [[https://github.com/pyenv/pyenv][pyenv]] globally:
|
Use the built-in module, venv, to create isolated Python environments for specific projects, enabling you to manage dependencies separately.
|
||||||
|
|
||||||
|
Create a virtual environment, either in the project’s directory, or in a global spot:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh
|
||||||
|
python3 -m venv .venv
|
||||||
|
#
|
||||||
|
python3 -m venv ~/.venv/my_project/
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
And then activate it:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh
|
||||||
|
source ~/.venv/my_project/bin/activate
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Or add that to a projects' =.envrc=.
|
||||||
|
|
||||||
|
Now, do what you need to do with this isolation:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh :tangle no
|
||||||
|
pip install -r test-requirements.txt
|
||||||
|
#+END_SRC
|
||||||
|
** Virtual Environment with new Python Version
|
||||||
|
Pyenv is a tool for managing multiple versions of Python on your machine, allowing you to switch between them easily. On a Mac, installed it via Homebrew:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh
|
||||||
|
brew install readline xz
|
||||||
|
brew install pyenv pyenv-virtualenv
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Or on other systems, use the /system/ Python to install [[https://github.com/pyenv/pyenv][pyenv]] globally:
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
pip install pyenv
|
pip install pyenv
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
Make sure we load this in [[file:zshell.org::*Python][the Zsh profile]]:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh
|
||||||
|
export PATH="$HOME/.pyenv/bin:$PATH"
|
||||||
|
eval "$(pyenv init -)"
|
||||||
|
eval "$(pyenv virtualenv-init -)"
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Install the python versions you need, for instance:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh
|
||||||
|
pyenv install 3.9.23
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Run =pyenv versions= to see what you have installed.
|
||||||
|
|
||||||
|
In any particular project directory, use a version you installed by creating a =.python-version= file, or call:
|
||||||
|
|
||||||
|
#+BEGIN_SRC sh
|
||||||
|
pyenv local 3.9.23
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
And have this in your =.envrc= file for use with [[file:ha-programming.org::*Virtual Environments with direnv][direnv]]:
|
And have this in your =.envrc= file for use with [[file:ha-programming.org::*Virtual Environments with direnv][direnv]]:
|
||||||
#+begin_src conf
|
#+begin_src conf
|
||||||
use python 3.7.1
|
use python 3.7.1
|
||||||
|
@ -257,6 +311,16 @@ Let’s expand our =major-mode-hydra= with some extras:
|
||||||
("D" elpy-doc "Docs Symbol")))))
|
("D" elpy-doc "Docs Symbol")))))
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
* Anaconda
|
||||||
|
The [[https://github.com/pythonic-emacs/anaconda-mode][anaconda-mode project]] seems as good as Elpy, but also include Evil keybindings.
|
||||||
|
|
||||||
|
#+BEGIN_SRC emacs-lisp :tangle no
|
||||||
|
(use-mode anaconda-mode
|
||||||
|
:hook ((python-mode . anaconda-mode)
|
||||||
|
(python-mode ../ anaconda-eldoc-mode)))
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Since we are using
|
||||||
* LSP Integration of Python
|
* LSP Integration of Python
|
||||||
** Dependencies
|
** Dependencies
|
||||||
Each Python project's =requirements-dev.txt= file would reference the [[https://pypi.org/project/python-lsp-server/][python-lsp-server]] (not the /unmaintained/ project, =python-language-server=):
|
Each Python project's =requirements-dev.txt= file would reference the [[https://pypi.org/project/python-lsp-server/][python-lsp-server]] (not the /unmaintained/ project, =python-language-server=):
|
||||||
|
|
37
zshell.org
37
zshell.org
|
@ -198,6 +198,19 @@ The [[https://github.com/zsh-users/zsh-syntax-highlighting][ZShell Syntax Highli
|
||||||
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
|
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
|
** Language Support
|
||||||
|
Anything special for particular languages.
|
||||||
|
*** Python
|
||||||
|
Not overly impressed, for to get =pyenv= to work, we need to add this code:
|
||||||
|
|
||||||
|
#+BEGIN_SRC zsh
|
||||||
|
export PYENV_ROOT="$HOME/.pyenv"
|
||||||
|
export PATH="$PYENV_ROOT/bin:$PATH"
|
||||||
|
|
||||||
|
eval "$(pyenv init --path)"
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
|
||||||
** Plugins
|
** Plugins
|
||||||
Configure the plugins, making sure to not use =git=, as the aliases are a pain to remember when I already have a superior Git interface in Emacs.
|
Configure the plugins, making sure to not use =git=, as the aliases are a pain to remember when I already have a superior Git interface in Emacs.
|
||||||
|
|
||||||
|
@ -209,12 +222,13 @@ Configure the plugins, making sure to not use =git=, as the aliases are a pain t
|
||||||
- =tab= :: To open a new terminal tab
|
- =tab= :: To open a new terminal tab
|
||||||
- =cdf= :: To open a directory in the Finder, meh. Why not change this to open it in =dired= in Emacs?
|
- =cdf= :: To open a directory in the Finder, meh. Why not change this to open it in =dired= in Emacs?
|
||||||
- =quick-look= :: To view a file
|
- =quick-look= :: To view a file
|
||||||
|
- [[https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/pyenv/README.md][pyenv]] :: Call the =pyenv init= and whatnot.
|
||||||
- [[https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/zbell][zbell]] :: To beep when a long running command has completed. Similar to my =beep= command.
|
- [[https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/zbell][zbell]] :: To beep when a long running command has completed. Similar to my =beep= command.
|
||||||
|
|
||||||
To have a plugin /install/, add its name to the =plugins= array variable /before/ we =source= the OMZ script:
|
To have a plugin /install/, add its name to the =plugins= array variable /before/ we =source= the OMZ script:
|
||||||
|
|
||||||
#+begin_SRC zsh
|
#+begin_SRC zsh
|
||||||
plugins=(colorize direnv gnu-utils iterm2 macos zbell zsh-syntax-highlighting)
|
plugins=(colorize direnv gnu-utils iterm2 macos pyenv virtualenv zbell zsh-syntax-highlighting)
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
Notice the =iterm2= plugin as well as the =macos= plugins that would be nice to figure out how to make them optionally added (although I believe they check themselves).
|
Notice the =iterm2= plugin as well as the =macos= plugins that would be nice to figure out how to make them optionally added (although I believe they check themselves).
|
||||||
|
@ -263,7 +277,7 @@ I keep the prompt simple since all of the /gunk/ we typically put in a prompt is
|
||||||
* iTerm2
|
* iTerm2
|
||||||
On Mac systems, I like the [[https://www.iterm2.com/][iTerm2 application]], and we can enable [[https://iterm2.com/documentation-shell-integration.html][shell integration]], either via the old school way, or just rely on [[https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/iterm2][the /plugin/ ]]above:
|
On Mac systems, I like the [[https://www.iterm2.com/][iTerm2 application]], and we can enable [[https://iterm2.com/documentation-shell-integration.html][shell integration]], either via the old school way, or just rely on [[https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/iterm2][the /plugin/ ]]above:
|
||||||
|
|
||||||
#+BEGIN_SRC zsh :tangle no
|
#+BEGIN_SRC zsh
|
||||||
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
|
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
|
@ -273,18 +287,31 @@ Also, while use the =title= command to change the Terminal’s title bar, don’
|
||||||
DISABLE_AUTO_TITLE="true"
|
DISABLE_AUTO_TITLE="true"
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
Favorite feature is the Status Bar at the bottom of the screen that shows the Git branch, current working directory, etc. This allows my prompt to be much shorter. What other information I want has changed over the years, but I define this information with this function:
|
Favorite feature is the [[https://iterm2.com/documentation-status-bar.html][Status Bar]] at the bottom of the screen that shows the Git branch, current working directory, etc. This allows my prompt to be much shorter. What other information I want has changed over the years, but I define this information with this function:
|
||||||
|
|
||||||
Currently, I show the currently defined Kube namespace.
|
Currently, I show the currently defined Kube namespace.
|
||||||
|
|
||||||
#+BEGIN_SRC zsh
|
#+BEGIN_SRC zsh
|
||||||
function iterm2_print_user_vars() {
|
function iterm2_print_user_vars() {
|
||||||
iterm2_set_user_var kubecontext $($ yq '.users[0].name' ~/.kube/config):$(kubectl config view --minify --output 'jsonpath={..namespace}')
|
# iterm2_set_user_var kubecontext $($ yq '.users[0].name' ~/.kube/config):$(kubectl config view --minify --output 'jsonpath={..namespace}')
|
||||||
|
|
||||||
# Correct version:
|
# Correct version:
|
||||||
# iterm2_set_user_var kubecontext $(kubectl config current-context):$(kubectl config view --minify --output 'jsonpath={..namespace}')
|
# iterm2_set_user_var kubecontext $(kubectl config current-context):$(kubectl config view --minify --output 'jsonpath={..namespace}')
|
||||||
# Faster version:
|
# Faster version:
|
||||||
# iterm2_set_user_var kubecontext $(awk '/^current-context:/{print $2;exit;}' <~/.kube/config)
|
iterm2_set_user_var kubecontext $(awk '/^current-context:/{print $2;exit;}' <~/.kube/config)
|
||||||
|
|
||||||
|
iterm2_set_user_var pycontext "$(pyenv version-name):$(echo $VIRTUAL_ENV | sed 's/.*.venv\///')"
|
||||||
|
}
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Add the following:
|
||||||
|
|
||||||
|
#+BEGIN_SRC zsh
|
||||||
|
function pycontext {
|
||||||
|
local version venvstr
|
||||||
|
version=$(pyenv version-name)
|
||||||
|
venvstr=$(echo $VIRTUAL_ENV | sed 's/.*.venv\///')
|
||||||
|
echo "🐍 $version:$venvstr"
|
||||||
}
|
}
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue