user8563636
user8563636

Reputation: 143

ImportError: No module named 'parse'

I got an error,

ImportError: No module named 'parse' .

In parse.py I wrote

class DataRate():
    data_rate ={}
    data_rate =defaultdict(dict)
    def try_to_int(arg):
        try:
            return int(arg)
        except:
            return arg

    book4 = xlrd.open_workbook('./data/excel1.xlsx')
    sheet4 = book4.sheet_by_index(0)

    tag_list = sheet4.row_values(0)
    for row_index in range(0, sheet4.nrows):
        row = sheet4.row_values(row_index)
        row = list(map(try_to_int, row))
        value = dict(zip(tag_list, row))

        closing_rate_dict[value['ID']].update(value)
        user = User.objects.filter(corporation_id=closing_rate_dict[value['ID']]['NAME'])

I wanna user DataRate class in data_rate_savedata.py,so I wrote

import parse
def data_rate_save():
   if user2:
      if parse.closing_rate_dict[parse.value['ID']]['NAME'] == 'A':
         parse.user2.update(data_rate_under_500 = parse.closing_rate_dict[parse.value['ID']]['d500'],
                            data_rate_under_700 = parse.closing_rate_dict[parse.value['ID']]['d700'],
                            data_rate_upper_700 = parse.closing_rate_dict[parse.value['ID']]['u700'])

I wanna use parse.py & data_rate_savedata.py in main_save.py,so I wrote

from app.parse import DataRate

#parse
DataRate()
#save
data_rate_save()

When I run main_save.py,I got an error ImportError:

 No module named 'parse',traceback says
File "/Users/app/data_rate_savedata.py", line 1, in <module>
    import parse

is wrong.Is it IDE problem?Or am I wrong to write codes?How can I fix this error?

Upvotes: 1

Views: 5558

Answers (2)

Astik Anand
Astik Anand

Reputation: 13047

You need to specify the path from where you are importing parse,

If it is as the same level as of data_rate_savedata.py write:

from . import parse

Or else you can write:

from app import parse

And this will work fine.

Upvotes: 1

PAR
PAR

Reputation: 654

In Python, modules are accessed by using the import statement. So, it is very important to understand the folder hierarchy.

Everything is working as expected if your folder structure is as follows.

-->Users
    -->app
         --> parse.py
         --> data_rate_savedata.py
         --> __init__.py
    -->main_save.py

Upvotes: 0

Related Questions