'NameError: "global name 'ZIP_STORED' is not defined"

I'm using the zipfile module in python like this:

from zipfile import ZipFile

And I have a method that uses this library like this:

def compress(self, filename, data):
    inMemoryOutputFile = StringIO()
    zipFile = ZipFile(inMemoryOutputFile, 'w', ZIP_STORED)
    zipFile.writestr(filename, data)
    zipFile.close()
    inMemoryOutputFile.seek(0)
    return inMemoryOutputFile

But, why am I getting this error:

NameError: "global name 'ZIP_STORED' is not defined"?

Shouldn't ZIP_STORED be in the global namespace?



Solution 1:[1]

Just add

from zipfile import ZIP_STORED

to your imports, or replace your import with

from zipfile import *

to import all symbols from zipfile to the current namespace.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 clemens