Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import spacy transformers

I have already installed spacy using conda install -c conda-forge spacy and spacy-transformers using pip install spacy-transformers. But when I try to import spacy_transformers. I get the following error

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In [1], line 1
----> 1 import spacy_transformers

File ~/Documents/Programming/Python/Mini-Project/spacy_transformers.py:4
      1 import spacy
      3 text = "Apple and Samsung"
----> 4 trf = spacy.load('en_core_web_trf')
      5 doc = trf(text)
      6 for ent in doc.ents:

File ~/opt/anaconda3/envs/transformers2/lib/python3.9/site-packages/spacy/__init__.py:54, in load(name, vocab, disable, enable, exclude, config)
     30 def load(
     31     name: Union[str, Path],
     32     *,
   (...)
     37     config: Union[Dict[str, Any], Config] = util.SimpleFrozenDict(),
     38 ) -> Language:
     39     """Load a spaCy model from an installed package or a local path.
     40 
     41     name (str): Package name or model path.
   (...)
     52     RETURNS (Language): The loaded nlp object.
     53     """
...
    125             break
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

ModuleNotFoundError: No module named 'spacy_transformers.pipeline_component'; 'spacy_transformers' is not a package

My goal is to extract entities from a sentence using en_core_web_trf

like image 457
Fiesty_Anywhere Avatar asked Dec 31 '25 13:12

Fiesty_Anywhere


1 Answers

So from your stack trace I can tell you named your script spacy_transformers.py. What happens is when en_core_web_trf tries to load spaCy transformers, Python loads your script instead of the library, because the name is the same. You need to change the name of your script.

Keep in mind that when importing, Python (typically) checks the local path for a module (which can be a file or a directory) with the name before checking other directories. So don't make a file or directory with the same name as any library you're using.

like image 119
polm23 Avatar answered Jan 04 '26 08:01

polm23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!