TypeError: 'module' object is not callable

Can anyone please help me to solve this error?
TypeError: ‘module’ object is not callable at , line 6 called from [Form1, line 19](javascript:void(0))

This is my Anvil Code:
results = anvil.server.call(‘unzip’,self.file_loader_1.file,self.text_box_1.text)

And this is my Python Code:
import zipfile as ZipFile
import anvil.media

@anvil.server.callable
def unzip(a,b):
with ZipFile(a, ‘r’) as zip_ref:
input = zip_ref.extractall(b)

Hi @fatimamahmood717

In your server code, you import the zipfile module and alias it as ‘ZipFile’

You then call that alias on line 6. As the error says, a module is not callable, so that fails.

I suspect your import should instead be:

from zipfile import ZipFile

Ohh! yes thank you:)

But now i got this error:

AttributeError: ‘StreamingMedia’ object has no attribute ‘seek’ at C:/ProgramData/Anaconda3/lib/site-packages/anvil/init.py, line 48 called from C:/ProgramData/Anaconda3/lib/zipfile.py, line 259 called from C:/ProgramData/Anaconda3/lib/zipfile.py, line 1321 called from C:/ProgramData/Anaconda3/lib/zipfile.py, line 1258 called from , line 6 called from [Form1, line 19](javascript:void(0))

You’ll need to share your code in order for anyone to have much chance of being able to help here.

2 Likes

Perhaps the library you are using needs a random access file, not a stream.

If that’s the case you could get the media object, save it to file (see here for an example), then feed the file to your library.

1 Like

Alternatively, if it’s small enough to fit in memory, you can try this approach:

What I’m trying to do:
is to upload a zip file using file loader but getting this error
image

Here is my python code:

from zipfile import ZipFile
import anvil.media

@anvil.server.callable
def unzip(a):
     with ZipFile(a, 'r') as zip:
        zip.extractall()

Clone link:
https://anvil.works/build#clone:MOMROK6DF7YCGUSM=HL7UHR3CB6ZYCE3GVOXOWXUL

I moved the above post here as they seem to be related to the same issue.

thanks for the clone @ayeshazahid913

Always worth checking the documentation here.

ZipFile expects the file name when used as a context manager (or a ZipInfo object):

And you’re passing it a media object.

The anvil docs are helpful here and show you how to convert an anvil media object to a file on disk with a filename.

Anvil Docs | Files on Disk

You can then combine the context managers as follows

from zipfile import ZipFile
import anvil.media

@anvil.server.callable
def unzip(zip_media_object):
     with anvil.media.TempFile(zip_media_object) as file_name, ZipFile(file_name, 'r') as myzip:
        myzip.extractall()

2 Likes

Thanks for your help :slightly_smiling_face: