1

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?

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
user8563636
  • 163
  • 1
  • 5
  • 12
  • may be duplicate of https://stackoverflow.com/questions/28247374/importerror-no-module-named-parse?rq=1 – Jayesh Bhoi Sep 14 '17 at 05:24
  • Possible duplicate of [ImportError: No module named parse](https://stackoverflow.com/questions/28247374/importerror-no-module-named-parse) – SphynxTech Sep 14 '17 at 06:35
  • https://stackoverflow.com/questions/15746675/how-to-write-a-python-module#15747198 – Martin B. Sep 14 '17 at 07:00

2 Answers2

1

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.

Astik Anand
  • 12,757
  • 9
  • 41
  • 51
0

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
PAR
  • 624
  • 1
  • 5
  • 16