Arman
Arman

Reputation: 146

cannot import name 'pipline' from 'transformers' (unknown location)

I am getting this error when trying following code in the jupyter-lab:

from transformers import pipeline

Amazingly, if I copy that line of code in a code_test.py file, and execute it using python3 code_test.py(both in the terminal and jupyter-lab itself) everything will work fine.

I am using jupyter-lab and which is configured to use a virtual-env(the one containing transformers module).

I have searched for similar problems, but none of proposed solutions worked(such as reinstalling the transformers module).

Edited:

Output of sys.path in jupyter-lab:

['/Users/{my_username}/{path_to_my_project}/code',
 '/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '',
 '/Users/{my_username}/Library/Python/3.7/lib/python/site-packages',
 '/usr/local/lib/python3.7/site-packages',
 '/Users/{my_username}/Library/Python/3.7/lib/python/site-packages/IPython/extensions',
 '/Users/{{my_username}/.ipython']

Output of sys.path in code_test.py:

['/Users/{my_username}/{path_to_my_project}/code',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
'/Users/{my_username}/{path_to_my_project}/code/env/lib/python3.7/site-packages']

Upvotes: 1

Views: 3880

Answers (1)

cronoik
cronoik

Reputation: 19320

In general when you face such an issue that an import is working in one environment (script code_test.py) but not in the other (jupyter-lab), you need to compare the search path for modules with sys.path and the location of the module with MODULE.__file__ (transformers.__file__ in this case).

When you compare the outputs of sys.path of both environments you will notice that '/Users/{my_username}/{path_to_my_project}/code/env/lib/python3.7/site-packages' is only listed in one and this is exactly the location where the transformers module is loaded from (output of transformers.__file__). That means jupyter-lab is not using your virtual environment.

All you need to do is to register your environment for jupyter-lab with:

python3 -m ipykernel install --user --name=env

and jupyter-lab will now allow you to select the environment as kernels.

Upvotes: 1

Related Questions