Charlie Parker
Charlie Parker

Reputation: 5301

Why can't I import a function in Python using the "as" syntax?

Why does this work,

from sklearn.metrics import mean_squared_error

but not the other?

import sklearn.metrics.mean_squared_error as mse

This gives

ModuleNotFoundError: No module named 'sklearn.metrics.mean_squared_error'

It is not possible because mean_squared_error is a function is my guess?

Upvotes: 1

Views: 66

Answers (1)

Karl Knechtel
Karl Knechtel

Reputation: 61526

You cannot import sklearn.metrics.mean_squared_error because it is not a module but a function, yes. The as part stands completely independently. So you can, for example, from sklearn.metrics import mean_squared_error as mse.

Upvotes: 4

Related Questions