11

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)

4
  • I'm confused - are you trying to import a named function (life_table) from a module (__init__.py) that only contains a global import? What does * refer to here? Commented Feb 22, 2019 at 20:16
  • I'm using * to import all the variables from cdc_life_tables Commented Feb 22, 2019 at 21:50
  • Where are you trying to execute that? Is that a valid name in that module? Commented Feb 22, 2019 at 21:53
  • I think it's a valid name. I'll add some more information above which may help Commented Feb 22, 2019 at 22:05

2 Answers 2

13

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 *
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I deleted "from cdc_life_tables import *" from init.py and then renamed the first file in the path and then used: from life_tables.cdc_life_tables import life_table
0

My __init__.py file looked like this

repo_root = os.path.abspath(os.path.dirname(__file__))
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

giving this error on repo_root .

Changing to

modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
repo_root = os.path.abspath(os.path.dirname(__file__))

solved it.

Annoying that you don't get some more informative error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.