我已成立了一个项目目录如下:Python 3.6正确的方式在项目目录中导入?
project/
src/
__init__.py
data/
__init__.py
pull_data.py
format_data.py
update_data.py
model/
__init__.py
train.py
predict.py
我会做的主要事情是运行train.py
和predict.py
这两个更新,然后我的训练数据分别预测。我也希望能够独立运行其他python文件,如果需要的话。
我的问题是,这是一个适当的方式来设置我的项目目录,我该如何导入文件?
蟒蛇文件看起来是这样的:
# update_data.py
from pull_data import pull_data # how? this is in same directory
from format_data import format_data # how? this is in same directory
def update_data():
pull_data() # this is in the same directory
format_data() # this is in the same directory
# other stuff
if __name__ == '__main__':
update_data()
,
# train.py
from update_data import update_data # how? this is in ../data
def train():
update_data()
# other stuff
if __name__ == '__main__':
train()
,
# predict.py
from update_data import update_data # how? this is in ../data
def predict():
update_data()
# other stuff
if __name__ == '__main__':
predict()
我也是,我应该从src
运行代码的前提下,目录像python model/train.py
。我也愿意改进。
您当前的配置是否有效? – wwii
[在python中的绝对导入不工作,相对导入工作](https://stackoverflow.com/questions/45448182/absolute-imports-in-python-not-working-relative-imports-work) –
With current运行'python model/train.py'的配置会导致'ModuleNotFoundError:No module named'update_data''。 – PL3