19
votes

Running Python 2.7.3, installed with HomeBrew, on a mac.

Installed several packages using PIP, including virtualenv. (Using virtualenv as an example, but NONE of the packages work.)

When I try to run them in terminal, it fails as follows:

$ virtualenv venv --distribute
-bash: virtualenv: command not found

Alternatively:

$ python virtualenv.py venv
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'virtualenv.py': [Errno 2] No such file or directory

A few other points that may help:

$ which python
/usr/local/bin/python
$ pip freeze
MySQL-python==1.2.4
...
virtualenv==1.8.4
$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
$ echo $PYTHONPATH
/usr/local/lib/python2.7/site-packages:

By default, the $PYTHONPATH was blank, I changed it in .bash_profile (didn't help). VirtualEnv does exist at that path. I also tried adding this path to the .profile $path, but that didn't help either, so I removed it.

On the HomeBrew Python page it seems to somewhat relate to this, but I am new to Python, and can't figure it out. Have spent some hours DuckDuckGo'ing with nothing gained.

Any help would be greatly appreciated.

EDIT: Updated to reflect actual usage.

3
If $ virtualenv venv does not work, try $ python -m virtualenv venv that does not count on your $PATH to find virtualenv binary. - Hugo Tavares
@HugoTavares I'm getting: /usr/bin/python: No module named virtualenv - RicardoGonzales
@RicardoGonzales: that means you don't have virtualenv installed on that specific python. You can install virtulaenv with pip or from source; check the documentation: virtualenv.pypa.io/en/latest/installation.html - Hugo Tavares
@HugoTavares I've installed virtualven with pip. Please if you have the time see my question. stackoverflow.com/questions/31707557/… - RicardoGonzales

3 Answers

34
votes

The problem was that I had not added Python to the system $PATH.

At the end of the brew install it says (viewable by typing brew info python):

Executable python scripts will be put in:  
   /usr/local/share/python
so you may want to put "/usr/local/share/python" in your PATH, too.

So, simply had to open .profile and paste it in, and all packages work.

Much thanks to MistyM on the Brew IRC channel for pointing that out!

0
votes

Download virtualenv.py if your system does not provide virtualenv command:

curl -L -o virtualenv.py https://raw.github.com/pypa/virtualenv/master/virtualenv.py

First create your virtualenv folder:

 python virtualenv.py venv # venv <-- name of the folder

You need run virtualenv's activate in shell:

 . venv/bin/activate

or

 source venv/bin/activate

This fixes PYTHONPATH and PATH. You do this once per each shell session. Then python command will magically work :)

Now run pip, packages will be installed in venv.

More info (disclaimer, I am the author) http://opensourcehacker.com/2012/09/16/recommended-way-for-sudo-free-installation-of-python-software-with-virtualenv/

-1
votes

Quick work flow on creating a Virtual env

$ mkdir awesomeapp 
$cd awesomeapp
$virtualenv venv --distribute
New python executable in venv/bin/python
Installing distribute.........done.
Installing pip................done.
$source venv/bin/activate
(venv)$python

One you CD into your directory that's when you're creating your virtual venv folder to store your path.

You'll now it's active when you see the (venv)