Apply local aliases and exports in pipenv/Python

This article explains a method to automatically load a configuration file (like .zshrc) containing aliases and exports in the current directory when zsh (bash) is loaded. The Python virtual environment pipenv loads a new shell, which allows aliases and other settings to be automatically applied. When exiting pipenv, the virtual environment’s settings become invalid, so they do not affect your base shell or other environments. This can be implemented easily (and effectively), but probably because pipenv isn’t that popular, I could not find exact information easily. Hope you love it.

Environment

Shell: zsh (bash seems to work as well.)

Python virtual environment: pipenv

OS: macOS Sequoia (should not matter)

Steps

To use pipenv on macOS, first install it with brew install pipenv. The steps to easily create a virtual environment are like this:

mkdir my_project # Create a project directory.
cd my_project # Go in to the directory.
pipenv --python 3.11 # Create a virtual environment with Python 3.11
pipenv shell # Enter the virtual env. To exit, enter "exit" or press ctrl + D

Add a line to ~/.zshrc.

Add the following to the .zshrc in your home directory.

# Load .zshrc.local if it exists in the current directory.
[[ -f .zshrc.local ]] && source .zshrc.local

This checks whether the .zshrc.local file exists in the current directory as a conditional expression on the left side of &&. If it is true, the source command on the right is executed, loading the .zshrc.local file (I haven’t tried it, but apparently the same method works in bash in terms of syntax).

Place .zshrc.local in the root of pipenv directory.

Of course, anything you can write in .zshrc such as alias or export are allowed to be added. Here is a simple sample for reference:

alias t='time'
export HW="Hello, World!"

Enter the virtual environment and test.

Example of commands and outputs:

$ pipenv shell # Enter the virtual env.
$ t # alias of the time command
(Output of the time command appears here.)

$ echo $HW
Hello, World!

Exit from the virtual environment and test.

Example of commands and outputs:

$ exit # or ctrl + D to exit.
$ t
zsh: command not found: t

$echo $HW

(An empty line appears here.)

Note

If projects to be published on GitHub, etc., be sure to add .zshrc.local to .gitignore.

Similar method for venv

The most popular virtual environment tool venv does not load a new shell. Therefore, similar functionality needs to be implemented through alternative means.

Add the below to the bottom of bin/activate in your venv directory. That’s the same thing as added to the ~/.zshrc in the above.

# Load .zshrc.local if it exists in the current directory.
[[ -f .zshrc.local ]] && source .zshrc.local

It involves one more step than using pipenv, but this allows you to do roughly the same thing.

Difference with venv

This method for venv won’t load a new shell but .zshrc.local, so applied aliases and exports are still valid even after exiting by deactivate. Maybe you better close terminal session to avoid potential conflicts.

Why I needed this

Recently, I run mlx-lm.server to use the MLX version of LLMs. Unlike Ollama, it often happens that the memory is not released (the memory pressure remains high). So, I have no choice but to stop it with Ctrl + C each time and restart the server from the CLI. However, I felt stressed because I couldn’t immediately re-execute it by a single push of the up arrow key when I was entering commands in another terminal window. That’s why I thought about creating an alias that is only valid in the pipenv environment.

I couldn’t find the solution as easily as I thought on the web, but after consulting with local QwQ, Qwen3, and ChatGPT, I eventually figured it out by myself. When I asked each LLM for their evaluation, they praised me with “Great!” and made me happy. It was the motivation of this blog post. haha!

Image by Stable Diffusion (Mochi Diffusion)

I couldn’t imagine what kind of image would go well with this post, so I just got a showroom with various bicycles generated. Still not sure if the image is a good match.

Date:
2025-6-14 19:47:15

Model:
realisticVision-v51VAE_original_768x512_cn

Size:
768 x 512

Include in Image:
showroom with different types of bycicles

Exclude from Image:

Seed:
1251791658

Steps:
20

Guidance Scale:
20.0

Scheduler:
DPM-Solver++

ML Compute Unit:
CPU & GPU

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© Peddals.com