ImportError: cannot import name 'Sized' from 'collections' - python

I am trying to use one package that somebody developed on GitHub called PyLipid.
The first parts of the code are importing various packages, but I am continuously getting this error, showed bellow:
%matplotlib inline
import pylipid
import collections
from pylipid.api import LipidInteraction
# print(pylipid.__version__) # make sure pylipid version is later than 1.4
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [37], in <cell line: 4>()
2 import pylipid
3 import collections
----> 4 from pylipid.api import LipidInteraction
File ~/miniconda3/envs/work/lib/python3.10/site-packages/pylipid/api/__init__.py:32, in <module>
1 ##############################################################################
2 # PyLipID: A python module for analysing protein-lipid interactions
3 #
(...)
14 # copies or substantial portions of the Software.
15 ##############################################################################
17 r"""
18 PyLipID class
19 =============
(...)
29
30 """
---> 32 from .api import LipidInteraction
File ~/miniconda3/envs/work/lib/python3.10/site-packages/pylipid/api/api.py:30, in <module>
28 import pandas as pd
29 from tqdm import trange, tqdm
---> 30 from p_tqdm import p_map
31 from ..func import cal_contact_residues
32 from ..func import Duration
File ~/miniconda3/envs/work/lib/python3.10/site-packages/p_tqdm/__init__.py:1, in <module>
----> 1 from p_tqdm.p_tqdm import p_map, p_imap, p_umap, p_uimap, t_map, t_imap
3 __all__ = [
4 'p_map',
5 'p_imap',
(...)
9 't_imap'
10 ]
File ~/miniconda3/envs/work/lib/python3.10/site-packages/p_tqdm/p_tqdm.py:11, in <module>
1 """Map functions with tqdm progress bars for parallel and sequential processing.
2
3 p_map: Performs a parallel ordered map.
(...)
8 t_imap: Returns an iterator for a sequential map.
9 """
---> 11 from collections import Sized
12 from typing import Any, Callable, Generator, Iterable, List
14 from pathos.helpers import cpu_count
ImportError: cannot import name 'Sized' from 'collections' (/home/srdjanm/miniconda3/envs/work/lib/python3.10/collections/__init__.py)
Does anybody know how to solve this error? I have also posted the question on the webpage in github but it might take a while for someone to respond, also this seems to me like a more general problem. Is this the package or "python version" related issue?

You may want to modify one line in the library to it works. It was created a pull request in github of p_tqdm library
In file ~/miniconda3/envs/work/lib/python3.10/site-packages/p_tqdm/p_tqdm.py modify line 11 from
from collections import Sized
to
from collections.abc import Sized

Related

TypeError: load() missing 1 required positional argument: 'Loader' in Google Colab

