diff options
| author | Mike McQuaid | 2014-01-20 13:50:25 -0800 |
|---|---|---|
| committer | Mike McQuaid | 2014-01-20 15:43:00 -0800 |
| commit | e7d29e629c3d17fdb7be9614daade79cb37ee727 (patch) | |
| tree | cab2ce8ef987c2580b54883ebe32ce18c81c7e66 /Library/Formula/python3.rb | |
| parent | 8768a664afbf6c18713fc61e4c96e0c5467b5369 (diff) | |
| download | homebrew-e7d29e629c3d17fdb7be9614daade79cb37ee727.tar.bz2 | |
python3: write sitecustomize.py in formula.
Diffstat (limited to 'Library/Formula/python3.rb')
| -rw-r--r-- | Library/Formula/python3.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Library/Formula/python3.rb b/Library/Formula/python3.rb index 1cc2ac045..5f7419c96 100644 --- a/Library/Formula/python3.rb +++ b/Library/Formula/python3.rb @@ -130,6 +130,10 @@ class Python3 < Formula # Symlink the prefix site-packages into the cellar. ln_s site_packages, site_packages_cellar + # Write our sitecustomize.py + Dir["#{site_packages}/*.py{,c,o}"].each {|f| Pathname.new(f).unlink } + (site_packages/"sitecustomize.py").write(sitecustomize) + # "python3" executable is forgotten for framework builds. # Make sure homebrew symlinks it to HOMEBREW_PREFIX/bin. ln_s "#{bin}/python#{VER}", "#{bin}/python3" unless (bin/"python3").exist? @@ -217,6 +221,56 @@ class Python3 < Formula end end + def sitecustomize + <<-EOF.undent + # This file is created by Homebrew and is executed on each python startup. + # Don't print from here, or else python command line scripts may fail! + # <https://github.com/Homebrew/homebrew/wiki/Homebrew-and-Python> + import os + import sys + + if sys.version_info[0] != 3: + # This can only happen if the user has set the PYTHONPATH for 3.x and run Python 2.x or vice versa. + # Every Python looks at the PYTHONPATH variable and we can't fix it here in sitecustomize.py, + # because the PYTHONPATH is evaluated after the sitecustomize.py. Many modules (e.g. PyQt4) are + # built only for a specific version of Python and will fail with cryptic error messages. + # In the end this means: Don't set the PYTHONPATH permanently if you use different Python versions. + exit('Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python ' + + str(sys.version_info[0]) + '.x!\\n PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\\n' + + ' You should `unset PYTHONPATH` to fix this.') + else: + # Only do this for a brewed python: + opt_executable = '#{HOMEBREW_PREFIX}/opt/python3/bin/python#{VER}' + if os.path.realpath(sys.executable) == os.path.realpath(opt_executable): + # 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 + if (not p.startswith('/System') and + not p.startswith('#{HOMEBREW_PREFIX}/lib/python') and + not (p.startswith('#{HOMEBREW_PREFIX}/Cellar/python') and p.endswith('site-packages'))) ] + + # LINKFORSHARED (and python-config --ldflags) return the + # 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 + # -F/#{HOMEBREW_PREFIX}/Frameworks switch. + # Assume Framework style build (default since months in brew) + try: + from _sysconfigdata import build_time_vars + build_time_vars['LINKFORSHARED'] = '-u _PyMac_Error #{HOMEBREW_PREFIX}/opt/python3/Frameworks/Python.framework/Versions/#{VER}/Python' + except: + pass # remember: don't print here. Better to fail silently. + + # Set the sys.executable to use the opt_prefix + sys.executable = opt_executable + + # Tell about homebrew's site-packages location. + # This is needed for Python to parse *.pth. + import site + site.addsitedir('#{HOMEBREW_PREFIX}/lib/python#{VER}/site-packages') + EOF + end + def caveats text = <<-EOS.undent Setuptools and Pip have been installed. To update them |
