[Tutor] How to create dictionaries loadable with import

Steven D'Aprano steve at pearwood.info
Wed Sep 25 01:16:12 CEST 2013


On Tue, Sep 24, 2013 at 04:33:20PM -0400, Treder, Robert wrote:
> Hi Python tutors,
> 
> I'm fairly new to Python.  I'm working with Python v2.7.4 and the nltk 
> package on a couple of text mining projects.  I create several 
> dictionaries that are pretty static. Will probably only be updated 
> every or month or every couple of months.  I want to turn those 
> dictionaries into loadable data sets prior to running a module which 
> uses them.  If I define several dictionaries, dict1, dict2 and dict3, 
> in a single module named myDict, I'd like to do
> 
> from myDict import *


Hopefully you'll actually use more meaningful names, but otherwise, this 
is perfectly doable.


> I've tried defining the dictionaries in a the myDict module as follows:
> 
> Dict1 = {}
> with open('file1, 'rb') as infile:
>     reader = csv.reader(infile, delimiter = ',')
>     for row in reader:
>         try:
>             Dict1[ row[1] ].append(row[0])
>         except:
>             Dict1[ row[1] ] = [ row[0], ]
> 
> Dict2 = {}
> with open('file2, 'rb') as infile:
>     reader = csv.reader(infile, delimiter = ',')
>     for row in reader:
>         try:
>             Dict2[ row[1] ].append(row[0])
>         except:
>             Dict2[ row[1] ] = [ row[0], ]


I can tell that these aren't the *actual* code you use, copy and pasted, 
not just because of the fake file names, but because of the SyntaxError 
that they both have. So the error that the code you show above is 
different from the error that you actually get.

 
> These are simple dictionary structures with no additional structure, 
> i.e., not embedded in classes or functions. The try/except sequence is 
> because some of the keys may be duplicated in the files and I want to 
> append the values rather than overwrite.

A minor point, it is better to avoid bare "except" since that can mask 
real errors. You should use "except KeyError" here. But other than that, 
the basic principle is fine.


> Now when I build the module with setup tools
> 
> python setup.py install -prefix=C:\PY_MODULES
> 
> it builds without error but I can't find the dictionaries when I load the module
> 
>     from myDict import *
> AttributeError: 'module' object has no attribute 'Dict1'

Please show us the complete traceback, not just the last line. Copy and 
paste the entire thing, starting with the line

Traceback (most recent call last)

all the way to the final error message.


> How can I make the dictionaries loadable using import?

Dicts are no different from any other object, be it classes or functions 
or strings, they are automatically importable. Without seeing the entire 
traceback, and your actual code, I can't be sure, but my *guess* is that 
your actual module looks something like this:


dict1 = {}
with open('file1', 'rb') as infile:
    reader = csv.reader(infile, delimiter = ',')
    for row in reader:
        try:
            Dict1[ row[1] ].append(row[0])
        except:
            Dict1[ row[1] ] = [ row[0], ]


Notice the difference is name between "dict1" and "Dict1"? Python, like 
most programming languages, is case-sensitive. As is English itself, of 
course -- as they say, capitalization is the difference between helping 
your Uncle Jack off a horse, and helping your uncle jack off a horse.

Anyway, that's my guess as to what's going wrong. Without seeing your 
actual code, and the actual traceback, that's as far as I can go.


-- 
Steven


More information about the Tutor mailing list