Timm
Timm

Reputation: 212

Publish azure functionapp using python in PyCharm

I have an azure function app which I use for simple testing purpose. 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.

Therefore I os.chdir() into the corresponding folder and then try to run the publish command.

If I run the publish command manually (func azure functionapp publish <name> --python --build remote) everything works fine. However, If I call the command from within PyCharm I always get the error (here truncated)

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

My python code looks like this:

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. 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:

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

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.

Upvotes: 1

Views: 2599

Answers (2)

Timm
Timm

Reputation: 212

As it turns out, PyCharm sets an environmental variable called 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.

In order to successfully execute the build process, it can be deleted from within the python script prior to running the subprocess:

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')

Upvotes: 1

suziki
suziki

Reputation: 14080

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.)

Upvotes: 0

Related Questions