lmi*_*asf 161 python module python-import python-3.6
我试图从控制台运行一个模块.我的目录结构是这样的:
我正在尝试运行模块p_03_using_bisection_search.py
,从problem_set_02
目录使用:
$ python3 p_03_using_bisection_search.py
Run Code Online (Sandbox Code Playgroud)
里面的代码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()
Run Code Online (Sandbox Code Playgroud)
我正在导入一个函数,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()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ModuleNotFoundError: No module named '__main__.p_02_paying_debt_off_in_a_year'; '__main__' is not a package
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题.我试过添加一个__init__.py
文件,但它仍然无法正常工作.
Mos*_*oye 179
只需删除相对导入的点,然后执行以下操作:
from p_02_paying_debt_off_in_a_year import compute_balance_after
Run Code Online (Sandbox Code Playgroud)
小智 70
我和你一样有同样的问题.我认为问题是你使用了相对导入in-package import
.__init__.py
您的目录中没有.所以只需按照摩西的回答进行导入.
我认为核心问题是当您使用点导入时,例如:
__main__
.
它相当于:
p_03_using_bisection_search.py
.
我们都知道,这p_03.py
是指您当前的模块p_03_using_bisection_search
.
问题出现了:
当解释器进入时p_02_paying_debt_off_in_a_year
,脚本等于:
main.py
显然,setup.py
不包含任何调用的模块或实例problem_set_02/
.
简而言之,解释器不知道您的目录体系结构.
所以我提出了一个更清晰的解决方案,而没有更改python环境的贵重物品(在查询请求在相对导入中的工作方式)之后:
该目录的主要架构是:
__init__.py
p01.py
---p02.py
------p03.py
------__init__.py
------__main__
------__init__
然后写下problem_set_02
:
from .p_02_paying_debt_off_in_a_year import compute_balance_after
Run Code Online (Sandbox Code Playgroud)
这main.py
是import problem_set_02
,它正是指模块setup.py
.
然后去in-package import
:
__init__.py
您还可以编写一个__main__
以向环境添加特定模块.
小智 6
只需使用 .py 文件所在的主文件夹的名称。
from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after
Run Code Online (Sandbox Code Playgroud)
删除文件开头的点并导入absolute_import
from __future__ import absolute_import
from p_02_paying_debt_off_in_a_year import compute_balance_after
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
185710 次 |
最近记录: |