Running a PreBuild python script

Hi,

you can run a python script from Automation Studio via the Pre- or Postbuild mechanism.

For that, I created a simple batchfile for Automation Studio to call:

Description here

Batchfile:
I created an virtual environment (venv) for the python script. That way, everyone can compile the project even without having python installed on the machine.

@echo off
call %1\Logical\source\tools\venv\Scripts\activate.bat
python %1\Logical\source\tools\build_cam.py %1
deactivate

first I activate the venv, then I call the pyhtonscript
afterwards the venv is deactivated again

The Script reads and, if necessary, changes an “.axisfeature” File with a configured CAM Automat. If there are no changes, the file is not rewritten.

A simple “print()” within the pythonscript lets you print messages into the “Output Result” Windows in AS.

5 Likes

Python venvs are not portable → If someone else uses a different location the virtual environment won’t work anymore.

To be a little bit more “independent” I’m sticking to PowerShell :slight_smile:
Everyone using AS has PowerShell :stuck_out_tongue:

Best regards,

3 Likes

Well, would have been nice… fortunately I dont have to care about everyone :sweat_smile:

I’m still able to make the project compilable for “outsiders” like B&R Support…

1 Like

Hi,

I tried changing the “activate.bat” in the venv folder:

“VIRTUAL_ENV” was the absolute Path to my Project.
I tried calling the .bat in my Batchfile with the Project-Folder Parameter.
It seems to work, I copy-pasted the project to a new location and changed the path of the original one…

the compile runs through and I can see that it runs in the copied venv (by printing “sys.prefix”)

Maybe I will run into problems again if I start including specific packages into my venv…

But thanks for the tip anyway, helped a lot!

Hi,

A tips I can add to your post is that if you want to avoid virtual environnement of python you can compile your python script to executable file using “Pyinstaller” module of python.

Here is the documentation : PyInstaller Manual — PyInstaller 6.9.0 documentation

You can embed the executable to the project source and in pre/post build step you just have to add the path to the executable :
image

Hope this can help !

Regards,
Florent

5 Likes

Update:
It did not work as planned,

I now simply check whithin the batchfile if there is a python installed and if not I dont execute the “*.py” File.

Thank all for the helpfull discussion!

1 Like

Hi Stefan

Thanks for your explanation. We are also using python prebuild scripts.

Did you know, that you could use the logging module instead of “print()”, to create the Output Results. This is very helpful if you also like to generate “warnings” or “Errors”.

import logging
logging.basicConfig(level=logging.DEBUG)

# logging.info('message')
# logging.warning('warning')
# logging.error('error')

# EXAMPLE:
try:
    # do something
except Exception as e:
    logging.error(e) 

BR
Christoph

2 Likes