<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/</id><updated>2020-02-27T14:54:59.353000Z</updated><subtitle>Recent posts to Python and CoDeSys</subtitle><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#57cf" rel="alternate"/><published>2020-02-27T14:54:59.353000Z</published><updated>2020-02-27T14:54:59.353000Z</updated><author><name>aoj</name><uri>https://forge.codesys.com/u/aoj/</uri></author><id>https://forge.codesys.comfcc573bb21f3853d87b886641334509cf335093f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;That is what i was searching for. Endless THANKS &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#aee2" rel="alternate"/><published>2020-02-27T14:54:58.739000Z</published><updated>2020-02-27T14:54:58.739000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.comdfb96351e12e9f8daa41803f2d8ccf07415f1e56</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; M.Schaber&lt;/p&gt;
&lt;p&gt;Hi, aoj,&lt;/p&gt;
&lt;p&gt;The Project itsself also has an export_native method, which allows to pass a list of objects to export, as well as an recursive parameter to include child objects.&lt;/p&gt;
&lt;p&gt;Simply exporting the whole project could be done with a command like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;proj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;export_native&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_children&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="ss"&gt;"d:\testination.export"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#7de2" rel="alternate"/><published>2020-02-27T14:54:57.107000Z</published><updated>2020-02-27T14:54:57.107000Z</updated><author><name>aoj</name><uri>https://forge.codesys.com/u/aoj/</uri></author><id>https://forge.codesys.com697703c7b336c66c7799278ca840739de2713944</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi Markus,&lt;br/&gt;
thanks a lot for your help.&lt;br/&gt;
One more question &lt;br/&gt;
The following code is a part of my ExportScript:```&lt;/p&gt;
&lt;p&gt;proj = projects.open(pfad)&lt;br/&gt;
device = proj.find('Device', recursive = False)&lt;span&gt;[0]&lt;/span&gt;&lt;br/&gt;
device.export_native(destination=utils.temp(File), recursive = True)&lt;br/&gt;
print ("exporting finished")&lt;/p&gt;
&lt;p&gt;```As you can see the script exports the object &lt;strong&gt;Device&lt;/strong&gt;. What about if I have in my project 50 objects to export? Is it possible to export the whole project?&lt;/p&gt;
&lt;p&gt;Thanks &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#9518" rel="alternate"/><published>2020-02-27T14:54:56.487000Z</published><updated>2020-02-27T14:54:56.487000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com7ed54f64b0f6034b51be4f33bc8f2cbf41780fde</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; M.Schaber&lt;/p&gt;
&lt;p&gt;Hi, aoj,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;aoj hat geschrieben:&lt;/strong&gt;&lt;br/&gt;
What should I change to make fail-proof? And for the case if I dont know in which directory  is saved, is it possible to run it without giving the directory in the method?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;AFAICS, there's no perfect approach for this, as there can be several CoDeSys version installed in parallel, and the script cannot guess which one you wanted to start.&lt;/p&gt;
&lt;p&gt;I know the following approaches which all are being used in the field:&lt;/p&gt;
&lt;p&gt;Note that the directory containing a script is usually contained in the search path. Additionally, in most cases, a python script or module can detect its own directory using a construct like the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;scriptdir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;os&lt;/span&gt;.&lt;span class="nv"&gt;path&lt;/span&gt;.&lt;span class="nv"&gt;abspath&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;os&lt;/span&gt;.&lt;span class="nv"&gt;path&lt;/span&gt;.&lt;span class="k"&gt;dirname&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;__file__&lt;/span&gt;&lt;span class="ss"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can use this to find the directory of the Export_New.py script if it is installed in the same directory (or relative path) to the first script.&lt;/p&gt;
&lt;p&gt;HTH,&lt;br/&gt;
Markus&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#2f3e" rel="alternate"/><published>2020-02-27T14:54:55.876000Z</published><updated>2020-02-27T14:54:55.876000Z</updated><author><name>aoj</name><uri>https://forge.codesys.com/u/aoj/</uri></author><id>https://forge.codesys.com1e179f3b61d0710daa240af0545a0f4b526fc089</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;your&lt;/span&gt; &lt;span class="nv"&gt;methods&lt;/span&gt; &lt;span class="nv"&gt;of&lt;/span&gt; &lt;span class="nv"&gt;searching&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;CoDeSys&lt;/span&gt;.&lt;span class="nv"&gt;exe&lt;/span&gt; &lt;span class="nv"&gt;is&lt;/span&gt; &lt;span class="nv"&gt;not&lt;/span&gt; &lt;span class="nv"&gt;fail&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nv"&gt;proof&lt;/span&gt;: &lt;span class="k"&gt;If&lt;/span&gt; &lt;span class="nv"&gt;several&lt;/span&gt; &lt;span class="nv"&gt;CoDeSys&lt;/span&gt; &lt;span class="nv"&gt;versions&lt;/span&gt; &lt;span class="nv"&gt;are&lt;/span&gt; &lt;span class="nv"&gt;installed&lt;/span&gt; &lt;span class="nv"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;different&lt;/span&gt; &lt;span class="nv"&gt;directories&lt;/span&gt; &lt;span class="nv"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;C:\Program Files&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;, &lt;span class="nv"&gt;your&lt;/span&gt; &lt;span class="nv"&gt;script&lt;/span&gt; &lt;span class="nv"&gt;will&lt;/span&gt; &lt;span class="nv"&gt;match&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;first&lt;/span&gt; &lt;span class="nv"&gt;CoDeSys&lt;/span&gt;.&lt;span class="nv"&gt;exe&lt;/span&gt; &lt;span class="nv"&gt;it&lt;/span&gt; &lt;span class="nv"&gt;can&lt;/span&gt; &lt;span class="nv"&gt;find&lt;/span&gt;, &lt;span class="nv"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;this&lt;/span&gt; &lt;span class="nv"&gt;is&lt;/span&gt; &lt;span class="nv"&gt;not&lt;/span&gt; &lt;span class="nv"&gt;necessarily&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;one&lt;/span&gt; &lt;span class="nv"&gt;matching&lt;/span&gt; &lt;span class="nv"&gt;the&lt;/span&gt; &lt;span class="nv"&gt;profile&lt;/span&gt; &lt;span class="nv"&gt;you&lt;/span&gt; &lt;span class="nv"&gt;give&lt;/span&gt;.
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;What should I change to make fail-proof? And for the case if I dont know in which directory  is saved, is it possible to run it without giving the directory in the method?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#076b" rel="alternate"/><published>2020-02-27T14:54:54.184000Z</published><updated>2020-02-27T14:54:54.184000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.comc988d7e302315d78a9cf9b21a5e5e531b9363899</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; M.Schaber&lt;/p&gt;
&lt;p&gt;Hi, AOJ,&lt;/p&gt;
&lt;p&gt;The building of the command line is not related to the subprocess-module, it is just a normal string operation. You could do the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;codesys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;"CoDeSys.exe"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;exportscript&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PythonScript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;"Export.py"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;commandline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'"%s" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="%s"'&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;codesys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exportscript&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;commandline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The % operator for string formatting is documented in the python documentation: &lt;a class="" href="http://docs.python.org/library/stdtypes.html#string-formatting-operations" rel="nofollow"&gt;http://docs.python.org/library/stdtypes.html#string-formatting-operations&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Another way would be to use string concatenation:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;commandline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;codesys&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;'" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;exportscript&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Btw, your methods of searching the CoDeSys.exe is not fail-proof: If several CoDeSys versions are installed in different directories in "C:\Program Files", your script will match the first CoDeSys.exe it can find, and this is not necessarily the one matching the profile you give.&lt;/p&gt;
&lt;p&gt;HTH,&lt;br/&gt;
Markus&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#7bbb" rel="alternate"/><published>2020-02-27T14:54:53.540000Z</published><updated>2020-02-27T14:54:53.540000Z</updated><author><name>aoj</name><uri>https://forge.codesys.com/u/aoj/</uri></author><id>https://forge.codesys.com685d9cf81d92f609df25c14e5c7e92f17696712f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I have one more question  Is it possible to run CoDeSys without giving the directory of CoDeSys.exe in my method?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#ce6c" rel="alternate"/><published>2020-02-27T14:54:52.856000Z</published><updated>2020-02-27T14:54:52.856000Z</updated><author><name>aoj</name><uri>https://forge.codesys.com/u/aoj/</uri></author><id>https://forge.codesys.com6144177484acd1ca8b205084b28463611c55ec83</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Markus you are my hero &lt;br/&gt;
I didnt know how to build the CoDeSys command line using the subprocess-Modul. Now it is working. Thank you again and wish you a nice day.&lt;/p&gt;
&lt;p&gt;AOJ&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#c933" rel="alternate"/><published>2020-02-27T14:54:51.219000Z</published><updated>2020-02-27T14:54:51.219000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com3e5fca72e209f990906dc3a8fb03d9baa59d8d48</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; M.Schaber&lt;/p&gt;
&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;In your first post, you had the following code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s1"&gt;'"C:\Program Files (x86)\3S CoDeSys\CoDeSys\Common\CoDeSys.exe" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="D:\-----Python32\Export_New.py"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This code shows how the command line to start CoDeSys has to be built together. Then, you should build the CoDeSys command line using this pattern.&lt;/p&gt;
&lt;p&gt;Why do you to call CoDeSys without arguments, and afterwards try to start the script outside of CoDeSys with os.system instead?&lt;/p&gt;
&lt;p&gt;Try something like the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;codesys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;"CoDeSys.exe"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;exportscript&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PythonScript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;"Export.py"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'"%s" --Profile="CoDeSys V3.4 SP4 Patch 1" --runscript="%s"'&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;codesys&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exportscript&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;HTH,&lt;br/&gt;
Markus&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Python and CoDeSys</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/1a5565059b/?limit=25#cf94" rel="alternate"/><published>2020-02-27T14:54:50.642000Z</published><updated>2020-02-27T14:54:50.642000Z</updated><author><name>aoj</name><uri>https://forge.codesys.com/u/aoj/</uri></author><id>https://forge.codesys.comcd40f7f55419c358f7c0059a34d9b35f904462ce</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi M.Schaber,&lt;/p&gt;
&lt;p&gt;I am trying to call the two functions with the following code:```&lt;/p&gt;
&lt;p&gt;codesys = "CoDeSys.exe"&lt;br/&gt;
subprocess.call(runProcess(r"%s" % codesys))&lt;br/&gt;
exportscript = "Export.py"&lt;br/&gt;
os.system(PythonScript(exportscript))&lt;/p&gt;
&lt;p&gt;``` &lt;/p&gt;
&lt;p&gt;The project doesnt get exported because the second script deosnt get run from inside of the CoDeSys environment. Is it now more clear?&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>