Khaleel Shazada
Khaleel Shazada

Reputation: 351

ModuleNotFoundError: no module named 'transformers'

This is my first post and I am new to coding, so please let me know if you need more information. I have been running some AI to generate artwork and it has been working, but when I reloaded it the python script won't work and it is now saying "No module named 'transformers'". Can anyone help me out? It was when I upgraded to Google Colab Pro that I started to encounter issues although I am not sure why that would make a difference.

ModuleNotFoundError

enter image description here

Upvotes: 34

Views: 165403

Answers (13)

Rihana Parveen
Rihana Parveen

Reputation: 1

I encountered the same problem. I rectified this by:

  1. Selecting the Python kernel in the vs code editor.
  2. Please check the environmental variables, and whether you have given the correct path variable.
  3. Finally pip install transformers torch.

Now set to go

Upvotes: 0

Saikat Roy
Saikat Roy

Reputation: 1

The following worked for me:

!pip install transformers==2.1.0 

Upvotes: 0

Deepthi Tabitha Bennet
Deepthi Tabitha Bennet

Reputation: 1156

These are some possible solutions

1. Install the required package:

Run the following command

!pip install transformers

Or, if that doesn’t work, try

!pip3 install transformers

Please note the use of !, as it is a shell command, and not a python script.

2. Start over with a new run-time:

Click Runtime > Disconnect and delete runtime

Then click Runtime > Run all

Upvotes: 5

Saurav Jajodia
Saurav Jajodia

Reputation: 1

This command worked for me:

py -3 -m pip install transformers

Upvotes: 0

rr_goyal
rr_goyal

Reputation: 694

In addition to @chattershuts answer and based on a different answer on the usage of magic functions (the full post can be found here), I would recommend the usage of these magic functions.

Therefore, the command would be-

%pip install transformers

Upvotes: 0

Ashok Chhetri
Ashok Chhetri

Reputation: 549

Even after I used this command (pip install transformers) the terminal said,

ModuleNotFoundError: No module named 'transformers'

But this solved it, in vscode terminal:

python -m pip install transformers

Upvotes: 1

rz1027
rz1027

Reputation: 86

Sometimes your editor will confuse python interpreters.

Probably use

!python -m pip install transformers

Upvotes: 4

Aleksandr Bogatyrev
Aleksandr Bogatyrev

Reputation: 11

You need to check which environment you are installing the library in and which one you are using to run the cells. Perhaps this is the problem.

It's not very intuitive, and for some reason the notebook doesn't change the environment to the correct one automatically when switching

Even when you change the environment and run "!pip install transformers" in the cell, it will be installed in the originally selected environment

Upvotes: 1

Mohammed Younis
Mohammed Younis

Reputation: 1

if it does not work with pip, try installing it with anaconda: conda install -c conda-forge transformers

Upvotes: 0

mirekphd
mirekphd

Reputation: 6881

If the already installed package shows in !pip show transformers but you still cannot import transformers, try restarting Python kernel (runtime) using Jupyter Lab/Notebook (Google Colab) menu.

This worked for me in Google Colab's GPU (T4) instance after installing transformers with pip initially produced the above error message during import attempts.

Upvotes: 0

Tyler Robbins
Tyler Robbins

Reputation: 121

Assuming you are referring to the module here https://pypi.org/project/transformers/ you need to install transformers with pip

pip install transformers

Upvotes: 9

Omar Hossam
Omar Hossam

Reputation: 312

after you install transformers, make sure to import it , and import the Module youre gonna use

Upvotes: 0

chattershuts
chattershuts

Reputation: 503

Probably it is because you have not installed in your (new, since you've upgraded to colabs pro) session the library transformers. Try to run as first cell the following: !pip install transformers (the "!" at the beginning of the instruction is needed to go into "terminal mode" ). This will download the transformers package into the session's environment.

Upvotes: 33

Related Questions