Python error: No module named 'sklearn'

Hi,
I have troubles trying to import the DecisionTreeClassifier python class in a Python Learner node. Using the Anaconda Python prompt, it works.
Cheers
Alfredo

Knime version is 3.7.1
Python version is 3.6.8

When executing this line:
from sklearn.tree import DecisionTreeClassifier

I get this error message:

Traceback (most recent call last):
File "C:\Program Files\KNIME\plugins\org.knime.python2_3.7.1.v201901281201\py\PythonKernelBase.py", line 278, in execute
exec(source_code, self._exec_env, self._exec_env)
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sklearn’

Hi alfroc,

Before digging deeper into this problem, I just want to make sure that you’re using the same conda environment in both KNIME and the Anaconda Prompt - is this the case?
Something you could test, would be to execute the py36.bat inside the Anaconda prompt and then try to execute your code.

Marcel

Hi Marcel,
executing the py36.bat inside Anaconda prompt gives the same error.
Thanks,
Alfredo

Alright, then I’d assume that the batch script activates the wrong conda environment. There should be a line like

@CALL activate <MY_ENVIRONMENT>

somewhere in py36.bat, where <MY_ENVIRONMENT> should be the name of the conda environment in which sklearn is installed. If you were able to run your code in the Anaconda prompt without activating a specific environment first, then the right environment name would most likely be base.

Please also see our installation guide which shows in more detail how to install packages in a conda environment and use that within KNIME.

Marcel

2 Likes

Hi Marcel,
I followed the steps described in the installation guide but the problem still remains.
Please have a look to my configuration as described in the attached file and tell me what is wrong.
Thanks!
Alfredo

python_configuration.docx (82.1 KB)

Hi Alfredo,

Executing

conda install -n py36_knime scikit-learn

using the Anaconda prompt should resolve the problem.

Some explanation:

The py36_knime.yml file we provide on our documentation page (the first item in your uploaded python_configuration.docx file) does not contain scikit-learn at the moment because we currently intend to only provide a list of packages that are essential for the KNIME Python integration to work, we do not include other popular data science packages. Therefore the py36_knime environment you created does not contain sklearn by default. It needs to be manually installed.

The existence of the C:\Users\Alfredo\Anaconda3\pkgs\scikit-learn-0.20.1-py37h343c172_0\Lib\site-packages\sklearn\tree directory (the fourth item in you upload) only means that you downloaded scikit-learn using conda sometime in the past (think of the pkgs directory as the “Downloads” folder of conda). It does not mean that the downloaded package is automatically available in the py36_knime environment. It will however be available in the environment once you install it there (see the command at the top). You will see that there will be a directory C:\Users\Alfredo\Anaconda3\envs\py36_knime\lib\python3.6\site-packages\sklearn\tree after you executed the command above but not before.

Hope this helps!

Marcel

2 Likes

It works!
Thank you very much, Marcel, for the quick answers and excellent support!
Alfredo

2 Likes

Glad I could help! :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.