python - ModuleNotFoundError : What does it mean __main__ is not a package?

标签 python module python-3.6 python-import

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

enter image description here

我正在尝试使用 problem_set_02 目录运行模块 p_03_using_bisection_search.py​​:

$ python3 p_03_using_bisection_search.py

p_03_using_bisection_search.py​​里面的代码是:

__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 中的函数,该函数的代码是:

__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()

我收到以下错误:

ModuleNotFoundError: No module named '__main__.p_02_paying_debt_off_in_a_year'; '__main__' is not a package

我不知道如何解决这个问题。我已经尝试添加一个 __init__.py 文件,但它仍然无法正常工作。

最佳答案

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

from p_02_paying_debt_off_in_a_year import compute_balance_after

关于python - ModuleNotFoundError : What does it mean __main__ is not a package?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41816973/

相关文章:

python - 处理Python中的按键错误

pip 安装模式得到 SyntaxError : Missing parentheses in call to 'print'

python - 增加移动平均线

python - Paramiko 错误 "missing_host_key() missing 1 required positional argument: ' key '”

javascript - 如何使用 "tumblr.js"NodeJS 模块将照片博客发布到 tumblr

ruby - 了解 self.including 在 ruby​​ 中

python - 使用 BeautifulSoup 通过 id 获取 div 的内容

python - 删除某些单词并合并表格

c# - 使用 WPF 构建高度模块化的业务应用程序?

python - 逐行读取文本文件并存储匹配 Python 中特定模式的变量