我正在尝试从位于与当前目录不同的目录中的模块调用函数。
这是我正在使用的代码
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.
答案 0 :(得分:0)
在要导入的模块的文件夹中创建并清空__init__.py
文件。
答案 1 :(得分:-1)
下面的代码应该可行。
import sys
sys.path.append(r'c:\commands')
import nlog
parselog(inFileDir=r'os.getcwd()')