导入错误访问服务器时没有名为constant_time的模块 [英] Import error No module named constant_time while accessing server

查看:338
本文介绍了导入错误访问服务器时没有名为constant_time的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在Nifi ExecuteScript中导入模块的后续操作

我是python以及nifi的新手.我试图在ExecuteScript处理器中执行我的python脚本.

I am new to python as well as nifi. I am trying to execute my python script in ExecuteScript processor.

我要访问服务器.所以我用了paramiko客户.但是,当我运行处理器时,它在session.write()行显示导入错误,没有模块名为constant_time".虽然我在"/usr/local/lib/python2.7/dist-packages/"下有这个constant_time.py

I want to access a server. so i used paramiko client. But when i run the processor, it shows "Import error No module named constant_time" at line session.write(). Though i have this constant_time.py under "/usr/local/lib/python2.7/dist-packages/ "

我在sys.path中也有路径"/usr/local/lib/python2.7/dist-packages/".我还在模块目录"(Module Directory)属性中给出了此路径.

I have also the path "/usr/local/lib/python2.7/dist-packages/ " in sys.path. I have also given this path in the "Module Directory" property.

这是我的代码:

import json, pysftp, paramiko
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

class ModJSON(StreamCallback):
  def __init__(self):
    pass
  def process(self, inputStream, outputStream):

    text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    inputText = text.rstrip('\r\n')
    json_content = json.loads(inputText)
    body = ''
    try:
       body = json_content['id']['body']
       body_encoded = body.encode('utf-8')
    except (KeyError,TypeError,ValueError):
       body_encoded = ''

    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.load_system_host_keys()
    ssh_client.connect('server', username='xxx', password='xxxx')

    sftp_client = ssh_client.open_sftp()
    text_file = sftp_client.open ('/doc/body.txt', 'w')   
    text_file.write("%s"%body_encoded)
    text_file.close()
    outputStream.write(bytearray(json.dumps(body, indent=4).encode('utf-8')))

flowFile = session.get()
if (flowFile != None):
   flowFile = session.write(flowFile, ModJSON())
   flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
   session.transfer(flowFile, REL_SUCCESS)

任何帮助将不胜感激.

推荐答案

正如Matt Burgess在此答案中所述, ExecuteScript处理器使用 Jython ,而不是 Python ,因此编译模块不可用.正在进行有关如何/是否添加此功能的讨论.

As explained by Matt Burgess in this answer, the ExecuteScript processor uses Jython, not Python, so compiled modules are not available. There is ongoing discussion of how/if to add this feature.

这篇关于导入错误访问服务器时没有名为constant_time的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