Python: Exit if PYTHONPATH is wrong
In our sitecustomize.py we instruct python to exit with an error message
if the PYTHONPATH is pointing to a wrong homebrew site-packages dir.
With wrong meaning another major python version.
If you set the PYTHONPATH to include
$(brew --prefix)/lib/python2.7/site-packages
and start python3, it may pick up modules from there, wich can result
in errors for non pure python modules (such as PyQt).
This commit is contained in:
parent
c968f8302f
commit
a73012def4
@ -288,30 +288,32 @@ class PythonInstalled < Requirement
|
|||||||
def sitecustomize
|
def sitecustomize
|
||||||
<<-EOF.undent
|
<<-EOF.undent
|
||||||
# This file is created by Homebrew and is executed on each python startup.
|
# This file is created by Homebrew and is executed on each python startup.
|
||||||
# Don't print from here, or else universe will collapse.
|
# Don't print from here, or else python command line scripts may fail!
|
||||||
|
# <https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python>
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.version_info[0] == #{version.major} and sys.version_info[1] == #{version.minor}:
|
if sys.version_info[0] != #{version.major}:
|
||||||
if sys.executable.startswith('#{HOMEBREW_PREFIX}/opt/python'):
|
import os
|
||||||
# Fix 1)
|
# This can only happen if the user has set the PYTHONPATH for 3.x and run Python 2.x or vice versa.
|
||||||
# A setuptools.pth and/or easy-install.pth sitting either in
|
# Every Python looks at the PYTHONPATH variable and we can't fix it here in sitecustomize.py,
|
||||||
# /Library/Python/2.7/site-packages or in
|
# because the PYTHONPATH is evaluated after the sitecustomize.py. Many modules (e.g. PyQt4) are
|
||||||
# ~/Library/Python/2.7/site-packages can inject the
|
# built only for a specific version of Python and will fail with cryptic error messages.
|
||||||
# /System's Python site-packages. People then report
|
# In the end this means: Don't set the PYTHONPATH permanently if you use different Python versions.
|
||||||
# "OSError: [Errno 13] Permission denied" because pip/easy_install
|
exit('Your PYTHONPATH points to a site-packages dir for Python #{version.major}.x but you are running Python ' +
|
||||||
# attempts to install into
|
str(sys.version_info[0]) + '.x!\\n PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\\n' +
|
||||||
# /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
|
' You should `unset PYTHONPATH` to fix this.')
|
||||||
# See: https://github.com/mxcl/homebrew/issues/14712
|
else:
|
||||||
# Fix 2)
|
# Only do this for a brewed python:
|
||||||
# Remove brewed Python's hard-coded Cellar-site-packages
|
if sys.executable.startswith('#{HOMEBREW_PREFIX}'):
|
||||||
|
# Remove /System site-packages, and the Cellar site-packages
|
||||||
|
# which we moved to lib/pythonX.Y/site-packages. Further, remove
|
||||||
|
# HOMEBREW_PREFIX/lib/python because we later addsitedir(...).
|
||||||
sys.path = [ p for p in sys.path
|
sys.path = [ p for p in sys.path
|
||||||
if not (p.startswith('/System') or
|
if (not p.startswith('/System') and
|
||||||
p.startswith('#{HOMEBREW_PREFIX}/Cellar/python') and p.endswith('site-packages')) ]
|
not p.startswith('#{HOMEBREW_PREFIX}/lib/python') and
|
||||||
# Fix 3)
|
not (p.startswith('#{HOMEBREW_PREFIX}/Cellar/python') and p.endswith('site-packages'))) ]
|
||||||
# Set the sys.executable to use the opt_prefix
|
|
||||||
sys.executable = '#{HOMEBREW_PREFIX}/opt/#{name}/bin/python#{version.major}.#{version.minor}'
|
# LINKFORSHARED (and python-config --ldflags) return the
|
||||||
# Fix 4)
|
|
||||||
# Make LINKFORSHARED (and python-config --ldflags) return the
|
|
||||||
# full path to the lib (yes, "Python" is actually the lib, not a
|
# full path to the lib (yes, "Python" is actually the lib, not a
|
||||||
# dir) so that third-party software does not need to add the
|
# dir) so that third-party software does not need to add the
|
||||||
# -F/#{HOMEBREW_PREFIX}/Frameworks switch.
|
# -F/#{HOMEBREW_PREFIX}/Frameworks switch.
|
||||||
@ -321,11 +323,14 @@ class PythonInstalled < Requirement
|
|||||||
build_time_vars['LINKFORSHARED'] = '-u _PyMac_Error #{HOMEBREW_PREFIX}/opt/#{name}/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}/Python'
|
build_time_vars['LINKFORSHARED'] = '-u _PyMac_Error #{HOMEBREW_PREFIX}/opt/#{name}/Frameworks/Python.framework/Versions/#{version.major}.#{version.minor}/Python'
|
||||||
except:
|
except:
|
||||||
pass # remember: don't print here. Better to fail silently.
|
pass # remember: don't print here. Better to fail silently.
|
||||||
# Fix 5)
|
|
||||||
# For all Pythons of the right major.minor version: Tell about homebrew's
|
# Set the sys.executable to use the opt_prefix
|
||||||
# site-packages location. This is needed for Python to parse *.pth.
|
sys.executable = '#{HOMEBREW_PREFIX}/opt/#{name}/bin/#{xy}'
|
||||||
|
|
||||||
|
# Tell about homebrew's site-packages location.
|
||||||
|
# This is needed for Python to parse *.pth.
|
||||||
import site
|
import site
|
||||||
site.addsitedir('#{global_site_packages}')
|
site.addsitedir('#{HOMEBREW_PREFIX}/lib/#{xy}/site-packages')
|
||||||
EOF
|
EOF
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user