CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Thu Jun 20, 2019 10:00 am

All times are UTC+01:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Aug 20, 2012 10:12 am 
Offline

Joined: Wed Apr 11, 2012 9:54 am
Posts: 13
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.
Code:
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


Top
   
PostPosted: Mon Aug 20, 2012 10:40 am 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, Matthias,

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

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:

Code:
// 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

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
PostPosted: Mon Aug 20, 2012 12:38 pm 
Offline

Joined: Wed Apr 11, 2012 9:54 am
Posts: 13
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


Top
   
PostPosted: Mon Aug 20, 2012 12:45 pm 
Offline
Frequent User
Frequent User
User avatar

Joined: Fri Mar 18, 2011 4:12 pm
Posts: 266
Hi, Matthias,

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


I'm glad I could help. :D

Matthias S. wrote:
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. :wink:

Markus

_________________
Check out the CODESYS store: http://store.codesys.com/

CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited