Nameerror: name ‘train_test_split’ is not defined

How to fix the Python nameerror: name ‘train_test_split’ is not defined error message.

If this error gives you a hard time, then you must continue reading.

It is because this article discusses what this error means and why it occurs.

Aside from that, we will also guide you to fix the nameerror name train_test_split is not a defined error message in Python.

What is train_test_split?

The train_test_split is a function from the scikit-learn library that is used to split data into two subsets:

The subsets are:


✔ and testing set.

The training set is used to train a machine learning model, while the testing set is used to evaluate its performance.

To use this function, you have to import it from the sklearn.model_selection module.

What is “nameerror name ‘train_test_split’ is not defined”?

The nameerror: name ‘train_test_split’ is not defined is an error message in Python that occurs when you are trying to use a function train_test_split without importing it first.

In addition to that, this error is usually raised because you did not install the required modules or libraries or forgot to define it before using the function.

Therefore, Python doe not know what the train_test_split function is.

Why does this error occur?

The nameerror name train_test_split is not defined can occur due to some factors that include the following:

❌ The Scikit-learn or sklearn library is not imported.

❌ The train_test_split function is not imported.

❌ Misspelled the function name

How to fix “nameerror: name ‘train_test_split’ is not defined”?

To fix the nameerror name ‘train_test_split’ is not defined error message, ensure that the train_test_split function is imported correctly.

Solution 1: Install installed sklearn

If you haven’t installed the sklearn library, or you are not if you have it already, you can check it using the following command:

import sklearn

If you get this error message below:

ModuleNotFoundError: no module named 'sklearn' 

You have to install the module, here’s the command that will install the sklearn.

✅ pip install sklearn
pip install scikit-learn


✅ pip3 install sklearn
✅ pip3 install scikit-learn

Solution 2: Import the train_test_split function from sklearn.model_selection

You can import the train_test_split function from the sklearn.model_selection module.

Kindly refer to the example code below:

✅ from sklearn.model_selection import train_test_split

Solution 3: Check for typos

Ensure that you’ve spelled the function name correctly. It should be train_test_split, to avoid encountering this error.


In conclusion, the error message nameerror: name ‘train_test_split’ is not defined occurs when you are trying to use a function train_test_split without importing it first.

This article discusses what this error is all about and already provides solutions to help you fix this error.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment