Python Environment with Pipx and Pipenv

Published on 28 May 2022

I first exposed to python development environment with Conda and then I switched to the lightweight Miniconda. I was happy with this solution for a long while. Then I was not. I realized that issuing pip within a conda environment does not isolate the package to that conda environment (kinda defeats the purpose here), and this was when I started having failure of (?) package resolution when trying to install packages through conda.

Then I thought, maybe I should try something else. Previously, I was happy with simple venv module (python3 built-in), and I still use it from time-to-time for small projects. However, I intended to find a user-wide replacement like how conda previously served me. Inspired by a write-up on python project setup by Bas Steins, I decided to give pipenv a shot.

python3 -m pip install pipx
pipx install pipenv

I decided to use pypa/pipx to ensure isolation of pipenv from other python system packages.

# Create a directory for the pipenv environment
mkdir -p ~/.local/share/<venv-name>
cd ~/.local/share/<venv-name>

# Start installing python packages
pipenv install pandas

Usually I have these packages installed: ipykernel. openpyxl, flake8, matplotlib, ipython, xlrd, seaborn, sklearn, statsmodels, scipy, odfpy, streamlit, jupyterlab. Issuing pipenv --venv reveals the location of the environment, usually defaults to /home/<user>/.local/share/virtualenvs/<venv-name> (on a linux system). To enable automatic venv activation on the shell, I added the following line to ~/.zshrc:

source /home/<user>/.local/share/virtualenvs/<venv-name>/bin/activate

To deactivate, simply remove that line above from ~/.zshrc or issue deactivate at terminal. Issuing pipenv --rm while being inside ~/.local/share/<venv-name> removes the virtual environment.

The nice thing about this setup is that VS Code recognizes the location of the virtualenvs.