我正在尝试从控制台运行模块。我的目录结构是这样的:
我正在尝试使用以下命令p_03_using_bisection_search.py
从problem_set_02
目录中运行模块:
$ 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
文件,但是仍然无法正常工作。
eval(input(...
是2to3建议的。我今天要对我这样做。很高兴我没有遵循它的建议,使人感到盲目
eval(input...
可能不是一个好主意。我只是解析它,而不是为任意代码执行提供机会。