Skip to content

Commit

Permalink
python: do not add main brewed Python site-package to virtualenv
Browse files Browse the repository at this point in the history
If added, this makes the virtualenv read the main site-package from brewed Python,
and especially makes it read our sitecustomize.py file, which will
modify the sys.executable path.

See the full discussion at:
#8873

I also took the opportunity to not include test deps, as these will
be not be installed, so the .pth file should not contains references
to site-packages from test deps.

Previous packages on Linux did already contain the wrong lines in the pth file,
for example:
cat /home/linuxbrew/.linuxbrew/Cellar/aws-google-auth/0.0.36_1/libexec/lib/python3.8/site-packages/homebrew_deps.pth
import site; site.addsitedir('/home/linuxbrew/.linuxbrew/opt/[email protected]/lib/python3.8/site-packages')
import site; site.addsitedir('/home/linuxbrew/.linuxbrew/opt/libxml2/lib/python3.8/site-packages')

This might have caused subtle bugs for some packages but not for others.
  • Loading branch information
iMichka committed Oct 8, 2020
1 parent 0732a86 commit ddc73ff
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Library/Homebrew/language/python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ def virtualenv_create(venv_root, python = "python", formula = self)
# Find any Python bindings provided by recursive dependencies
formula_deps = formula.recursive_dependencies
pth_contents = formula_deps.map do |d|
next if d.build?
next if d.build? || d.test?
# Do not add the main site-package provided by the brewed
# Python formula, to keep the virtual-env's site-package pristine
next if python_names.include? d.name

dep_site_packages = Formula[d.name].opt_prefix/Language::Python.site_packages(python)
next unless dep_site_packages.exist?
Expand Down Expand Up @@ -196,8 +199,7 @@ def needs_python?(python)
def virtualenv_install_with_resources(options = {})
python = options[:using]
if python.nil?
pythons = %w[python python3 pypy pypy3] + Formula.names.select { |name| name.start_with? "python@" }
wanted = pythons.select { |py| needs_python?(py) }
wanted = python_names.select { |py| needs_python?(py) }
raise FormulaUnknownPythonError, self if wanted.empty?
raise FormulaAmbiguousPythonError, self if wanted.size > 1

Expand All @@ -210,6 +212,10 @@ def virtualenv_install_with_resources(options = {})
venv
end

def python_names
%w[python python3 pypy pypy3] + Formula.names.select { |name| name.start_with? "python@" }
end

# Convenience wrapper for creating and installing packages into Python
# virtualenvs.
class Virtualenv
Expand Down

0 comments on commit ddc73ff

Please sign in to comment.