Matthias S. wrote:
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.
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.
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:
// and later
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.
Check out the CODESYS store: http://store.codesys.com/CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions