首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ModuleNotFoundError:__main__不是一个包是什么意思?

ModuleNotFoundError:__main__不是一个包是什么意思?
EN

Stack Overflow用户
提问于 2017-01-24 06:40:48
回答 4查看 309.5K关注 0票数 250

我正在尝试从控制台运行一个模块。我的目录结构是这样的:

我正在尝试使用以下命令从problem_set_02目录运行模块p_03_using_bisection_search.py

代码语言:javascript
复制
$ python3 p_03_using_bisection_search.py

p_03_using_bisection_search.py中的代码是:

代码语言:javascript
复制
__author__ = 'm'


from .p_02_paying_debt_off_in_a_year import compute_balance_after


def compute_bounds(balance: float,
                   annual_interest_rate: float) -> (float, float):

    # there is code here, but I have omitted it to save space
    pass


def compute_lowest_payment(balance: float,
                           annual_interest_rate: float) -> float:

    # there is code here, but I have omitted it to save space
    pass    

def main():
    balance = eval(input('Enter the initial balance: '))
    annual_interest_rate = eval(input('Enter the annual interest rate: '))

    lowest_payment = compute_lowest_payment(balance, annual_interest_rate)
    print('Lowest Payment: ' + str(lowest_payment))


if __name__ == '__main__':
    main()

我正在导入一个p_02_paying_debt_off_in_a_year.py中的函数,它的代码是:

代码语言:javascript
复制
__author__ = 'm'


def compute_balance(balance: float,
                    fixed_payment: float,
                    annual_interest_rate: float) -> float:

    # this is code that has been omitted
    pass


def compute_balance_after(balance: float,
                          fixed_payment: float,
                          annual_interest_rate: float,
                          months: int=12) -> float:

    # Omitted code
    pass


def compute_fixed_monthly_payment(balance: float,
                                  annual_interest_rate: float) -> float:

    # omitted code
    pass


def main():
    balance = eval(input('Enter the initial balance: '))
    annual_interest_rate = eval(
        input('Enter the annual interest rate as a decimal: '))
    lowest_payment = compute_fixed_monthly_payment(balance,
                                                   annual_interest_rate)
    print('Lowest Payment: ' + str(lowest_payment))


if __name__ == '__main__':
    main()

我收到以下错误:

代码语言:javascript
复制
ModuleNotFoundError: No module named '__main__.p_02_paying_debt_off_in_a_year'; '__main__' is not a package

我不知道如何解决这个问题。我尝试添加一个__init__.py文件,但它仍然不起作用。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-01-24 06:44:45

只需删除相对导入的点,然后执行以下操作:

代码语言:javascript
复制
from p_02_paying_debt_off_in_a_year import compute_balance_after
票数 288
EN

Stack Overflow用户

发布于 2018-07-18 15:39:47

尝试以如下方式运行:

python3 -m p_03_using_bisection_search

票数 12
EN

Stack Overflow用户

发布于 2019-09-06 00:35:53

删除文件开头的点并导入absolute_import

代码语言:javascript
复制
from __future__ import absolute_import

from p_02_paying_debt_off_in_a_year import compute_balance_after
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41816973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档