1

I am able to import scikit-learn from python or ipython console (both python 2.7 and 3.5), but when I do import sklearn within an ipython notebook (again, either 2.7 or 3.5) I get the error message below. How can I fix this?


ImportError Traceback (most recent call last) in () ----> 1 import sklearn

ImportError: No module named sklearn

zarak
  • 2,933
  • 3
  • 23
  • 29
DJname
  • 667
  • 2
  • 6
  • 8
  • How do you start the notebook (command)? Are you sure you are using the same python environment than when running the ipython console? – dukebody Aug 20 '16 at 06:39
  • I start it by typing "ipython notebook". I don't know if I'm using the same environment. How can I check it? And if they disagree, how can I enforce using the same environment? – DJname Aug 24 '16 at 19:49
  • Create a virtualenv and run all commands from there. You probably want to install the `jupyter` package in that virtualenv. – dukebody Aug 25 '16 at 13:54
  • Is there a way to do it without creating a virtual environment? That seems like an extra step I'd have to execute and keep track of every time I use my ipython scripts. – DJname Aug 25 '16 at 14:34
  • I think what I'd like to achieve, for ease of use, is simply to use the same environment in iPython notebook that I use in shell-based iPython. Why is this so hard? – DJname Aug 25 '16 at 15:21

2 Answers2

0

The issue you report remains a bit unclear, as you are not giving much detail about the steps you have taken to setup Python or iPython. Such information, along with your Operating System (OS) version information, would help answer your question.

I'm assuming you use some verison of Windows (if not, please specify your OS in the question). Also, you seem to have multiple versions of Python installed, and iPython connected to one of them. First, go to iPython and use the menu 'Help > About' in order to check which version of Python is connected to iPython. Write this down for future reference.

Net, you want to check whether this version of Python has sklearn installed. The generally most convenient way to install packages in Python, which also works for sklearn, is pip, see here. However, before you use pip to install or upgrade sklearn, you first want to make sure that the pip commands you'll be giving will install sklearn in the installation instance of Python that is linked to your notebook.

To do that, you need to check that the version of Python that is linked to your iPython is the only one recognized in your system path (this means that when you put in commands at the Windows command prompt, they will be recognized as commands to this version of Python).

Here is an instruction for how to check this in Windows 7. For other versions of Windows, please google, or search engine name of your choice, something like 'Add python to path '. If you are using a different OS, you may need to follow an entirely different procedure. In that case I'd advise you to edit your question and search StackOverflow.

After you've made sure the version of python appended to the path variable is the one linked to iPython, go to your command prompt (search for 'cmd' in your start menu), open it, and type 'pip install sklearn'. Pip will now install the sklearn package for you in the Python version iPython is using.

Finally, you can use the 'cd' command to go to your preferred working directory for iPython (the one where you want to put your projects), start the Jupyter notebook with the command 'jupyter notebook' and, in theory, import sklearn for usage in your code.

Note that there are various possible version / configuration problems with sklearn and Jupyter notebook which I am not covering here. Should any of those occur, they are perfect input for a next StackOverflow question ; )

Best of luck,

Evert

Community
  • 1
  • 1
0

If you use Anaconda Navigator you can install scikit-learn manually. Go to the Environments tab, verify that it is installed in enviroment root and the enviroment where you want to use it. You can use the search engine to find scikit-learn. Click Apply and try it.

Good luck.