我想class从父目录中的一个目录中的 python 文件中导入一个。这是我当前的文件夹结构:
? run_simulation_from_parent.py
? __init__.py
?
????.vscode
? launch.json
? settings.json
?
????mod
? ? mods.py
? ? __init__.py
? ?
? ????__pycache__
? __init__.cpython-37.pyc
?
????sim
? ? run_simulation.py
? ? __init__.py
? ?
? ????__pycache__
? __init__.cpython-37.pyc
?
????__pycache__
__init__.cpython-37.pyc
Run Code Online (Sandbox Code Playgroud)
该文件mod/mods.py包含以下类:
class Objective:
"""Objective function class"""
def __init__(self, x):
self.x = x
Run Code Online (Sandbox Code Playgroud)
该文件sim/run_simulation.py包含:
from mod.mods import Objective
x = 5
obj = Objective(x)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,出现以下错误:
File "sim/run_simulation.py", line 1, in <module> …Run Code Online (Sandbox Code Playgroud)