5

I've been trying to get up and running with the built-in "venv" module of Python 3.3 on my OS X machine. I've installed Python 3.3 using Homebrew.

As per the docs, creating and switching virtual environment works as you'd expect:

$ python3 -m venv myvenv
$ source myvenv/bin/activate

And I've tested something like this:

$ echo "YEAH = 'YEAH!'" > myvenv/lib/python3.3/site-packages/thingy.py
$ python
>>> import thingy
>>> print(thingy.YEAH)
'YEAH!'

But when I try to install distribute, it simply won't go in the proper place. For some reason, it insists on trying to install into /usr/local/lib/python3.3/site-packages/, which fails with the following messages:

No setuptools distribution found
running install
Checking .pth file support in /usr/local/lib/python3.3/site-packages/
/Users/victor/myvenv/bin/python -E -c pass
TEST FAILED: /usr/local/lib/python3.3/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH

You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /usr/local/lib/python3.3/site-packages/

and your PYTHONPATH environment variable currently contains:

    ''

This happens regardless if I try to install using distribute_setup.py or using the source distribution directly. I've even tried using --prefix=/Users/victor/myenv but it still tries to put everything in my "global" site-packages.

I can't figure out why this happens, but it's consistent on two of my machines. Note that sys.prefix reports the correct path (the virtual environment).

Is this a problem with Homebrew? OS X? Python 3.3? venv? Me?

vicvicvic
  • 6,025
  • 4
  • 38
  • 55
  • 2
    Installing distribute 0.6.33 into a venv works just fine for me with a python.org 3.3.0 from the python.org 64-/32-bit installer on 10.8. – Ned Deily Dec 31 '12 at 04:42
  • 1
    I had completely stopped using python.org for the convenience of Homebrew. But their installer also works for me. So, it's probably an issue with Homebrew's way of installing Python. – vicvicvic Dec 31 '12 at 04:54
  • 1
    FWIW, it also works fine with the MacPorts python33 port. So, yes, most likely some flaw in a Homebrew recipe. – Ned Deily Dec 31 '12 at 05:31
  • Homebrew installs distribute and pip into `/usr/local/lib/python3.3/site-packages/` already. Is that a problem? – Samuel John Apr 01 '13 at 14:19
  • you might need to set the PYTHONPATH to `/usr/local/lib/python3.3/site-packages/` perhaps. – Samuel John Apr 01 '13 at 14:20

2 Answers2

3

This has been an issue with Homebrew, yes, but it is working now since https://github.com/mxcl/homebrew/commit/0b50110107ea2998e65011ec31ce45931b446dab.

$ brew update
$ brew rm python3  #if you have installed it before
$ brew install python3
$ cd /tmp
$ which python3
  /usr/local/bin/python3
$ python3 -m venv myvenv 
$ source myvenv/bin/activate
$ wget http://python-distribute.org/distribute_setup.py  # may need brew install wget
$ python3 distribute_setup.py  
  ...
  Finished processing dependencies for distribute==0.6.45
  After install bootstrap.
  Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info
  Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools.pth

You see that distribute install successfully into the /tmp dir.

Samuel John
  • 1,362
  • 10
  • 12
1

This happens because homebrew installs distutils config file:

$ brew cat python3  | grep "Tell distutils" -A5
    # Tell distutils-based installers where to put scripts
    (prefix/"Frameworks/Python.framework/Versions/#{VER}/lib/python#{VER}/distutils/distutils.cfg").write <<-EOF.undent
      [install]
      install-scripts=#{scripts_folder}
      install-lib=#{site_packages}
    EOF

$ mv ~/.local/Frameworks/Python.framework/Versions/3.3/lib/python3.3/distutils/distutils.cfg ~/tmp/
$ cat ~/tmp/distutils.cfg 
[install]
install-scripts=/Users/gatto/.local/share/python3
install-lib=/Users/gatto/.local/lib/python3.3/site-packages
$ . venv/bin/activate
(venv) $ python distribute-0.6.36/distribute_setup.py
(venv) $ ls venv/lib/python3.3/site-packages/
distribute-0.6.36-py3.3.egg  easy-install.pth  setuptools-0.6c11-py3.3.egg-info  setuptools.pth 

See "distutils.cfg Can Break venv" issue at bugs.python.org.

gatto
  • 2,947
  • 1
  • 21
  • 24
  • 1
    Homebrew changed this distutils.cfg to a more standard conform way and now only specifies prefix. https://github.com/mxcl/homebrew/commit/0b50110107ea2998e65011ec31ce45931b446dab – Samuel John Jun 05 '13 at 13:48