How to import a module from a directory?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2013-06-25T16:09:22Z Indexed on 2013/06/25 16:21 UTC
Read the original article Hit count: 213

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about import