<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Python plugin - handling many scripts...]]></title><description><![CDATA[<p dir="auto">Hi, I had a few general questions about using the python plugin. I’ve been benefiting from it for a while now, and my script list keeps on growing more and more… and is getting a little too long.<br />
Is there a way to handle this?<br />
(I don’t mind so much the sub-script list, I am mostly speaking about the scripts that appear in the main python menu, in which many of my scripts appear, since only those that appear in this list can be used in shortcuts and in toolbar using Customize Toolbar plugin)</p>
<p dir="auto">Thanks<br />
Davey</p>
<p dir="auto">P.S. Just to mention a few ideas: In this regard, there a few ‘better’ options in the javascript plugin for n++:</p>
<ol>
<li>You can setup customized menus and submenus and have all your scripts listed within</li>
<li>You can add a keyboard shortcut directly from the plugin, no need to go to settings &gt; shortcut mapper</li>
<li>All of the above can be done within one script (or multiple)</li>
</ol>
<p dir="auto">Are any of these options available through python plugin and I’m just missing it?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/11090/python-plugin-handling-many-scripts</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 15:53:45 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/11090.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Jan 2016 21:50:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 03:13:47 GMT]]></title><description><![CDATA[<p dir="auto">Hey Claudia!<br />
Nice! I understand now fully!<br />
(I got mixed up with the name of the variable and the value…)</p>
<p dir="auto">You’re wonderful!!<br />
Thank you<br />
Davey</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13179</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13179</guid><dc:creator><![CDATA[DaveyD]]></dc:creator><pubDate>Mon, 11 Jan 2016 03:13:47 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 03:07:41 GMT]]></title><description><![CDATA[<p dir="auto">Hello Davey,</p>
<p dir="auto">no, __name__ is the variable, __main__ the value.<br />
consider the following in a script which gets imported by another script</p>
<pre><code>from Npp import *

console.write('imported or called directly\n')

if __name__ == "__main__":
    console.write('only if called directly\n')
</code></pre>
<p dir="auto">if your scripts imports it you will only see the line 'imported or called directly<br />
if you run the script directly you will see both.</p>
<p dir="auto">Why the need of this if…</p>
<p dir="auto">You want to write a module which provides some basic function to other scripts.<br />
Like, you have a convert module which converts what ever you want. Instead of writing the<br />
function in evey script again and again you write a module and import it. I know you know this.<br />
But what if you want to set up test routines which should check if the module is still working<br />
after you have modified it? You don’t use another script to test it you write your test case under the if clause.<br />
So they get not executed when some other script is importing it but when you call it directly.</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13177</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13177</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Mon, 11 Jan 2016 03:07:41 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 02:53:28 GMT]]></title><description><![CDATA[<p dir="auto">Thanks Claudia!<br />
Your suggestion works perfectly!</p>
<p dir="auto">This is what i experienced:</p>
<ol>
<li>When I ran the code with <code>if __name__ == "__main__":main()</code> everything worked perfectly</li>
<li>When I removed the if statement, and just wrote <code>main()</code>, the script got ran 2 times - I’m understanding, once while <strong>importing</strong> with the <code>import</code> statement, and then again when the script called it</li>
<li>When I run that script directly, it works in both instances (with and without the if condition)</li>
</ol>
<p dir="auto">As to why this is true, here is what I understood from testing:</p>
<ol>
<li>When the script is called directly, the <code>__main__</code> variable is always <code>"__main__"</code>, which means… (not sure)</li>
<li>When the script is imported into a different script, the <code>__main__</code> variable is set to the name of the script being imported!</li>
<li>When the script is <strong>called</strong> from another script, then it is similar to running it directly, and the <code>__main__</code> variable is set to <code>"__main__"</code></li>
</ol>
<p dir="auto">And after I wrote all that… I now can say that I understand your previous posts!</p>
<p dir="auto">Thank you very much Claudia!<br />
Davey</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13174</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13174</guid><dc:creator><![CDATA[DaveyD]]></dc:creator><pubDate>Mon, 11 Jan 2016 02:53:28 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 02:29:05 GMT]]></title><description><![CDATA[<p dir="auto">Hello Davey,</p>
<p dir="auto">as said, the python interpreter reads your module/python script and executes all <em>insecure</em> code. Don’t know how to say this better.<br />
Before it is done, the interpreter sets some special variables like __name__  and its value is __main__ only if this file is the main file.<br />
( Btw there is an agreement in the python community that you shouldn’t mess with variables already defined which start with an<br />
underscore. Variables defined by you can, of course, treated as you like)</p>
<p dir="auto">E.g. python.exe <a href="http://myfile.py" rel="nofollow ugc">myfile.py</a></p>
<p dir="auto">means, that __main__ refers to <a href="http://myfile.py" rel="nofollow ugc">myfile.py</a> and all code within will be executed, as long as everything is correct of course.</p>
<p dir="auto">If <a href="http://myfile.py" rel="nofollow ugc">myfile.py</a> has an import mymodule then it’s __name__ variable is set to mymodule instead.<br />
So if the mymodule gets loaded the interpreter doesn’t find __name__ == __main__ and does not execute code<br />
which is under this if statement. Other code gets executed.<br />
Instead you need to explicitly call it via mymodule.myfunction.</p>
<p dir="auto">So this line is simply a way to secure code against running.</p>
<p dir="auto">Is this more understandable? I guess if you do the steps I described you will get it.</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13173</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13173</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Mon, 11 Jan 2016 02:29:05 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 02:05:17 GMT]]></title><description><![CDATA[<p dir="auto">Hi <strong>Claudia</strong><br />
Thanks for your detailed response! Sounds great!<br />
I happen to know a little about importing - I have made my own module with basic functions that I use often (e.g., getWord() to get current word etc.)<br />
However, I didn’t know about this</p>
<pre><code> if __name__ == "__main__":
</code></pre>
<p dir="auto">What does this mean and do exactly?<br />
How does it work - you write the function that you want to run? (I.e., in your case, the function name was “main” so you wrote “main”, but what if I have a different function name - do I declare that name, or should I just create one which guides the script?)</p>
<p dir="auto">Thanks,<br />
Davey</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13171</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13171</guid><dc:creator><![CDATA[DaveyD]]></dc:creator><pubDate>Mon, 11 Jan 2016 02:05:17 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 00:03:05 GMT]]></title><description><![CDATA[<p dir="auto">Hello Davey,</p>
<p dir="auto">part1 - no, as you already said,</p>
<ul>
<li>main python script menu,</li>
<li>toolbar (but no need of extra plugin)</li>
<li>and context menu (with a level of one folder)</li>
</ul>
<p dir="auto">can be used to access scripts faster.</p>
<p dir="auto">part2 - not really, Python Script plugin doesn’t allow execution of multiple scripts at the same time<br />
but you can overcome this by using the python way - <strong>import</strong> might be your friend.</p>
<p dir="auto">Might, because you need to start writing the code a little bit different and you need to be aware<br />
what import really means.</p>
<p dir="auto">When importing a python module, which isn’t really different to any other python script, the loader<br />
executes every code which isn’t <em><strong>secured</strong></em>.</p>
<p dir="auto">I guess I better explain it by a simple example.</p>
<p dir="auto">Assuming you have 2 python scripts</p>
<ul>
<li>main_test.py</li>
<li><a href="http://test1.py" rel="nofollow ugc">test1.py</a></li>
</ul>
<p dir="auto">The main_test.py is, as the name suggest ;-), the script which you use to execute other scripts<br />
therefore it has the following line in it.</p>
<pre><code>import test1
</code></pre>
<p dir="auto"><a href="http://test1.py" rel="nofollow ugc">test1.py</a> scripts does have the lines</p>
<pre><code>from Npp import *

console.write('test1.py\n')
</code></pre>
<p dir="auto">now if you execute main_test.py you will see that python script console shows <a href="http://test1.py" rel="nofollow ugc">test1.py</a> as message.<br />
So what you need to do to <em>secure</em> it, you need to take care that every code is encapsulated in a function.</p>
<pre><code>from Npp import *

def main():
    console.write('test1.py\n')
</code></pre>
<p dir="auto">Now you can import it from main_test and nothing gets executed automatically.<br />
Run an inputbox, choose the script you want and execute it like</p>
<pre><code>test1.main()
</code></pre>
<p dir="auto">name_of_the_module.name_of_the_function</p>
<p dir="auto">If you import it like</p>
<pre><code>import test1 as t
</code></pre>
<p dir="auto">then execution would be</p>
<pre><code>t.main()
</code></pre>
<p dir="auto">But now there is another issue. You cannot run <a href="http://test1.py" rel="nofollow ugc">test1.py</a> independently. To overcome this<br />
you need to add the following lines to the end of the test1 script</p>
<pre><code>if __name__ == "__main__":
    main()
</code></pre>
<p dir="auto">or any other extra code which is needed to execute it independently.</p>
<pre><code>if __name__ == "__main__":
    console.write('now i'm called directly\n')
    main()
</code></pre>
<p dir="auto">Done.</p>
<p dir="auto">As you see, <a href="http://test1.py" rel="nofollow ugc">test1.py</a> needs to import Npp because every module/python script which get imported<br />
will be imported to the local context and doesn’t have access to the global context automatically.</p>
<p dir="auto">Cheers<br />
Claudia</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13161</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13161</guid><dc:creator><![CDATA[Claudia Frank]]></dc:creator><pubDate>Mon, 11 Jan 2016 00:03:05 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Mon, 11 Jan 2016 00:00:02 GMT]]></title><description><![CDATA[<p dir="auto">Should work.  Try it out in some simple test first, before investing a lot of time architecting something bigger that could perhaps not work for your situation.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13160</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13160</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Mon, 11 Jan 2016 00:00:02 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Sun, 10 Jan 2016 23:56:12 GMT]]></title><description><![CDATA[<p dir="auto">Hi Scott, thanks for replying<br />
I did not try this yet, but I do have a question:<br />
I want the other_script_file to be run in n++ to make changes to the text. The execFile() is a python function, not a n++ function - does this work?</p>
<p dir="auto">Thanks,<br />
Davey</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13159</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13159</guid><dc:creator><![CDATA[DaveyD]]></dc:creator><pubDate>Sun, 10 Jan 2016 23:56:12 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Sun, 10 Jan 2016 23:51:53 GMT]]></title><description><![CDATA[<p dir="auto">For the running-a-pythonscript-file-from-another part of your question, have a look at the execfile() function.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13158</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13158</guid><dc:creator><![CDATA[Scott Sumner]]></dc:creator><pubDate>Sun, 10 Jan 2016 23:51:53 GMT</pubDate></item><item><title><![CDATA[Reply to Python plugin - handling many scripts... on Sun, 10 Jan 2016 22:36:54 GMT]]></title><description><![CDATA[<p dir="auto">I would like to add one more part to the question,<br />
Is it possible to run a python script from a python script?<br />
This would also help, because then I can have a script that would raise an input box, and I can type in which script I want</p>
<p dir="auto">I tried using <code>notepad.runPluginCommand("Python Script", "ScriptName")</code> but I got a message that another script is already running, which I assumed was the current one…</p>
<p dir="auto">So, Is this possible any other way?</p>
<p dir="auto">Thanks<br />
Davey</p>
]]></description><link>https://community.notepad-plus-plus.org/post/13157</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/13157</guid><dc:creator><![CDATA[DaveyD]]></dc:creator><pubDate>Sun, 10 Jan 2016 22:36:54 GMT</pubDate></item></channel></rss>