Import Spacy Error "cannot import name dataclass_transform" - python

I am working on a jupyter notebook project which should use spacy. I already used pip install to install spacy in anaconda prompt.
However, when I tried to import spacy, it gives me the follwing error.
I wonder what the problem is and what I can do to solve that.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-96-3173a3034708> in <module>
9 #nltk.download()
10 from nltk.corpus import stopwords
---> 11 import spacy
12
13 #path where we store the txt files
D:\Python\lib\site-packages\spacy\__init__.py in <module>
4
5 # set library-specific custom warning handling before doing anything else
----> 6 from .errors import setup_default_warnings
7
8 setup_default_warnings() # noqa: E402
D:\Python\lib\site-packages\spacy\errors.py in <module>
1 import warnings
----> 2 from .compat import Literal
3
4
5 class ErrorsWithCodes(type):
D:\Python\lib\site-packages\spacy\compat.py in <module>
1 """Helpers for Python and platform compatibility."""
2 import sys
----> 3 from thinc.util import copy_array
4
5 try:
D:\Python\lib\site-packages\thinc\util.py in <module>
6 import functools
7 from wasabi import table
----> 8 from pydantic import create_model, ValidationError
9 import inspect
10 import os
D:\Python\lib\site-packages\pydantic\__init__.cp38-win_amd64.pyd in init pydantic.__init__()
D:\Python\lib\site-packages\pydantic\dataclasses.cp38-win_amd64.pyd in init pydantic.dataclasses()
ImportError: cannot import name dataclass_transform

You can try following codes:
pip install -U pip setuptools wheel
pip install -U spacy
python -m spacy download en_core_web_sm
After installation restart the kernal if you are using Jupyter notebook or lab.
It does works for my end.

I am using Windows with Anaconda virtual environment. Uninstalling spacy from pip and installing it from conda did the job for me.
pip uninstall spacy
conda install spacy
python -m spacy download en_core_web_sm --user

Related

Cannot import name 'soft_unicode' from 'markupsafe' in google colab

I'm trying to install pycaret==3.0.0 in google colab, But I'm having a problem, the library requires Jinja2 to be installed which I did, but then It finally throws off another error.
ImportError Traceback (most recent call last)
<ipython-input-26-4f8843d24b3a> in <module>()
----> 1 import jinja2
2 from pycaret.regression import *
3 frames
/usr/local/lib/python3.7/dist-packages/jinja2/filters.py in <module>()
11 from markupsafe import escape
12 from markupsafe import Markup
---> 13 from markupsafe import soft_unicode
14
15 from ._compat import abc
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/root/.local/lib/python3.7/site-packages/markupsafe/__init__.py)
This is caused by upgrade in MarkupSafe:2.1.0 where they have removed soft_unicode, try using:
pip install markupsafe==2.0.1
Can you try this. This will install the latest release of pycaret and should take care of all dependencies for you. Just remember to restart the kernel after installation for the install to take effect correctly.
pip install -U --pre pycaret

Attribute error when trying to import Spacy

I have some issues when I try to import spacy. I downloaded the package and have the newest version, but still I get this error and I cannot find any answer. Maybe someone here had this problem and can help me out here.
This is the error when I do "import spacy":
AttributeError Traceback (most recent call last)
in ()
----> 1 import spacy
~\Anaconda3\lib\site-packages\spacy_init_.py in ()
8
9 # These are imported as part of the API
---> 10 from thinc.neural.util import prefer_gpu, require_gpu
11
12 from . import pipeline
~\Anaconda3\lib\site-packages\thinc_init_.py in ()
6
7 from .about import name, version # noqa: F401
----> 8 from ._registry import registry
~\Anaconda3\lib\site-packages\thinc_registry.py in ()
----> 1 import catalogue
2
3
4 class registry(object):
5 optimizers = catalogue.create("thinc", "optimizers", entry_points=True)
~\Anaconda3\lib\site-packages\catalogue.py in ()
16
17 # Only ever call this once for performance reasons
---> 18 AVAILABLE_ENTRY_POINTS = importlib_metadata.entry_points()
19
20 # This is where functions will be registered
AttributeError: module 'importlib_metadata' has no attribute 'entry_points'
Thanks in advance!
It's just a problem with your metadata package. Try this since you are using conda environments:
conda install -c conda-forge importlib_metadata
If you prefer pip, you can do
pip uninstall importlib_metadata
pip install importlib_metadata --force-reinstall

ImportError: cannot import name 'context' from 'tensorflow.python.eager' (unknown location)

