Unable to import modules from 'math' in Python - 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

Related

Problem with StringIO importing ee, although it could be imported alone

I am trying to import the module ee to use the google earth engine according to the documentation on the dedicated website.
https://developers.google.com/earth-engine/guides/python_install
I got this error:
import ee
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_35721/2985164896.py in <module>
----> 1 import ee
~/anaconda3/lib/python3.7/site-packages/ee/__init__.py in <module>
----> 1 from .main import main
~/anaconda3/lib/python3.7/site-packages/ee/main.py in <module>
8 import stat
9 import plistlib
---> 10 import StringIO
11 import platform
12 import time
ModuleNotFoundError: No module named 'StringIO'
The puzzling thing is that I can import this module without ee flawlessly.
from io import StringIO
import io
Would anyone had the same issue?
Modifying the original python file at the following location solve the problem:
~/anaconda3/lib/python3.7/site-packages/ee/main.py
Line 10, replace 'import StringIO' by 'from io import StringIO'.

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

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

Scipy cannot import median_abs_deviation

I'm using a program called galpro which uses may packages, including scipy. When I use the command
import galpro as gp
I am then given the error:
ImportError Traceback (most recent call last)
<ipython-input-7-6eb7571a326c> in <module>
----> 1 import galpro as gp
~/galpro/galpro/__init__.py in <module>
----> 1 from .model import Model
~/galpro/galpro/model.py in <module>
8 import joblib
9 import h5py
---> 10 from .validation import Validation
11 from .plot import Plot
12 from .utils import convert_1d_arrays, create_directories
~/galpro/galpro/validation.py in <module>
2 import numpy as np
3 import h5py
----> 4 from .plot import Plot
5 from .utils import load_posteriors, create_templates
6 from .conf import set_plot_params
~/galpro/galpro/plot.py in <module>
4 import seaborn as sns
5 import statsmodels.api as sm
----> 6 from .utils import *
7 from .conf import set_plot_params
8
~/galpro/galpro/utils.py in <module>
3 import h5py
4 import numpy as np
----> 5 from scipy.stats import entropy, kstest, median_abs_deviation, cramervonmises
6
7
ImportError: cannot import name 'median_abs_deviation' ---------------------------------------------------------------------------
I have tried reinstalling scipy and this didn't help. According to documentation, the median_abs_deviation module should work with scipy 1.3.0 onwards, and I'm using 1.5.4. I have also tried reinstalling different versions of scipy and this has not helped either.

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

python from . import fails

Trying caffe python examples from: http://caffe.berkeleyvision.org/tutorial/interfaces.html gives me error:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
caffe_root = '/opt/caffe'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-18cb333d5c1b> in <module>()
7 sys.path.insert(0, caffe_root + 'python')
8
----> 9 import caffe
...
...
/usr/lib/python2.7/site-packages/scipy/signal/__init__.py in <module>()
272 from __future__ import division, print_function, absolute_import
273
--> 274 from . import sigtools
275 from .waveforms import *
276 from ._max_len_seq import max_len_seq
ImportError: cannot import name sigtools
Apparently the sigtools import fails, but I can't figure out why. The /usr/lib/python2.7/site-packages/scipy/signal contains all files:
$ ls -1 /usr/lib/python2.7/site-packages/scipy/signal/sign*
/usr/lib/python2.7/site-packages/scipy/signal/signaltools.py
/usr/lib/python2.7/site-packages/scipy/signal/signaltools.pyc
In general, how python process directives like this, specifically what dot is resolved to if my working directory was completely different from the location where sigtools package is located?
from . import sigtools
As stated here:
`from ... import` vs `import .`
"from . import sigtools" imports the main module "." (which is "signal") than imports the object/module sigtools. If "." has been already imported it rely on that structure.
I think that this can be tricky in case you have 2 modules with the same name in the python import path: the interpreter imports the first one found and never imports the second one. If the second one has more modules than the first one, this can lead to something similar to your problem.

Categories