i was trying to understand the following code:
# We use the python 3 syntax for print
from __future__ import print_function
This statement enables the python 3 syntax for print (See the python documentation for more details). I use this syntax so that this script can run with both Python 2 and python 3 without modifications.
# Our project file is in the data subdirectory of the directory containing
# the script, so we calculate the appropriate path.
scriptdir = os.path.dirname(__file__) # Directory of our script.
datadir = os.path.join(scriptdir, "Data") # Enter the subdirectory.
projectpath = os.path.join(datadir, "Ampel.project") # And add the project name.
This code calculates the location of the project "Ampel.project" relative to the directory where the script itsself is. It assumes that in the directory containing the script file there is a subdirectory "Data" containing the "Ampel.project" file. This just happens to be the directory layout I used for my internal test scripts.
but I recieve always the following error: NameError: name 'projects' is not defined
could u help please and tell me how to correct it?
"projects" is an object imported into the namespace for scripts running in CoDeSys, so it is only available if your script is being started in CoDeSys directly (either via the "Execute Script" menu command or button, or via the "--runscript" command line parameter.)
For modules imported by your scripts, this automatic injection does not take place because it would break some modules from the standard library. Simply add "from scriptengine import *" at the top of your module.
If you run the script from outside of CoDeSys (for example in the py.exe or ipy.exe interpreters, or an IDE like IDLE or Eric), it won't work, since the codesys environment is not available there.
But remember that you can start CoDeSys with the "--runscript" and "--NoUI" parameters to execute python scripts within CoDeSys. (See the documentation for more information.)
Check out the CODESYS store: http://store.codesys.com/CODESYS® a trademark of 3S-Smart Software Solutions GmbH
Inspiring Automation Solutions