[英]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.