user1234
user1234

Reputation: 155

AttributeError: 'module' object has no attribute 'Environment'

I am trying convert jpeg files to 'lmdb' format. But I got this error:

>>> import lmdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lmdb.py", line 25, in <module>
    write_images_to_lmdb('/home/anaca/ferjpg' , 'fer2013')
  File "lmdb.py", line 13, in write_images_to_lmdb
    env = lmdb.Environment(db_name, map_size=map_size)
AttributeError: 'module' object has no attribute 'Environment'

Things that I try :

 pip install lmdb
 sudo apt-get install liblmdv-dev

Here's the code:

import caffe
import lmdb
import os
import numpy as np
import matplotlib.pyplot as plt
from caffe.proto import caffe_pb2
from caffe.io import datum_to_array, array_to_datum
def write_images_to_lmdb(img_dir, db_name):
    for root, dirs, files in os.walk(img_dir, topdown = False):
        if root != img_dir:
            continue
        map_size = 64*64*3*2*len(files)
        env = lmdb.Environment(db_name, map_size=map_size)
        txn = env.begin(write=True,buffers=True)
        for idx, name in enumerate(files):
            X = mp.imread(os.path.join(root, name))
            y = 1
            datum = array_to_datum(X,y)
            str_id = '{:08}'.format(idx)
            txn.put(str_id.encode('ascii'), datum.SerializeToString())   
    txn.commit()
    env.close()
    print " ".join(["Writing to", db_name, "done!"])

write_images_to_lmdb('/home/anaca/ferjpg' , 'fer2013')

Upvotes: 0

Views: 5150

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64969

From your traceback it seems as if the script you are trying to run is named lmdb.py. Change its name to something else.

Otherwise, when Python sees import lmdb it assumes you mean your script rather than the lmdb module you have installed.

Upvotes: 1

Related Questions