Stefan Falk
Stefan Falk

Reputation: 25447

Cannot import packages in sub modules

I have a project structure like this:

project-root/
   source-root/
      sub-package/
         not_working.py
      working.py

If I import BaseEstimator in working.py:

from sklearn.base import BaseEstimator

everything is .. working.

If I do the same in not_working.py I am getting

Traceback (most recent call last):
  File "/home/user/project-root/source-root/sub-package/not_working.py", line 3, in <module>
    from sklearn.base import BaseEstimator
ImportError: No module named 'sklearn.base'; 'sklearn' is not a package

No idea what the problem is. I can load other modules

This is working.py and not_working.py - both have the same code.

import re

import numpy as np
import pandas as pd

from sklearn.base import BaseEstimator
from sklearn.pipeline import Pipeline, FeatureUnion

I can import numpy, pandas without troubles but it's not working for the sklearn packages.


I have set the interpreter to my local Python3.5 interpreter

enter image description here

and the packages seem to be installed correctly:

$ pip3 show sklearn
Name: sklearn
Version: 0.0
Summary: A set of python modules for machine learning and data mining
Home-page: https://pypi.python.org/pypi/scikit-learn/
Author: UNKNOWN
Author-email: UNKNOWN
License: None
Location: /usr/local/lib/python3.5/dist-packages
Requires: scikit-learn

Upvotes: 1

Views: 171

Answers (1)

Stefan Collier
Stefan Collier

Reputation: 4682

Following our convo the not_working.py was called sklearn.py, hence the conflict of names meant that sklearn was merely a file not the package we want.


Solution Rename sklearn.py to something else, e.g. learner.py

Upvotes: 3

Related Questions