如何导入位于不同目录中的函数

时间:2014-08-29 23:40:20

标签: python import

我正在尝试从位于与当前目录不同的目录中的模块调用函数。

这是我正在使用的代码

c:\ commands包含一个包含函数parselog的文件nlog.py。我想导入这个功能     到其他目录c:\ applications

中的python文件中
def parselog(inFileDir = )

    ### This is in directory c:\commands which need to imported ####


c:\applications\pscan.py is the script which is calling / importing from the above file / directory

if __name__ == '__main__':
    #sys.path.append("C:/commands")
    sys.path.insert(0,'C:/commands')

from commands import nlog


def pscan (infileDir = ) 


    parselog(inFileDir=r'os.getcwd()') # I am calling the function here 






We are getting error 


NameError: global name 'parselog' is not defined


I am not sure if I am importing in a wrong way. The C:\commands folder contains _init_.py file. Please let me know what I am missing. 

2 个答案:

答案 0 :(得分:0)

在要导入的模块的文件夹中创建并清空__init__.py文件。

答案 1 :(得分:-1)

下面的代码应该可行。

import sys
sys.path.append(r'c:\commands')
import nlog
parselog(inFileDir=r'os.getcwd()')