首页 > 解决方案 > 导入本地 .py 文件时如何确保导入包

问题描述

导入本地python文件时,如何确保也导入了必要的包?

例子:

我想从位于同一目录中的 pythonfile 导入 myfunction

from pythonfile import myfunction

但是 myfunction 使用库。如何确保正确导入库?

## this is pythonfile

import library

def myfunction():
    library.dosomething()

我想我可以将导入放在函数中,但我不想每次都导入包。

标签: pythonpackagepython-import

解决方案


推荐阅读