簡體   English   中英

Python ImportError:無法導入名稱__init__.py

[英]Python ImportError: cannot import name __init__.py

我收到此錯誤:

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

當我嘗試運行此代碼(retirement_mc.py)時:

from cdc_life_tables import life_table

init .py看起來像這樣

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

和cdc_life_tables.py包含life_table,如下所示:

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')

我正在使用Spyder(Python 3.7)

用這行:

from cdc_life_tables import *

您的軟件包正在嘗試從自身 import * 您需要從當前包的cdc_life_tables 子模塊import * ,最簡單的方法是使用相對導入:

from .cdc_life_tables import *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM