6

I installed brew and then

brew install python
brew install python3

Install flask with pip

pip install flask

Try

python -c 'import flask'

But I got exception

ImportError: No module named flask

Set and export PYTHONPATH

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

So far so good. But when I tried to install a package with pip3 for Python 3, I got error message

Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!
     PYTHONPATH is currently: ":/usr/local/lib/python2.7/site-packages"
     You should `unset PYTHONPATH` to fix this.

How to use site-packages without the PYTHONPATH trick to make both pip and pip3 usable and both Python 2 and Python 3 able to import site-packages?

1
  • I'm not really familiar with brew but if you type python (anything) at the system prompt I would expect that to run the built-in system Python - is that the same Python installation into which you've installed the flask module?
    – nekomatic
    Commented Dec 10, 2014 at 14:02

2 Answers 2

5

You should consider install pyenv for this.

pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

To install, just type:

$ brew install pyenv

Add this to your .bash_profile:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

Once installed use pyenv to install python versions you need:

$ pyenv install -l/--list --> List all available versions
$ pyenv install 2.7.6
$ pyenv install 3.3.3

Run this command after you install a python version:

$ pyenv rehash

You can specify multiple versions via PYENV_VERSION at once. Let's say if you have two versions of 2.7.6 and 3.3.3. If you prefer 2.7.6 over 3.3.3,

$ pyenv shell 2.7.6 3.3.3
$ pyenv versions
  system
* 2.7.6 (set by PYENV_VERSION environment variable)
* 3.3.3 (set by PYENV_VERSION environment variable)
$ python --version
Python 2.7.6
$ python2.7 --version
Python 2.7.6
$ python3.3 --version
Python 3.3.3
4
  • I installed pyenv. Could you please give some examples of how to use it to solve my problem? Commented Dec 12, 2014 at 1:23
  • Update my answer.
    – jherran
    Commented Dec 12, 2014 at 8:18
  • 1
    Better use virtualenv which is now part of python
    – mmmmmm
    Commented Jul 11, 2015 at 15:30
  • @Mark virtualenv only creates isolated virtual environments based on existing Python interpreter installations. pyenv installs (and manages) new Python interpreters and environments, and can additionally wrap virtualenv or venv to manage virtual environments in a consistent manner. pyenv has no dependency on Python itself, and can be used to bootstrap a Python environment on a “clean” machine.
    – wjv
    Commented Jan 10, 2017 at 9:06
4

To use both Python 2 and Python 3 on OS X, one should avoid using the Mac pre-installed python.

  1. Install python with homebrew:

    brew install python
    brew install python3
    
  2. Edit /etc/paths:

    /usr/local/bin
    /usr/bin
    /bin
    /usr/local/sbin
    /usr/sbin
    /sbin
    

Make sure /usr/local/bin comes before /usr/bin. The former is use by homebrew and the latter is where the Mac pre-installed python executable lives. Show your PATH for a double check:

    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/X11/bin

Now I can install packages with pip2 and pip3 for 2 versions of Python separately.

2
  • My /etc/paths looks like you describe. Your answer did not work for me.
    – n1000
    Commented Sep 22, 2015 at 8:27
  • @n1000 echo $PATH; echo $PYTHONPATH. What do you get? Commented Sep 22, 2015 at 9:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .