[Fixed] Module 'numpy' has no attribute 'asscalar'

Hi,

I am trying to call a server function after loading an ascii file (size=1038KB) with the file loader button on the client side. All this code was working fine a few hours ago, but now it fails when making the call to the function with this error .

AnvilWrappedError: module ‘numpy’ has no attribute ‘asscalar’
at /home/anvil/.env/lib/python3.10/site-packages/numpy/init.py, line 311
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil/_server.py, line 983
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil/_server.py, line 934
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil/_server.py, line 940
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil/_server.py, line 934
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil/_server.py, line 999
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil/_serialise.py, line 133
called from /home/anvil/.downlink-sources/downlink-2022-09-02-10-37-54/anvil_downlink_worker/full_python_worker.py, line 49
called from Mainwells, line 78

I’ve tried loading different files and sizes without success. When I search for this error on google it states that this attribute ‘asscalar’ is deprecated on versions of numpy after versions 1.16. Does this mean that the server is now running a newer version of numpy and if so how do I get around this issue.

Code snippet that is casing there at line 78 is below.
Code Sample:

def create_lasWell_fileloader_change(self, file, **event_args):
    welldict = anvil.server.call('readlasfileheader',file)
    pass

Thanks for any help on this issue.

Peter

Clone link:
share a copy of your app

Hi @pboothby and welcome to the community.

It looks like the numpy.asscalar() function was deprecated in v1.16. I am not sure if Anvil has upgraded its numpy version but that might be the reason it no longer works.

From the numpy docs

Deprecated since version 1.16: Deprecated, use numpy.ndarray.item() instead.

Link to numpy.ndarray.item

Thanks @rickhurlbatt. Since posting I have done quite a bit more debugging and discovered that when the code gets to the ‘return’ line at the end of the sever function its throwing this error so not sure why its throwing a ‘numpy’ error, when there is no reference to numpy in my code. Trying to return a dictionary object which seems to be the issue.

1 Like

Thanks for reporting. Moved to bug reports.

This should now be fixed

2 Likes

My proyect was working has intended untill a week or so ago, it used ocr to get the characters of a plate number, but now i get this error: AnvilWrappedError: module ‘numpy’ has no attribute ‘asscalar’
I havent changed anything on that part of the code and I havent found what causes the error.
I tried to revert to other versions of numpy but still it doesnt work

Hi @davidgrimm95 - it looks like you’re experiencing this bug so I moved your post.

Where is your code running that is hitting this error?
I suspect it is in an uplink?
(I just retested this bug on anvil’s servers and it’s working as expected)

1 Like

I can confirm that error. If I return a dict from a server function it will somehow throw the same not related numpy error. I will try to get some more details…

I get the same error (?) and it does seem numpy related but is hard to fix because it has to do with the serilization that is going on. Not even sure if it is a bug but here goes;

I have this code on the server side;

class SoilProfile1(BaseModel):
    """one dimensional stack of soillayers"""

    soillayers: List[Soillayer] = []

...
soilprofile1 = SoilProfile1(
        soillayers=[
            Soillayer(
                top=round(sl.top, 2),
                bottom=round(sl.bottom, 2),
                soilname=sl.soilcode,
            )
            for sl in sp1.soillayers
        ]
    )

return soilprofile1.dict()

So I use Pydantic to convert the result using the BaseModel dict function. Each time it gave me the numpy has no attribute ‘asscaler’ error. Printing the dict showed that it looked fine;

{'soillayers': [{'top': -1.04, 'bottom': -2.24, 'soilname': 'preexcavated'}, {'top': -2.24, 'bottom': -6.04, 'soilname': 'nl_veen'}, {'top': -6.04, 'bottom': -6.24, 'soilname': 'nl_venige_klei'}, {'top': -6.24, 'bottom': -6.44, 'soilname': 'nl_zandige_klei'}, {'top': -6.44, 'bottom': -6.64, 'soilname': 'nl_kleiig_zand'}]}

The numpy error got me thinking that maybe this seemingly stupid code where is specifically convert the numbers to a float would perhaps solve the problem… and it did…

soilprofile1 = SoilProfile1(
        soillayers=[
            Soillayer(
                top=round(float(sl.top), 2),
                bottom=round(float(sl.bottom), 2),
                soilname=sl.soilcode,
            )
            for sl in sp1.soillayers
        ]
    )

return soilprofile1.dict()

So I guess you should somehow avoid to send numpy floats back in a dictionary. I do not even think it is a bug but I thought I’d mention it in case other people are confronted with the same error. This might even happen because you use a library that does some internal stuff and returns numpy floats instead of float.

(added an image with the error and some ‘debug’ stuff :slight_smile: )

complete error;

AttributeError: module 'numpy' has no attribute 'asscalar'
at /usr/local/lib/python3.10/dist-packages/numpy/__init__.py:311
called from /usr/local/lib/python3.10/dist-packages/anvil/_server.py:983
called from /usr/local/lib/python3.10/dist-packages/anvil/_server.py:934
called from /usr/local/lib/python3.10/dist-packages/anvil/_server.py:940
called from /usr/local/lib/python3.10/dist-packages/anvil/_server.py:934
called from /usr/local/lib/python3.10/dist-packages/anvil/_server.py:934
called from /usr/local/lib/python3.10/dist-packages/anvil/_server.py:999
called from /usr/local/lib/python3.10/dist-packages/anvil/_serialise.py:133
called from /usr/local/lib/python3.10/dist-packages/anvil/server.py:317
called from /usr/local/lib/python3.10/dist-packages/anvil/server.py:55
called from CptClassificationPage, line 46
1 Like

@breinbaasnl Could you confirm where this code is running?
If it’s using the uplink (which it seems to be from the logged output)
then the fix to the bug on this thread hasn’t yet made its way to the anvil-uplink package.

You’re right about the fix you’ve implemented.
Anvil has, for a long time, converted numpy floats to python floats before serializing them to the client, so that you didn’t have to.
The original code that did this used the (now deprecated) asscalar method.
We fixed that on Anvil’s own server code, but the fix hasn’t yet propagated to the anvil-uplink package.

I’ll flag that we need to push a new release of anvil-uplink.

1 Like

@stucork the code is running on a digital ocean droplet and using the anvil uplink package

from requirements.txt

anvil-uplink==0.4.0
1 Like

I’m going to preface this by saying what I am about to suggest is ill-advised, but if you absolutely have to get something to work and they have not pushed the patch to anvil-uplink yet, (The fix is also still not in the github anvil-runtime master repo) you could… Go to that line 983 in _server.py in your anvil uplink code and diy patch it from this:

_json = numpy.asscalar(_json)

to this:

try:
    _json = numpy.asscalar(_json)
except AttributeError:
    _json = _json.item()

…but you shouldn’t do that, you should wait for a fix if you can.

anvil-uplink 0.4.1 has been released, which fixes this bug.

2 Likes