I created virtual environment and installed both tensorflow and tensorflow-gpu. After that I installed keras. And then I checked in my conda terminal by importing keras and I was able to import keras in it. However, using jupyter notebook if I try to import keras then it gives me below error.
import keras
ImportError Traceback (most recent call last)
<ipython-input-5-88d96843a926> in <module>
----> 1 import keras
~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications
~\Anaconda3\lib\site-packages\keras\utils\__init__.py in <module>
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7 from . import losses_utils
8 from . import metrics_utils
~\Anaconda3\lib\site-packages\keras\utils\conv_utils.py in <module>
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11
~\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>
----> 1 from .load_backend import epsilon
2 from .load_backend import set_epsilon
3 from .load_backend import floatx
4 from .load_backend import set_floatx
5 from .load_backend import cast_to_floatx
~\Anaconda3\lib\site-packages\keras\backend\load_backend.py in <module>
88 elif _BACKEND == 'tensorflow':
89 sys.stderr.write('Using TensorFlow backend.\n')
---> 90 from .tensorflow_backend import *
91 else:
92 # Try and load external backend.
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in <module>
4
5 import tensorflow as tf
----> 6 from tensorflow.python.eager import context
7 from tensorflow.python.framework import device as tfdev
8 from tensorflow.python.framework import ops as tf_ops
ImportError: cannot import name 'context' from 'tensorflow.python.eager' (unknown location)
Already tried uninstalling and installing keras and tensorflow.
I'm pretty new to programming so I am not sure how to go around it. Tried looking other threads but not helping. Can any one recommend what can I do to resolve it? Thanks
Did you installed the dependencies with conda? Like this:
$ conda install -c conda-forge keras
$ conda install -c conda-forge tensorflow
$ conda install -c anaconda tensorflow-gpu
If you installed with pip they will not work inside your virtual env. Look at your conda dependencies list, to see if the tensorflow and keras are really there using:
$ conda list
If they are, activate your virtual environment:
$ conda activate 'name_of_your_env'
And run the jupyter inside that, should be something like that (if your env shows in parenthesis the activation worked, and you are now inside the virtual env):
(your_env)$ jupyter notebook
Doing below solved my issue.
So I removed all the packages that were installed via pip and intstalled packages through conda. I had environment issue and created another environment from the scratch and ran below commands.
Create virtual environment:
conda create -n <env_name>
Install tensorflow-gpu via conda and not pip. If you skip create environment command, type in below as it will scratch off new env and specify python and tensorflow version.
conda create -n <env_name> python=3.6 tensorflow-gpu=2.2
And then I had to make sure that jupyter notebook is opening with the environment that I want it to open with. For that below code.
C:\Users\Adi(Your user here)\Anaconda3\envs\env_name\python.exe -m ipykernel install --user --name <env_name> --display-name "Python (env_name)"
When you go to Jupyter notebook, on the top right corner you should see your virtual environment and make sure you select that. And it got resolved like that.

ModuleNotFoundError: No module named 'xgboost.sklearn'

I'm trying to import xgboost into jupyter-notebook but get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-9-a585b270d0df> in <module>
1 import pandas as pd
2 import numpy as np
----> 3 import xgboost
~/.local/lib/python3.6/site-packages/xgboost/__init__.py in <module>
14 from . import tracker # noqa
15 from .tracker import RabitTracker # noqa
---> 16 from . import dask
17 try:
18 from .sklearn import XGBModel, XGBClassifier, XGBRegressor, XGBRanker
~/.local/lib/python3.6/site-packages/xgboost/dask.py in <module>
31 from .training import train as worker_train
32 from .tracker import RabitTracker
---> 33 from .sklearn import XGBModel, XGBClassifierBase, xgboost_model_doc
34
35 # Current status is considered as initial support, many features are
ModuleNotFoundError: No module named 'xgboost.sklearn'
I've downloaded sklearn as well as sci-kit learn and they work fine...
Any idea what the problem is?
You might need to install your packages properly.
For best practice, you'll need to use a conda environment. Check out how it works here: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
Once you have created your environment, activate it and then install all the packages you need. Presumably, you'll have to run the command:
conda install -c conda-forge xgboost
pip install -U scikit-learn
To install your machine learning packages.
XGBOOST is in xgboost module: It should be imported as:
`from xgboost import XGBRegressor`

Error importing LGBMResgressor to lightgib

I'm getting the following error while importing LGBMResgressor to lightgbm:
ImportError
Traceback (most recent call last)
<ipython-input-38-1a53b4f7b770> in <module>()
5 from sklearn.kernel_ridge import KernelRidge
6 import lightgbm as lgb
----> 7 from lightgbm import LGBMRegressor
8 # from lightgbm.sklearn import LGBMRegressor
9 from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
ImportError: cannot import name 'LGBMRegressor'
lightgbm's version is 2.0.5, and it is installed on Windows 10 by pip install lightgbm.
My environment is linux (python 3.6.1) and I installed with pip install lightgbm, opened a ipython terminal and copy your imports and not occurred error, but i can see an 'inconsistence' in your imports. On line six you make:
import lightgbm as lgb
And on the next line
from lightgbm import LGBMRegressor
On line 6 you already import all module as lgb (line 7 is unnecessary), for use LGBMRegressor just do:
lgb.LGBMRegressor
Finnaly, to make sure that the module are insalled, type in a command line:
python -c "import lightgbm; print(lightgbm.__version__)"
Output:
2.0.1

Categories