Alex
Alex

Reputation: 12923

python alias in ~/.zshrc overrides virtual environment source

I've got a couple aliases setup in my terminal session initialization shell script ~/.zshrc

alias python=/opt/homebrew/bin/python3.9
alias pip=/opt/homebrew/bin/pip3.9

These are interfering with my virtual environment workflow:

>>> cd my_project
>>> python -m venv venv
>>> source venv/bin/activate
>>> which python
python: aliased to /opt/homebrew/bin/python3.9

You can see how the virtual environment was not activated. Any suggestions on how I can work with venv while having these aliases setup in my ~/.zshrc?

Upvotes: 3

Views: 4580

Answers (3)

5top1t
5top1t

Reputation: 1

I noticed that on my machine python3 and pip3 commands always point to the right directory when venv is activated or deactivated. If this is the case on your machine use it to alias.

alias python='eval $(which python3)'
alias pip='eval $(which pip3)'

Updates the python and pip aliases when using virtual environments.

Upvotes: 0

Harkaran Brar
Harkaran Brar

Reputation: 121

You can change your alias:

python = "python3.9"

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 141583

Create a directory ~/bin. In that directory create two links:

ln -vs /opt/homebrew/bin/python3.9 ~/bin/python
ln -vs /opt/homebrew/bin/pip3.9 ~/bin/pip

Edit your .zshrs to include:

export PATH=~/bin:$PATH

The links will hide the normal executables. When virtualenv will be activated, the links will be hiden by the PATH set up by virtualenv.

Upvotes: 6

Related Questions