Question

On my system I have two versions of Python (to call them I type python and python2 in the command line). When I use the first version of Python, I cannot import sklearn module but I can do it in the second version of Python.

I would like to use the first version of python (because other modules are available there) and, at the same time, I would like to be able to import sklearn from this version of Python.

My solution was to use:

import sys
sys.path.append('location_of_the_sklearn_module')

To find the location of the sklearn module I started a python session (using the second version of python, in which sklearn works). The I type:

import sklearn
sklearn.__file__

As a result I got:

/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn/__init__.pyc

In the session of the first version of Python I tried:

import sys
sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')
import sklearn

Unfortunately it did not work. As a result I got: ImportError: No module named sklearn

Does anybody know what I am doing wrong and if it is possible to reach the goal in the way I try?

Was it helpful?

Solution

When importing packages, you need to add the parent directory of the package to PYTHONPATH, not the package directory itself, so just change...

sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages/sklearn')

...to...

sys.path.append('/home/name/my_name/numpy/local/lib/python2.7/site-packages')

...although it may not necessarily import correctly in Python 3.x.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top