0

For work-related reasons, I'm trying to install Python 2.7.8 directly onto my machine (Mac OSX 10.9).

I am current running Python 2.7.6 in Enthought Canopy, and I really don't want to touch the existing libraries there.

My problem is that I'd like to use pip to install packages for the new instantiation of Python, but currently pip is bundled up with Enthought Canopy, so it only installs packages in the site-packages path for Enthought Canopy.

  1. I first tried the following:

    pip install --install-option="--prefix=$PREFIX_PATH/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages" scikit-learn

But got the following error:

Requirement already satisfied (use --upgrade to upgrade): scikit-learn in ./Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
  1. Then, I tried to add the existing Enthought folder to the path for the newly installed Python 2.7.8, By entering the following line at the end of the .bash_profile:

    PYTHONPATH=$PYTHONPATH:Users/***/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

This led to errors when trying to import some of the packages, probably for this reason: Cannot import Scikit-Learn

I would really prefer just to install a new version of scikit-learn in a separate folder. Anyone have any suggestions?

Community
  • 1
  • 1
monkeybiz7
  • 4,898
  • 5
  • 21
  • 35

1 Answers1

1

You can use virtualenv to create a self-contained python environment that can be configured and used separately from your regular python installation.

Create the virtualenv (for oldish versions of virtualenv you'd want to include --no-site-packages right after virtualenv):

$ virtualenv limitedenv
Using base prefix '/usr/local/Cellar/python3/3.3.3/Frameworks/Python.framework/Versions/3.3'
New python executable in limitedenv/bin/python3
Also creating executable in limitedenv/bin/python
Installing setuptools, pip...done.

Move into the virtualenv and activate it:

$ cd limitedenv/
$ source bin/activate
(limitedenv)$ 

Install the packages you're after with pip as you'd do globally:

(limitedenv)$ pip install scikit-learn
Downloading/unpacking scikit-learn
Downloading scikit-learn-0.15.0.tar.gz (7.0MB): ...

scikit-learn will now be installed just inside limitedenv, and as long as you have that environment active, invoking python or pip will be like this is your very own, secluded Python installation.

You can exit from the virtual environment by calling deactivate:

(limitedenv)$ deactivate
$

This allows you to have different versions of python by themselves, different versions of libraries pr. project and different configurations based on what your project requires. virtualenv is a very useful tool!

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Am I supposed to copy the chunk of text directly into Terminal, or am I supposed to copy it into a text file and try to run that? The first approach is giving me a ton of errors. – monkeybiz7 Jul 18 '14 at 22:37
  • Each line prefixed with $ is typed into the terminal, the rest is examples of what happens when you do that. This assumes you're using a proper shell (which terminal does). – MatsLindh Jul 18 '14 at 22:40
  • I tried creating a shell script with the following: virtualenv limitedenv Using base prefix '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages' New python executable in limitedenv/bin/python2 Also creating executable in limitedenv/bin/python Installing setuptools, pip Unfortunately I got a whole bunch of errors when I tried to run the script, starting with: ERROR: The executable limitedenv/bin/python is not functioning ERROR: It thinks sys.prefix is u'/Users/***/Desktop' (should be u'/Users/***/Desktop/limitedenv') – monkeybiz7 Jul 19 '14 at 01:14
  • Did you run `source bin/activate`? This makes the virtual environment the currently active environment. You can do each of these steps manually in the Terminal, instead of creating a shell script. – MatsLindh Jul 19 '14 at 20:17
  • The limitedenv folder was successfully created, and contains the following folders: bin/, include/, and lib/ However, after cd'ed into limitedenv/, and typed in source bin/activate, I got the following:-bash: bin/activate: No such file or directory – monkeybiz7 Jul 23 '14 at 15:09
  • I'd be helpful if you could add the whole sequence to a pastebin somewhere, with output for each step. Also, include --no-site-packages when calling virtualenv. If you want to start over, just `rm -rf limitedenv`. – MatsLindh Jul 23 '14 at 17:46
  • Thanks, MatsLindh for your patience in helping a major noobie out. The steps I took are listed here: http://pastebin.com/5T7a1aiS It appears that things go wrong very early. – monkeybiz7 Jul 26 '14 at 00:22