Using virtualenv on Mac OS X

22,460

Solution 1

It turns out that my problems with virtualenv were my own fault: I had configured my .bash_profile to muck with the PYTHONPATH environment variable, which caused the import problems.

Thank you to everyone who took the time to answer; sorry for not investigating the problem further on my own.

Solution 2

I've not had any problems with the same OS X/Python/virtualenv version (OS X 10.5.6, Python 2.5.1, virtualenv 1.3.1)

$ virtualenv test
New python executable in test/bin/python
Installing setuptools............done.
$ source test/bin/activate
(test)$ which python
/Users/dbr/test/bin/python
$ echo $PATH
/Users/dbr/test/bin:/usr/bin:[...]
$ python
[...]
>>> import sys
>>> print sys.path
['', '/Users/dbr/test/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg',

One thing to check - in a clean shell, run the following:

$ virtualenv test
$ python
[...]
>>> import sys
>>> sys.path
['', '/Library/Python/2.5/site-packages/elementtree-1.2.7_20070827_preview-py2.5.egg'[...]
>>> sys.path.append("test/bin/")
>>> import activate_this
>>> sys.path
['/Users/dbr/test/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg'

Or from the virtualenv docs:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

That should force the current Python shell into the virtualenv

Also, after running source test/bin/activate try running python with the -v flag (verbose), it may produce something useful.

Share:
22,460
Joseph
Author by

Joseph

I used to be a computer science student at Rolla, but I escaped mostly unharmed.

Updated on May 22, 2020

Comments

  • Joseph
    Joseph almost 4 years

    I've been using virtualenv on Ubuntu and it rocks, so I'm trying to use it on my Mac and I'm having trouble.

    The virtualenv command successfully creates the directory, and easy_install gladly installs packages in it, but I can't import anything I install. It seems like sys.path isn't being set correctly: it doesn't include the virtual site-packages, even if I use the --no-site-packages option. Am I doing something wrong?

    I'm using Python 2.5.1 and virtualenv 1.3.3 on Mac OS 10.5.6

    Edit: Here's what happens when I try to use virtualenv:

    $ virtualenv test
    New python executable in test/bin/python
    Installing setuptools............done.
    $ source test/bin/activate
    (test)$ which python
    /Users/Justin/test/bin/python
    (test)$ which easy_install
    /Users/Justin/test/bin/easy_install
    (test)$ easy_install webcolors
    [...]
    Installed /Users/Justin/test/lib/python2.5/site-packages/webcolors-1.3-py2.5.egg
    Processing dependencies for webcolors
    Finished processing dependencies for webcolors
    (test)$ python
    [...]
    >>> import webcolors
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named webcolors
    >>> import sys
    >>> print sys.path
    ['',
     '/Library/Python/2.5/site-packages/SQLObject-0.10.2-py2.5.egg',
     '/Library/Python/2.5/site-packages/FormEncode-1.0.1-py2.5.egg',
     ...,
     '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5',
     '/Users/Justin/test/lib/python25.zip',
     '/Users/Justin/test/lib/python2.5',
     '/Users/Justin/test/lib/python2.5/plat-darwin',
     '/Users/Justin/test/lib/python2.5/plat-mac',
     '/Users/Justin/test/lib/python2.5/plat-mac/lib-scriptpackages',
     '/Users/Justin/test/Extras/lib/python',
     '/Users/Justin/test/lib/python2.5/lib-tk',
     '/Users/Justin/test/lib/python2.5/lib-dynload',
     '/Library/Python/2.5/site-packages',
     '/Library/Python/2.5/site-packages/PIL']
    

    Edit 2: Using the activate_this.py script works, but running source bin/activate does not. Hopefully that helps narrow down the problem?

  • Joseph
    Joseph almost 15 years
    The import activate_this technique worked perfectly; I guess it manipulates sys.path differently than the activate script?