简体   繁体   English

在 PyCharm 中使用 python 发布 azure functionapp

[英]Publish azure functionapp using python in PyCharm

I have an azure function app which I use for simple testing purpose.我有一个用于简单测试目的的 azure 函数应用程序。 In order to (semi-)automatically run some tests, I want to be able to automatically upload a set of function apps to azure from within a python script.为了(半)自动运行一些测试,我希望能够从 python 脚本中自动将一组函数应用上传到 azure。

Therefore I os.chdir() into the corresponding folder and then try to run the publish command.因此我将os.chdir()放入相应的文件夹,然后尝试运行发布命令。

If I run the publish command manually ( func azure functionapp publish <name> --python --build remote ) everything works fine.如果我手动运行发布命令( func azure functionapp publish <name> --python --build remote )一切正常。 However, If I call the command from within PyCharm I always get the error (here truncated)但是,如果我从 PyCharm 中调用命令,我总是会收到错误消息(此处已截断)

The format of value 'Bearer eyJ0eXAiO...
' is invalid

My python code looks like this:我的python代码如下所示:

import subprocess
import shutil

try:
    result = subprocess.run([
        shutil.which('func'),
        'azure', 'functionapp',
        'publish', azure_function_app_resource_name,
        '--python',
        '--build remote'
    ], text=True)
    if result.returncode > 0:
        print('Failed')
    else:
        print('OK')
except FileNotFoundError:
    print('Failed')

Before executing this I make sure the azure cli / core functions are available by running and evaluating the output of the following statements.在执行此操作之前,我通过运行和评估以下语句的输出来确保 azure cli / core 函数可用。 This works flawlessly.这完美无缺。

subprocess.run([shutil.which('az'), '--version'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run([shutil.which('func'), '--version'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

I can also get the appsettings of the function app without any problem in python, so it is not a problem of not being logged in:我在python中也可以毫无问题的拿到函数app的appsettings,所以不是没有登录的问题:

result = subprocess.run([
    shutil.which('az'), 'functionapp', 'config', 'appsettings', 'list',
    '--name', azure_function_app_resource_name,
    '--resource-group', azure_resource_group_name
], capture_output=True, text=True)

Update : This is on Windows 10, Python 3.7, PyCharm 2019.3.4更新:这是在 Windows 10、Python 3.7、PyCharm 2019.3.4 上

Does anyone have an idea or working code?有没有人有想法或工作代码?

Note : I am aware that azure.core.cli exists, however, it is badly documented and does not help with the azure core function tools.知道azure.core.cli存在,但是,它严重记录,并没有帮助与湛蓝的核心功能的工具。

As it turns out, PyCharm sets an environmental variable called PYCHARM_HOSTED .事实证明,PyCharm 设置了一个名为PYCHARM_HOSTED的环境变量。 My inquiries suggest that it is used to identify whether a script is run from within PyCharm, but apparently it somehow influences the publishing process.我的查询表明它用于识别脚本是否在 PyCharm 中运行,但显然它以某种方式影响了发布过程。

In order to successfully execute the build process, it can be deleted from within the python script prior to running the subprocess:为了成功执行构建过程,可以在运行子过程之前从 python 脚本中删除它:

import subprocess
import shutil
import os

if 'PYCHARM_HOSTED' in os.environ:
    # try/except would be more pythonic, but longer
    del os.environ['PYCHARM_HOSTED']
try:
    result = subprocess.run([
        shutil.which('func'),
        'azure', 'functionapp',
        'publish', azure_function_app_resource_name,
        '--python',
        '--build remote'
    ], text=True)
    if result.returncode > 0:
        print('Failed')
    else:
        print('OK')
except FileNotFoundError:
    print('Failed')
import os
import subprocess
print(u'Test Beginning!')
input('input:')
os.system('func azure functionapp publish yourfunctionname --force')
input('input:')
print(u'Test Endding!')

This code works fine on my side, please have a try.(run the script in the function folder.)这段代码在我这边工作正常,请试一试。(运行函数文件夹中的脚本。)

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

相关问题 使用 Python 将 DataFrame 转换为 Azure Functionapp 中的 Parquet - Convert DataFrame to Parquet in Azure Functionapp using Python 如何使用 python SDK 获取 Azure FunctionApp 列表 - How to get Azure FunctionApp list using python SDK 在开发操作部署期间“不是 vso 映像,因此不编写构建命令”:$func azure functionapp publish。 Python - "Not a vso image, so not writing build commands" during dev ops deploy : $func azure functionapp publish. python Encountered the “Error calling sync triggers (TooManyRequests)” error when running “func azure functionapp publish” for a Python Function App in Azure - Encountered the “Error calling sync triggers (TooManyRequests)” error when running “func azure functionapp publish” for a Python Function App in Azure “ func azure functionapp发布”返回错误代码400 - “func azure functionapp publish” returns error code 400 PyCharm 中的 Azure 函数本地设置并发布到 Azure - Azure function local setup in PyCharm and publish to Azure FunctionApp 未导入 python 模块 - FunctionApp not importing python module 在venv和PyCharm中使用Python - Using Python with venv and PyCharm 在 Python Pycharm 中使用样式表 - Using Stylesheet in Python Pycharm Python Azure 函数远程应用程序发布错误 - Python Azure Function Remote App publish error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM