简体   繁体   English

Python Azure WebJob导入错误-无法导入Python扩展模块

[英]Python Azure WebJob Import Error - Cannot import Python Extension Modules

I am trying to run a Python script using an Azure WebJob, and it works fine when I want to make requests and generate CSVs. 我正在尝试使用Azure WebJob运行Python脚本,当我要发出请求并生成CSV时,它运行良好。 I've successfully zipped all the packages that I need along with my script and have uploaded them to the Azure App_Data directory. 我已经成功压缩了所需的所有程序包以及脚本,并将其上载到Azure App_Data目录。

AzureWebJobDirectory

However, I also need to be able to connect to an SFTP site, and some of the packages required contain Python extension modules. 但是,我还需要能够连接到SFTP站点,并且所需的某些软件包包含Python扩展模块。 When I run the script locally. 当我在本地运行脚本时。 it works fine. 它工作正常。 However, when I run on Azure, I get a message saying "ImportError: cannot import name _bcrypt" 但是,当我在Azure上运行时,收到一条消息,提示“ ImportError:无法导入名称_bcrypt”

Here is my script: 这是我的脚本:

import sys, os
sys.path.append(os.path.join(os.getcwd(), "site-packages"))

import pysftp
import paramiko

hostname = 'host'
username='user'
password='pass'
port='port'

source = 'D:\\Home\\PunchData.csv'
destination = 'PunchData_Success.csv'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=hostname,port=port,username=username,password=password)

ftp_client=client.open_sftp()

ftp_client.chdir('uploads')

ftp_client.put(source,destination)

ftp_client.close()

Here is the full error message I receive: 这是我收到的完整错误消息:

[12/15/2018 00:36:26 > 00ceeb: SYS INFO] Status changed to Initializing
[12/15/2018 00:36:28 > 00ceeb: SYS INFO] Job directory change detected: Job file 'enum\LICENSE' exists in source directory but not in working directory.
[12/15/2018 00:37:03 > 00ceeb: SYS INFO] Run script 'run.py' with script host - 'PythonScriptHost'
[12/15/2018 00:37:03 > 00ceeb: SYS INFO] Status changed to Running
[12/15/2018 00:37:06 > 00ceeb: ERR ] Traceback (most recent call last):
[12/15/2018 00:37:06 > 00ceeb: ERR ]   File "run.py", line 1, in <module>
[12/15/2018 00:37:06 > 00ceeb: ERR ]     import pysftp
[12/15/2018 00:37:06 > 00ceeb: ERR ]   File "D:\local\Temp\jobs\triggered\FTPTest\5zbuguvp.pts\pysftp\__init__.py", line 12, in <module>
[12/15/2018 00:37:06 > 00ceeb: ERR ]     import paramiko
[12/15/2018 00:37:06 > 00ceeb: ERR ]   File "D:\local\Temp\jobs\triggered\FTPTest\5zbuguvp.pts\paramiko\__init__.py", line 22, in <module>
[12/15/2018 00:37:06 > 00ceeb: ERR ]     from paramiko.transport import SecurityOptions, Transport
[12/15/2018 00:37:06 > 00ceeb: ERR ]   File "D:\local\Temp\jobs\triggered\FTPTest\5zbuguvp.pts\paramiko\transport.py", line 90, in <module>
[12/15/2018 00:37:06 > 00ceeb: ERR ]     from paramiko.ed25519key import Ed25519Key
[12/15/2018 00:37:06 > 00ceeb: ERR ]   File "D:\local\Temp\jobs\triggered\FTPTest\5zbuguvp.pts\paramiko\ed25519key.py", line 17, in <module>
[12/15/2018 00:37:06 > 00ceeb: ERR ]     import bcrypt
[12/15/2018 00:37:06 > 00ceeb: ERR ]   File "D:\local\Temp\jobs\triggered\FTPTest\5zbuguvp.pts\bcrypt\__init__.py", line 25, in <module>
[12/15/2018 00:37:06 > 00ceeb: ERR ]     from . import _bcrypt
[12/15/2018 00:37:06 > 00ceeb: ERR ] ImportError: cannot import name _bcrypt
[12/15/2018 00:37:06 > 00ceeb: SYS INFO] Status changed to Failed
[12/15/2018 00:37:06 > 00ceeb: SYS ERR ] Job failed due to exit code 1

It seems to fail any time a Python extension module is imported. 每次导入Python扩展模块似乎都会失败。 I'm using a 32bit Python 2.7 environment on a Windows machine. 我在Windows计算机上使用32位Python 2.7环境。 I've read that for Azure functions, you need to utilize a Python wheel to replace Python extension modules, but I'm not sure how I would use a wheel for an Azure WebJob. 我已经读过,对于Azure函数,您需要使用Python轮子来替换Python扩展模块,但是我不确定如何将轮子用于Azure WebJob。 Uploading the wheel to the WebJob directory doesn't solve the issue. 将转盘上传到WebJob目录不能解决问题。

Any help would be much appreciated! 任何帮助将非常感激!

I figured out the solution. 我想出了解决方案。

Instead of uploading the Python packages in a zip file directly with the Python script into the WebJob, you can install the packages with pip in the WebApp. 您可以将带有pip的软件包安装在WebApp中,而不是直接使用Python脚本将zip文件中的Python软件包直接上传到WebJob中。 To do so, I first added a Handler Mapping in Application Settings to specify my Python version: 为此,我首先在“应用程序设置”中添加了“处理程序映射”以指定我的Python版本:

处理程序映射

I then opened the Kudu console 然后我打开Kudu控制台

库杜

Once I had the Kudu console open, I went to Site Extensions and made sure I had the version of Python installed that I wanted: 打开Kudu控制台后,我转到了网站扩展,并确保已安装所需的Python版本:

KuduSiteExtensions

I then went to Debug Console > CMD to open the command prompt, and installed all my required packages using pip: 然后,我去调试控制台> CMD打开命令提示符,并使用pip安装了所有必需的软件包:

Kudupipinstall

Once I had all the required packages installed, I was able to remove them from the WebJob directory. 一旦安装了所有必需的软件包,便可以从WebJob目录中删除它们。 The WebJob script is able to use the packages that were just installed using pip. WebJob脚本能够使用刚刚使用pip安装的软件包。 Once I ran it, I no longer had any issue with Azure being able to import Python extension modules. 运行它之后,Azure能够导入Python扩展模块不再有任何问题。

Hopefully this helps someone out there who runs into a similar issue. 希望这可以帮助遇到类似问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM