Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ImportError: cannot import name __init__.py

Tags:

python

import

I'm getting this error:

ImportError: cannot import name 'life_table' from 'cdc_life_tables' (C:\Users\tony\OneDrive\Documents\Retirement\retirement-mc-master\cdc_life_tables\__init__.py)

When I try to run this (retirement_mc.py):

from cdc_life_tables import life_table

__init__.py looks like this

#!/usr/bin/env python
from cdc_life_tables import *

and cdc_life_tables.py contains life_table and looks like this:

def life_table(state_abbrev, demographic_group):
  
    state_abbrev = state_abbrev.upper()

    try:
        state = abbrev2name[state_abbrev]
    except KeyError:
        raise ValueError('"{}" not a state abbreviation.'.format(state_abbrev))

    state = state.lower().replace(' ', '_')


    try:
        demographic_group = demographic_group.lower()
        if len(demographic_group) > 2:
           demographic_group = groups_long2short[demographic_group]
    except KeyError:
        raise ValueError('"{}" not a valid .'.format(demographic_group))
        
    s = '{}{}_{}.csv'.format(lt_dir, state, demographic_group)

    if os.path.exists(s):
        df = pd.read_csv(s)
    else:
        raise ValueError('{} not a demographic group for {}.'.format(demographic_group, state_abbrev))

    return df['qx']
       
if __name__ == '__main__':
    q = life_table('PA', 'wf')

I'm using Spyder (Python 3.7)

like image 527
Simone Avatar asked Feb 22 '19 20:02

Simone


1 Answers

With this line:

from cdc_life_tables import *

your package is attempting to import * from itself. You need to import * from the cdc_life_tables submodule of the current package, most easily done with a relative import:

from .cdc_life_tables import *
like image 99
user2357112 supports Monica Avatar answered Oct 18 '22 10:10

user2357112 supports Monica