[Tutor] ImportError

Mats Wichmann mats at wichmann.us
Sun Nov 12 17:09:04 EST 2023


On 11/12/23 02:45, Tannu Dekeza wrote:
> Hi
> 
> *I'm a beginner at python programming. I have been trying to execute
> the below code and have been getting an import error for numpy. i am
> using jupyter notebook. i have reinstalled both python and jupyter
> notebook but the error always occurs when i restart a kernel. how can
> i solve this permanently *
> 
> 
> 
> import numpy as np
> import pandas as pd
> import math
> import xlsxwriter
> import requests
> 
> 
> 
> ImportError                               Traceback (most recent call last)
> Cell In[1], line 1----> 1 import numpy as np      2 import pandas as
> pd      3 import math
> 
> File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\__init__.py:171
>     169 from . import fft    170 from . import polynomial--> 171 from .
> import random    172 from . import ctypeslib    173 from . import ma
> 
> File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\random\__init__.py:180
>     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 ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\random\_pickle.py:1---->
> 1 from .mtrand import RandomState      2 from ._philox import Philox
>     3 from ._pcg64 import PCG64, PCG64DXSM
> 
> File numpy\\random\\mtrand.pyx:1, in init numpy.random.mtrand()
> 
> File bit_generator.pyx:40, in init numpy.random.bit_generator()
> ImportError: cannot import name randbits


There's a "randbits" in the Python standard library "secrets" module. 
When you get this sort of error (failing to import a name rather than a 
module), it usually means you're covering up the module it should have 
been imported from, although if that's the case I would have expected a 
slightly more complete error message.

Check if by any chance your project has a local secrets.py file or a 
secrets module.

It could be more complicated than this, but this would be the place to 
start.  Try just starting a plain Python interpreter and, at the >>> 
prompt, doing:

from secrets import randbits

and seeing if that gives you any weird messages.

The numpy and other data science toolkits have their own groups, you may 
be better placed asking there - it may be that they recognize this 
problem from something (like incorrect setting of search paths), in case 
they have local copies of  some kind of secrets module that needs to be 
kept clear of the Python one - I, at least, don't know anything about 
this world, *possibly* someone else here knowns.







More information about the Tutor mailing list