1

I installed python using Anaconda on Ubuntu, which installed the default version 3. However, I'm following the Titanic Kaggle tutorial, which uses 2.7 and is therefore throwing me a lot of errors. I managed to install Python 2.7 to use with my Jupyter notebook, but whenever I tried to import numpy it tells me there is no module named numpy, even though the import function works perfectly well on a Python 3 notebook.

What am I doing wrong here? How can I get the import function to work for the Python 2.7 notebook?

cel
  • 30,017
  • 18
  • 97
  • 117
AnnieH
  • 35
  • 8
  • What operating system are you using? – pneumatics Jul 15 '16 at 17:20
  • I'm sorry-- I'm using Ubuntu – AnnieH Jul 16 '16 at 17:53
  • My answer here might help.The idea is to add a python2.7 environment and install a kernel: http://stackoverflow.com/questions/30492623/using-both-python-2-x-and-python-3-x-in-ipython-notebook/30492913#30492913 – cel Jul 17 '16 at 22:03
  • @cel user already has the Python kernels installed, and needs numpy. Your answer doesn't even mention numpy. Read the question again. – pneumatics Jul 17 '16 at 22:23
  • @pneumatics, my answer explains how anaconda environments work and how you can make them available in jupyter. I am pretty sure OP knows that they have to install numpy before using it. However it is not so straightforward to figure out where you have to install it. Having understood the concept of conda environments it should be clear how to install numpy. – cel Jul 18 '16 at 05:49
  • @pneumatics, @AnnieH, the only thing you have to change from my answer is using `conda create -n py27 python=2.7 ipykernel anaconda` instead of `conda create -n py27 python=2.7 ipykernel`. This will install the full set of anaconda packages in the python2.7 environment. – cel Jul 18 '16 at 05:57
  • The user's goal is not to 'understand the concept of conda environments', it is to run the Titanic Kaggle tutorial. That tutorial's only non-system dependency is `numpy`. The error above indicates the user _does_ know that `numpy` is required, since the expectation was that `import numpy` would work, but wants to know how to install `numpy` for Python 2.7. On Ubuntu, system packages are available and reliable. I like conda, and your answer very nicely shows how to populate the Jupyter kernel list with conda environments, but we don't need an electric hammer. – pneumatics Jul 18 '16 at 15:20
  • Thanks to the both of you! When I tried the answer provided by @cel, it tells me that the prefix already exists, since I managed to make Python 2.7 available in my kernel, it just doesn't recognize numpy for whatever reason. – AnnieH Jul 19 '16 at 17:32
  • I also tried @pneumatics answer, but it didn't work as numpy works great with python 3, but not python 2.7 – AnnieH Jul 19 '16 at 17:35
  • @AnnieH, but you did install numpy into the python2 environment, right? – cel Jul 19 '16 at 18:50
  • @cel I used apt-get install python-numpy python2-numpy -y and it threw me an error... what did I mess up? – AnnieH Jul 19 '16 at 19:41
  • 1
    @AnnieH, I don't recommend using apt-get, but if you really want to use it over conda, you have to do `sudo apt-get ...`, it's a system package manager and therefore requires root privileges. – cel Jul 19 '16 at 19:46
  • @cel I used conda create -n py27 python=2.7 ipykernel anaconda but it says "Error: prefix already exists" .... so I do have python 2.7 installed, but it won't recognize numpy – AnnieH Jul 20 '16 at 17:14
  • @AnnieH, you explicitly have to install numpy in that environment (`conda install numpy -n py27`). You also have to make sure that this `py27` environment is available in jupyter by installing the ipython kernel (see my link above how to do that) – cel Jul 20 '16 at 17:48
  • 1
    @cel thank you! that worked! – AnnieH Jul 25 '16 at 15:38

1 Answers1

0

On Ubuntu, life is pretty easy, and there are packages for numpy for python2 and python3.

sudo apt-get install python-numpy python3-numpy -y

That should patch you up. The numpy package was available for me in both the Python 2 and Python 3 kernels, running Jupyter under Python 3, on a fresh Ubuntu 16.04 instance on Digital Ocean (14.04 also works, but installs Numpy 1.8, which is out of date. Recommend upgrading to 16.04)

Installing the Python 2 kernel in Jupyter used to be mildly tricky, but the Jupyter documentation is pretty good now.

cel
  • 30,017
  • 18
  • 97
  • 117
pneumatics
  • 2,836
  • 1
  • 27
  • 27