What does "ImportError: cannot import name randbits" mean? - python

The first cell of my jupyter notebook contains the libraries I want to import. For some reason when I run it receive the ImportError: cannot import name randbits. I have never seen this import error before and have already tried restarting the kernel and confirmed that all libraries were installed correctly. As anyone seen this before and know what to do about this error?
import numpy as np
import pandas as pd
import requests
import xlsxwriter
import math
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import numpy as np
2 import pandas as pd
3 import requests
File C:\pyver\py3.10.5\lib\site-packages\numpy\__init__.py:151, in <module>
149 from . import fft
150 from . import polynomial
--> 151 from . import random
152 from . import ctypeslib
153 from . import ma
File C:\pyver\py3.10.5\lib\site-packages\numpy\random\__init__.py:180, in <module>
126 __all__ = [
127 'beta',
128 'binomial',
(...)
176 'zipf',
177 ]
179 # add these for module-freeze analysis (like PyInstaller)
--> 180 from . import _pickle
181 from . import _common
182 from . import _bounded_integers
File C:\pyver\py3.10.5\lib\site-packages\numpy\random\_pickle.py:1, in <module>
----> 1 from .mtrand import RandomState
2 from ._philox import Philox
3 from ._pcg64 import PCG64, PCG64DXSM
File mtrand.pyx:1, in init numpy.random.mtrand()
File bit_generator.pyx:38, in init numpy.random.bit_generator()
ImportError: cannot import name randbits

I have been having the same issue all day. Finally figured out what solved my problem. Somehow anaconda3/Lib/secrets.py got overwritten. Numpy relies on files in this directory called random.py and secrets.py so if you have files with those names numpy will not load.
-I renamed my incorrect secrets.py file
-Found the secrets.py source code and recreated the file. Solved my issue. Hope it helps. The links below were the most beneficial for me.
People having similar issues with numpy:
https://github.com/numpy/numpy/issues/14860
Source code for secrets.py:
https://github.com/python/cpython/blob/3.7/Lib/secrets.py

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'.

cannot import name 'asanyarray'

I'm currently having trouble importing some simple packages.
I'm working on pycharm professional in a virtual environment.
just trying to load;
import pandas as pd
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.ensemble._iforest import _average_path_length
import shap
importError Traceback (most recent call last)
<ipython-input-1-5b7f1bf909af> in <module>
----> 1 import pandas as pd
2 import numpy as np
3 from sklearn.ensemble import IsolationForest
4 from sklearn.ensemble._iforest import _average_path_length
5 import shap
~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pandas\__init__.py in <module>
14
15 if missing_dependencies:
---> 16 raise ImportError(
17 "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
18 )
ImportError: Unable to import required dependencies:
numpy: cannot import name 'asanyarray' from 'numpy.core.multiarray' (C:\Users\James\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\numpy\core\multiarray.py
I've never come across this error before so don't know how to fix.

ImportError in Jupyter Notebook

I am trying import a module in Jupyter and it is not working:
import alyn
ImportError Traceback (most recent call last)
<ipython-input-8-8e9535ea4303> in <module>()
----> 1 import alyn
~\Anaconda3\envs\tracx\lib\site-packages\alyn\__init__.py in <module>()
1 """ Import required modules"""
----> 2 from deskew import *
3 from skew_detect import *
ImportError: No module named 'deskew'
I don't quite understand why, since the package in question has a correct init.py file:
whose contents are:
""" Import required modules"""
from deskew import *
from skew_detect import *
What am I missing?
P.S.
This is all taking place on Windows 10.
Well, I've figured it out!
Turns out that the package I was trying to import is written in Python 2 and its init file is using the relative import mechanism. However, I am working in Python 3 and relative import is no longer supported in it. The init file can be made to work in Python 3 by adding a . in both lines, like this:
""" Import required modules"""
from .deskew import *
from .skew_detect import *
I think this should be backward compatible with Python 2.

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.

About Graphlab library importing

In Ubuntu 14.04, I have installed Graphlab based on https://dato.com/download/install-graphlab-create-command-line.html and it seems to be working fine.
However, I receive this error when trying to use a recommender module:
import graphlab
from graphlab.recommender import ranking_factorization_recommender
In the first line, graphlab is imported without any error. However, the second line causes this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-34df81ffb957> in <module>()
----> 1 from graphlab.recommender import ranking_factorization_recommender
ImportError: No module named recommender
How can the problem be solved? Thanks
It's just a namespace issue. recommender actually lives in the `toolkits module, so this should work:
import graphlab
from graphlab.toolkits.recommender import ranking_factorization_recommender
Graphlab has already imported everything for you in their __init__.py file.
Just do:
from graphlab import ranking_factorization_recommender
from graphlab import <any_other_recommender>
Here is a snippet of graphlab.__init__.py file:
from graphlab.util import get_runtime_config
from graphlab.util import set_runtime_config
import graphlab.connect as _mt
import graphlab.connect.aws as aws
from . import visualization
import os as _os
import sys as _sys
if _sys.platform != 'win32' or \
(_os.path.exists(_os.path.join(_os.path.dirname(__file__), 'cython', 'libstdc++-6.dll')) and \
_os.path.exists(_os.path.join(_os.path.dirname(__file__), 'cython', 'libgcc_s_seh-1.dll'))):
from graphlab.data_structures.sgraph import Vertex, Edge
from graphlab.data_structures.sgraph import SGraph
from graphlab.data_structures.sarray import SArray
from graphlab.data_structures.sframe import SFrame
from graphlab.data_structures.sketch import Sketch
from graphlab.data_structures.image import Image
from graphlab.data_structures.sgraph import load_sgraph, load_graph
from graphlab.toolkits._model import Model, CustomModel
import graphlab.aggregate
import graphlab.toolkits
import graphlab.toolkits.clustering as clustering
import graphlab.toolkits.distances as distances
...

Categories