3

Since asking a question about Pip vs Package Manager for handling Python Packages I've learned a lot, especially about virtual environments with the venv module. These days I almost exclusively install packages with pip inside virtual environments. I almost rarely install a system-wide python package.

One thing I'm always terrified of, though, is the possibility that I'll run a pip install command without realizing I don't have a venv activated.

Is there a way to blacklist pip only when I'm not in a venv?

  • 1
    There is little chance you'll mess up things anyway (unless you work as root). Outside of venv, pip install will require root privileges so you are forced to use --user flag and inside the venv, pip install --user will fail because the user site is not accessible. – hoefling Jan 4 at 0:04
  • @hoefling I feel like a moron for not realizing that. – malan Jan 4 at 21:21
  • 1
    You shouldn't, it's a good question. You can even uninstall the system pip if your distro supports that, I don't have it installed on my box. – hoefling Jan 4 at 22:46
2

You could possibly use a shell alias to take over pip when outside the venv.

alias 'pip=cowsay "You are not in a virtual environment"'

And inside the virtualenv startup file (e.g. the one that gets sourced when you enter the venv):

unalias 'pip'

  • Or in a different PATH...but yeah, it is one way of dealing with it. – Rui F Ribeiro Jan 2 at 16:00
  • I've never opened the activate script before. Now that I have, that's a pretty snazzy idea. – malan Jan 2 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.