从另一个python文件导入函数

时间:2018-05-26 04:27:07

标签: python python-3.x

my_main_file.py:

from bfile import bfunc
def fna():
  bfunc(sth)

if __name__ == "__main__":
  fna()

bfile.py:

def bfunc(sth):
  #bla bla..

错误:

  

name' bfunc'未定义

两个文件都在同一目录下

PS: 我在这里尝试过everythig Call a function from another file in Python

3 个答案:

答案 0 :(得分:1)

__init__.py python文件添加到当前工作目录中。所以python会将目录视为包含包。您可以看到文档here

答案 1 :(得分:0)

尝试

import bfile
def fna():
    bfile.bfunc(sth)

答案 2 :(得分:0)

添加export PYTHONPATH="."bash_profile解决了问题