Problem with CoDeSys globals

Anonymous
2012-08-20
2012-08-20
  • Anonymous - 2012-08-20

    Originally created by: Matthias S.

    Hello Forum,

    my Python Script contains several .py Files to keep an good overview.
    In one File I added my Reporter classes for XML import and export, derived from ImportReporter and ExportReporter.

    class Reporter(ImportReporter):
    

    This file is imported by the "main" .py file which is called direct from CoDeSys.exe.
    When I start the "main" .py file with CoDeSys I get the following error in the other .py file:
    Global name ‘ImportReporter’ is not defined.
    I have a similar Problem with the enumeration OnlineChangeOption and the other CoDeSys globals.

    If I put the Reporter classes in the “main” python file which is called direct from CoDeSys.exe, all is fine.

    I tried to import the “_3S.CoDeSys” Namespace in the other .py files, but it doesn’t help.
    I have to use multiple .py files to keep an overview.
    The problem only happens if I use the special CoDeSys global Symbols in other .py files which I like to import.

    Thanks in advance for your help.

    Greetings,
    Matthias

     
  • Anonymous - 2012-08-20

    Originally created by: M.Schaber

    Hi, Matthias,

    Matthias S. hat geschrieben:
    my Python Script contains several .py Files to keep an good overview.
    In one File I added my Reporter classes for XML import and export, derived from ImportReporter and ExportReporter.

    class Reporter(ImportReporter):
    

    This file is imported by the "main" .py file which is called direct from CoDeSys.exe.
    When I start the "main" .py file with CoDeSys I get the following error in the other .py file:
    I have a similar Problem with the enumeration OnlineChangeOption and the other CoDeSys globals.
    If I put the Reporter classes in the “main” python file which is called direct from CoDeSys.exe, all is fine.
    I tried to import the “_3S.CoDeSys” Namespace in the other .py files, but it doesn’t help.
    I have to use multiple .py files to keep an overview.
    The problem only happens if I use the special CoDeSys global Symbols in other .py files which I like to import.
    Thanks in advance for your help.

    All the CoDeSys specific objects are provided via an built-in module called "scriptengine", so in your submodules, you could use one of the following constructs to access ImportReporter, OnlineChangeOption and the other objects:

    // Simple all-everything import:
    from scriptengine import *
    // Import only the needed objects:
    from scriptengine import ImportReporter, OnlineChangeOption
    // Import only the module itsself:
    import scriptengine 
    // and later 
    class Reporter(scriptengine.ImportReporter):
    

    Rationale:

    For the main script, an implicit "from scriptengine import *" is executed on script startup, to simplify the life of script authors. The reason is that those scripts are very likely to be written specifically for CoDeSys, and we wanted to reduce the burden of writing boilerplate code.

    However, performing such an implicit import on imported modules is dangerous: script authors import modules from the python standard library, as well as other existing 3rd-party modules from the internet. Fiddling in the module-internal namespaces by flooding them with CoDeSys objects may lead to strange misbehaviour and hard to track bugs or failures. So we decided to omit that implicit import for imported modules, you need to explicitly import the "scriptengine" module if you want to write CoDeSys aware python modules.

    I hope this answers your question.

    Happy Coding!
    Markus

     
  • Anonymous - 2012-08-20

    Originally created by: Matthias S.

    Hello M.Schaber,

    thanks for the quick and detailed response.
    Now all works fine!

    The implicit import of course makes sense.
    Most users will probably use only one python file.

    Greetings,
    Matthias

     
  • Anonymous - 2012-08-20

    Originally created by: M.Schaber

    Hi, Matthias,

    Matthias S. hat geschrieben:
    thanks for the quick and detailed response.
    Now all works fine!

    I'm glad I could help.

    Matthias S. hat geschrieben:
    The implicit import of course makes sense.
    Most users will probably use only one python file.

    Yes, it's always a tradeoff between beginners and power users.

    Markus

     

Log in to post a comment.