我有一个存储所有.py文件的目录.
bin/
main.py
user.py # where class User resides
dir.py # where class Dir resides
Run Code Online (Sandbox Code Playgroud)
我想在main.py中使用user.py和dir.py中的类.
如何将这些Python类导入main.py?
此外,User如果user.py位于子目录中,如何导入类?
bin/
dir.py
main.py
usr/
user.py
Run Code Online (Sandbox Code Playgroud) 我试图从控制台运行一个模块.我的目录结构是这样的:
我正在尝试运行模块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 …Run Code Online (Sandbox Code Playgroud) 目前正在尝试使用Python3并使用绝对导入将一个模块导入另一个模块但我收到错误ModuleNotFoundError: No module named '__main__.moduleB'; '__main__' is not a package.考虑这个项目结构:
proj
__init__.py3 (empty)
moduleA.py3
moduleB.py3
Run Code Online (Sandbox Code Playgroud)
moduleA.py3
from .moduleB import ModuleB
ModuleB.hello()
Run Code Online (Sandbox Code Playgroud)
moduleB.py3
class ModuleB:
def hello():
print("hello world")
Run Code Online (Sandbox Code Playgroud)
然后运行python3 moduleA.py3给出错误.这里有什么需要改变的?
谢谢!