Run python script in Codesys

  • eamonn

    eamonn - 2018-03-29


    I have a python script that sends a sms message to my phone when its run.
    I want this code to run when a led in codesys turns on.

    I can't figure out how to do this. I know that codesys can run python by using tools, scripting, execute script.

    I just want the code to run automatically to alert me of an alarm.

    Thanks for the help.

  • eschwellinger

    eschwellinger - 2018-03-30

    this sms you want to send on an alarm or IO triggered by the plc and send by the plc
    not in the CODESYS developmentsystem?
    Because this python scription here is all in the CODESYS developmentsystem not in the plc.

    If you need this for the plc side,
    this example here should show how this could work:
    (But this works only if the plc side could execute python...)
    l viewtopic.php?f=23&t=6247&p=19932#p19941 l


  • eamonn

    eamonn - 2018-04-05

    Thanks for the help.

    I am using the PLC to run the python code. It can run the code on it's own.

    I cant seem to get my code to run. I don't have the hardware to test yours but I used your example to make my code.
    It seems to run the code but it does not do anything. I have tested my python code on its own and it does work.

  • eschwellinger

    eschwellinger - 2018-04-07

    so on which plc do you execute this?


  • eamonn

    eamonn - 2018-04-11

    I am running it on a raspberry pi 3 model B.
    I am trying to have the single file run when a bool value = true.
    I can't seem to get it to work through Codesys though.


    Never mind I got it to work.

    Thanks for all the help though.

  • rKtiC

    rKtiC - 2018-04-15

    Could you specify how you made it work? I am trying to run a Python Script on a Raspberry 3 B with the _ and it does not do anything. Running the script via the command line works fine.
    I would appreciate any help.

  • eamonn

    eamonn - 2018-04-21

    Here is the code that made it work.

    This runs my python file sms_warning on the raspberry pi.

    Make sure you do this


    This can be done on the pi by using the command: sudo nano /etc/CODESYSControl.cfg

    hope this helps.

    IMG: Capture.PNG

  • eschwellinger

    eschwellinger - 2018-04-23

    one small remark:
    I would recommend to move from /etc/CODESYSControl.cfg to /etc/CODESYSControl_User.cfg:

    [SysProcess] Command=AllowAll

    This has the advanatage that on an runtime update ( for example from 3.5SP12 to 3.5SP13) this setting
    will be retained.


  • Peter.skokanek

    Peter.skokanek - 2018-06-12

    I tried to use your examples to run my python script. But there is no /var/opt/codesys directory. I can find my PlcLogic/Application directory only directly in root directory. Where is a problem. Is it possible to set it somewhere in development system?

  • eschwellinger

    eschwellinger - 2018-06-12

    you need to update the Pi runtime Version to have it in


  • jorgev

    jorgev - 2018-08-02

    Hi all,

    I would like to use this feature in Codesys (script lang) to transfer variable values between the PLC and a python script.

    First, I am trying to execute a simple python script using codesys with a RPI 3 B+ as PLC. I followed the example posted for the unicorn hat as reference. However, I do not have the hardware to test it so I developed a simple code "print()" to verify the code is running.
    Below the steps I have followed:
    1. Add my script as external file in my CODESYS program.

    1. Manually copy to the RPI my python script to the directory /var/opt/codesys/. I have also included
      in both files

    2. Download the Codesys Program to the RPI and run the program from CODESYS.

    Although I do not receive any error nor compilation or execution, I can not observe my code running. Is there any step I am missing or I should skip?

    Thanks in advance

    IMG: interpreter_py.PNG

  • agrobot

    agrobot - 2019-04-23

    I have the same problem, load the program well without errors, but do nothing, could someone solve this problem?

  • jar.pekar

    jar.pekar - 2019-09-27

    Hi gueys ,

    im still heaving problems with executing scripts.
    I did all the stuffs like .

    Im able only run commands like Echo xxx >>log.txt or reboot etc.

    If i run python command i get reply 01 , some general error.

    Can anyone help ? I looks like im in deadend . Last idea is reinstal RPI image.
    and that is what i dont wanna do now

    Thx lot for reply

  • tommy99

    tommy99 - 2019-12-12

    Hi guys, I've followed all your instructions but I can't visualize anything on my Adafruit dipsplay (ILI9341 driver).
    My python script is copied correctly from /var/opt/codesys/PlcLogic/Applicarion to /var/opt/codesys so I don't know what I'm doing wrong. Advices?

    P.S. obviously if I load my program from Python idle in my RPi, it works properly.

    EDIT: resolved. I tried to change "python" to "python3" in the code and it works!

  • epolicar

    epolicar - 2020-07-26

    Hi guys,
    I followed all the instructions in this thread about executing external python scripts from Codesys. I was successful in running a blink LED script. However, I am unable to run a script containing just a simple print('Hello World'). Any ideas?

  • alien

    alien - 2021-04-27

    Hi guys,

    I just started to use python script in codesys! My question is: is it possible to run a script in codesys and it executes the code as long as plc is running?
    For example, reading an string of integer value real-time.
    Thanks for your help in advance!


Log in to post a comment.