0

I am using scikit-learn library and want to play around with the code. How to call the custom library instead of the standard library? Here mycode.py is the main function, which call functions from sklearn (I want to modify sklearn functions)

Here is my code structure

mycode.py
scikit-learn\
    benchmarks\
    sklearn\
    etc

I tried in mycode.py:

from scikit-learn import sklearn

doesn't work because scikit-learn is an invalid Python module name. I can't change repository name either since it will break all the sklearn tests. I also tried

import os, sys
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(CURRENT_DIR+"/scikit-learn"))
import sklearn

But it keeps importing the standard library. How to fix this problem?

Dzung Nguyen
  • 3,794
  • 9
  • 48
  • 86

3 Answers3

0

Can you just make an __init__.py file?

http://effbot.org/pyfaq/what-is-init-py-used-for.htm

http://docs.python.org/2/tutorial/modules.html#packages

jossgray
  • 497
  • 6
  • 20
0

Instead of appending the directory to the end of sys.path, insert it at the beginning. This will cause Python to search it for packages and modules before trying entries that appear later, which include the standard locations containing the stdlib.

Note that this will only override external modules that haven't been previously imported.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

What about giving the custom module an entirely different name (let's call it customslkearn), and then:

import customsklearn as sklearn

If that isn't found, you could either add the directory you have it in to PYTHONPATH or to sys.path. This would of course force you to change the name of the directory you have it in, to avoid a name collision. Hope that's acceptable in your setting.

Zak
  • 3,063
  • 3
  • 23
  • 30