I am trying to do a regular import in Google Colab.
This import worked up until now.
If I try:
import plotly.express as px
or
import pingouin as pg
I get an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-19-86e89bd44552> in <module>()
----> 1 import plotly.express as px
9 frames
/usr/local/lib/python3.7/dist-packages/plotly/express/__init__.py in <module>()
13 )
14
---> 15 from ._imshow import imshow
16 from ._chart_types import ( # noqa: F401
17 scatter,
/usr/local/lib/python3.7/dist-packages/plotly/express/_imshow.py in <module>()
9
10 try:
---> 11 import xarray
12
13 xarray_imported = True
/usr/local/lib/python3.7/dist-packages/xarray/__init__.py in <module>()
1 import pkg_resources
2
----> 3 from . import testing, tutorial, ufuncs
4 from .backends.api import (
5 load_dataarray,
/usr/local/lib/python3.7/dist-packages/xarray/tutorial.py in <module>()
11 import numpy as np
12
---> 13 from .backends.api import open_dataset as _open_dataset
14 from .backends.rasterio_ import open_rasterio as _open_rasterio
15 from .core.dataarray import DataArray
/usr/local/lib/python3.7/dist-packages/xarray/backends/__init__.py in <module>()
4 formats. They should not be used directly, but rather through Dataset objects.
5
----> 6 from .cfgrib_ import CfGribDataStore
7 from .common import AbstractDataStore, BackendArray, BackendEntrypoint
8 from .file_manager import CachingFileManager, DummyFileManager, FileManager
/usr/local/lib/python3.7/dist-packages/xarray/backends/cfgrib_.py in <module>()
14 _normalize_path,
15 )
---> 16 from .locks import SerializableLock, ensure_lock
17 from .store import StoreBackendEntrypoint
18
/usr/local/lib/python3.7/dist-packages/xarray/backends/locks.py in <module>()
11
12 try:
---> 13 from dask.distributed import Lock as DistributedLock
14 except ImportError:
15 DistributedLock = None
/usr/local/lib/python3.7/dist-packages/dask/distributed.py in <module>()
1 # flake8: noqa
2 try:
----> 3 from distributed import *
4 except ImportError:
5 msg = (
/usr/local/lib/python3.7/dist-packages/distributed/__init__.py in <module>()
1 from __future__ import print_function, division, absolute_import
2
----> 3 from . import config
4 from dask.config import config
5 from .actor import Actor, ActorFuture
/usr/local/lib/python3.7/dist-packages/distributed/config.py in <module>()
18
19 with open(fn) as f:
---> 20 defaults = yaml.load(f)
21
22 dask.config.update_defaults(defaults)
TypeError: load() missing 1 required positional argument: 'Loader'
I think it might be a problem with Google Colab or some basic utility package that has been updated, but I can not find a way to solve it.
Now, the load() function requires parameter loader=Loader.
If your YAML file contains just simple YAML (str, int, lists), try to use yaml.safe_load() instead of yaml.load().
And If you need FullLoader, you can use yaml.full_load().
Starting from pyyaml>=5.4, it doesn't have any discovered critical vulnerabilities, pyyaml status.
source: https://stackoverflow.com/a/1774043/13755823
yaml.safe_load() should always be preferred unless you explicitly need
the arbitrary object serialization/deserialization provided in order
to avoid introducing the possibility for arbitrary code execution.
More about yaml.load(input) here.
Found the problem.
I was installing pandas_profiling, and this package updated pyyaml to version 6.0 which is not compatible with the current way Google Colab imports packages.
So just reverting back to pyyaml version 5.4.1 solved the problem.
For more information check versions of pyyaml here.
See this issue and formal answers in GitHub
##################################################################
For reverting back to pyyaml version 5.4.1 in your code, add the next line at the end of your packages installations:
!pip install pyyaml==5.4.1
It is important to put it at the end of the installation, some of the installations will change the pyyaml version.
this worked for me
config = yaml.load(ymlfile, Loader=yaml.Loader)
The Python "TypeError: load() missing 1 required positional argument: 'Loader'" occurs when we use the yaml.load() method without specifying the Loader keyword argument.
To solve the error, use the yaml.full_load() method instead or explicitly set the Loader keyword arg.
config = yaml.full_load(ymlfile)
or
config = yaml.load(ymlfile, Loader=yaml.FullLoader)

Facing Import Error when importing esda and libpysal libraries

Even though I have installed both the libraries several times using different orders in different virtual environments, I'm still facing an issue where I'm not able to import and use certain geospatial libraries like esda and libpysal. The following error shows up:
ImportError Traceback (most recent call last)
C:\Users\SLAADM~1\AppData\Local\Temp/ipykernel_35328/2667884714.py in <module>
3 import numpy as np
4 import matplotlib.pyplot as plt
----> 5 import esda
6 import libpysal as lps
7 import pysal
c:\users\sla admin\appdata\local\programs\python\python39\lib\site-packages\esda\__init__.py in <module>
5
6 """
----> 7 from . import adbscan
8 from .gamma import Gamma
9 from .geary import Geary
c:\users\sla admin\appdata\local\programs\python\python39\lib\site-packages\esda\adbscan.py in <module>
8 import pandas
9 import numpy as np
---> 10 from libpysal.cg.alpha_shapes import alpha_shape_auto
11 from scipy.spatial import cKDTree
12 from collections import Counter
c:\users\sla admin\appdata\local\programs\python\python39\lib\site-packages\libpysal\__init__.py in <module>
25 Tools for creating and manipulating weights
26 """
---> 27 from . import cg
28 from . import io
29 from . import weights
c:\users\sla admin\appdata\local\programs\python\python39\lib\site-packages\libpysal\cg\__init__.py in <module>
9 from .sphere import *
10 from .voronoi import *
---> 11 from .alpha_shapes import *
c:\users\sla admin\appdata\local\programs\python\python39\lib\site-packages\libpysal\cg\alpha_shapes.py in <module>
22
23 try:
---> 24 import pygeos
25
26 HAS_PYGEOS = True
c:\users\sla admin\appdata\local\programs\python\python39\lib\site-packages\pygeos\__init__.py in <module>
----> 1 from .lib import GEOSException # NOQA
2 from .lib import Geometry # NOQA
3 from .lib import geos_version, geos_version_string # NOQA
4 from .lib import geos_capi_version, geos_capi_version_string # NOQA
5 from .decorators import UnsupportedGEOSOperation # NOQA
ImportError: DLL load failed while importing lib: The specified procedure could not be found.
Would really appreciate any help in making this work. Please throw any suggestions you might have at me.
install pygeos i.e conda install pygeos
it worked for me
I found same issue when running example code from a couple of years ago. The pysal API has changed.
Import libpysal first then import the esda libraries eg
import libpysal
from esda.moran import Moran
from esda.smaup import Smaup
see
https://pysal.org/esda/generated/esda.Moran.html

GluonCV ImportError: cannot import name 'is_np_array'

I'm trying to set up a GluonCV in a jupyter notebook in a virtual environment. For some reason whenever I try to import GluonCV I get this error:
ImportError Traceback (most recent call last)
<ipython-input-2-9a2bc396118f> in <module>
----> 1 import gluoncv
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\__init__.py in <module>
10 _require_mxnet_version('1.4.0', '2.0.0')
11
---> 12 from . import data
13 from . import model_zoo
14 from . import nn
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\__init__.py in <module>
29 from .sampler import SplitSampler, ShuffleSplitSampler
30 from .otb.tracking import OTBTracking
---> 31 from .kitti.kitti_dataset import KITTIRAWDataset, KITTIOdomDataset
32
33 datasets = {
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\kitti\__init__.py in <module>
1 # pylint: disable=missing-module-docstring
----> 2 from .kitti_dataset import *
3 from .kitti_utils import *
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\kitti\kitti_dataset.py in <module>
19
20 from ...utils.filesystem import try_import_skimage
---> 21 from .kitti_utils import generate_depth_map
22 from .mono_dataset import MonoDataset
23
~\anaconda3\envs\mxnet\lib\site-packages\gluoncv\data\kitti\kitti_utils.py in <module>
10
11 import mxnet as mx
---> 12 from mxnet.util import is_np_array
13
14
ImportError: cannot import name 'is_np_array'
I've tried using the same files that work on Google Collaboratory but I still get that error. I've tried reinstalling gluon and all that stuff in all manners. No idea what's going on. For convenience I really need this to work.
I resolved this error by installing the compatible versions of mxnet and gluoncv.In my case I had installed mxnet with with gluoncv native, that resolved the error.

i just want to import spacy but i get the msgpack error

ImportError Traceback (most recent call last)
<ipython-input-1-76a01d9c502b> in <module>
----> 1 import spacy
~\Anaconda3\envs\nlp_course\lib\site-packages\spacy\__init__.py in <module>
8 from thinc.neural.util import prefer_gpu, require_gpu
9
---> 10 from .cli.info import info as cli_info
11 from .glossary import explain
12 from .about import __version__
~\Anaconda3\envs\nlp_course\lib\site-packages\spacy\cli\__init__.py in <module>
----> 1 from .download import download
2 from .info import info
3 from .link import link
4 from .package import package
5 from .profile import profile
~\Anaconda3\envs\nlp_course\lib\site-packages\spacy\cli\download.py in <module>
9
10 from ._messages import Messages
---> 11 from .link import link
12 from ..util import prints, get_package_path
13 from .. import about
~\Anaconda3\envs\nlp_course\lib\site-packages\spacy\cli\link.py in <module>
7 from ._messages import Messages
8 from ..compat import symlink_to, path2str
----> 9 from ..util import prints
10 from .. import util
11
~\Anaconda3\envs\nlp_course\lib\site-packages\spacy\util.py in <module>
25 # Import these directly from Thinc, so that we're sure we always have the
26 # same version.
---> 27 from thinc.neural._classes.model import msgpack
28 from thinc.neural._classes.model import msgpack_numpy
29
ImportError: cannot import name 'msgpack' from 'thinc.neural._classes.model' (C:\Users\salwa\Anaconda3\envs\nlp_course\lib\site-packages\thinc\neural\_classes\model.py)
The problem is with thinc, a dependency of spaCy, as you can see here: ImportError: cannot import name 'msgpack' from 'thinc.neural._classes.model'
Follow Ines's (a core developer of spaCy) suggestion that you can find here,
It looks like you might have ended up with conflicting installationds
and dependencies – for example, the latest version of spaCy, but an
older version of its dependency, Thinc. In cases like this, it often
helps to just start out with a clean environment and reinstall from
scratch.

Unable to import modules from 'math' in Python

I've hit this weird issue were I'm unable to import from the math module in Python. However importing the entire module works.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/bdhammel/Documents/research_programming/analysis_routines/visar_analysis.py in <module>()
16
17 from core import scopes as cscopes
---> 18 from core import plot as cplot
19 from core.plot import ImageData, IndexSelector
20 from core.math import savitzky_golay
/Users/bdhammel/Documents/research_programming/core_diag/core/plot.py in <module>()
10 sys.path.append(CORE_DIR)
11
---> 12 from core import math as cmath
13
14 class ImageData(object):
/Users/bdhammel/Documents/research_programming/core_diag/core/math.py in <module>()
3 import sys
4 import math
----> 5 from math import factorial
6 from scipy.integrate import cumtrapz
7
ImportError: cannot import name factorial
My directory structure is something like this
Main file
from math import factorial # works fine here
import custom_module
custom_module
import math # works fine
from math import factorial # breaks
I have no idea why this could be. Any help would be appreciated.
I'm working inside of a virtual environment.
If you want to import the built-in math inside core.math, turn on absolute imports so you don't get core.math instead:
from __future__ import absolute_import

Categories