13

Here's my problem. I can't install MatPlotLib both via pip and from source (Matplotlib installation on Mavericks). I tried brew install matplotlib and the installation successfully ended. However, it installed MatPlotLib globally and not inside the currently activated VirtualEnv.

Is it possible to tell brew to install a package inside the current VirtualEnv?

Community
  • 1
  • 1
se7entyse7en
  • 4,310
  • 7
  • 33
  • 50

2 Answers2

19

I help maintain the homebrew-python tap. This is not and will not be supported and there probably isn't an easy way to do it.

This is due to the fact Homebrew installs packages to Homebrew's prefix, which is necessarily global. Homebrew doesn't know about virtualenvs or how to install packages into them. Changing that would violate a core assumption of Homebrew's design, i.e. that packages are always installed to their private Homebrew prefix.

Sorry!

NirIzr
  • 3,131
  • 2
  • 30
  • 49
Tim Smith
  • 6,127
  • 1
  • 26
  • 32
  • Could you explain why? – Jorge Leitao Jan 26 '17 at 14:40
  • Homebrew installs packages to Homebrew's prefix, which is necessarily global. Homebrew doesn't know about virtualenvs or how to install packages into them. Changing that would violate a core assumption of Homebrew's design, i.e. that packages are always installed to their private Homebrew prefix. – Tim Smith Jan 27 '17 at 16:44
6

All that a virtual environment does is change what Python interpreter will be invoked by modifying the PATH variable. Homebrew doesn't look at that because it doesn't use Python to install packages (like pip does). By design, as Tim said, Homebrew installs inside its own prefix every time.

One could conceive of a modified Python virtual environment that also installs its own copy of Homebrew, but that would be almost entirely a bad thing.

What you can do, if you want Homebrew packages in a virtual environment, is to enable system site packages in the venv (in Python3, this would be python3 -m venv --system-site-packages ~/path/to/venv). This can be done to update existing virtual environments, as well.

Enabling system site packages allows Python to use globally installed packages, but it will still search inside the venv first, and you can override global packages by installing something with pip. As far as I can tell, this is as close as you'll get to The Right Way in that situation.

krs013
  • 2,799
  • 1
  • 17
  • 17
  • How do you update existing virtual environments with this? – muninn Aug 03 '18 at 17:00
  • 1
    I believe you can run the command on an existing venv and it will just overwrite the relevant files, effectively updating it. I've done this successfully a few times, though I can't remember if it's 100% successful. – krs013 Oct 22 '19 at 23:15