Integration of a script writen in Python
-
Thanks for your additions.
I already used the
startup (user)script.Kind Regards,
Thomas -
I opened the issue 205 for the difference in “preloaded” modules.
-
-
@tho-gru ,
To use different words than @Alan-Kilborn and @Ekopalypse have already used to try to explain this to you:
As was explained to you above by @Ekopalypse (in case @Alan-Kilborn’s link doesn’t work well – links to individual posts in the NodeBB-based forum don’t highlight the post, and sometimes don’t scroll to the part of the post you expect them to – you can look in this topic for where @Ekopalypse said, “because it is then defined in the main namespace”) before you created the issue 205, when you run a script from the PythonScript Scripts menu, it is in the
__main__namespace, so if yourstartup.pyhas already imported those default symbols, then the names are already defined in that__main__namespace, and so are available to your script. However, when you have a line instartup.pythat imports your script (theimport TgrUrlAltClick), Python (not the PythonScript plugin) puts that whole file in its own namespace (TgrUrlAltClick, in your example). Thus, in the context of that imported script, theTgrUrlAltClicknamespace has never imported the symbols from the Npp module, and that is why the Npp-defined objectsnotepad,console, andeditorare not available to functions in yourimported script.The PythonScript plugin cannot and will not redefine the Python language
importmechanism to inject the__main__namespace symbols into whatever scripts you happen toimportviastartup.py.The PythonScript plugin cannot and will not do any magic in
startup.pythat will be able to recognize when animportinstartup.pyis really trying to auto-execute a user-defined script rather than importing a standard Python library, so cannot and will not try to magically inject the__main__namespace symbols into the scripts you happen toimportviastartup.py.You can and should and are expected to import the symbols that you need from the Npp module into any script that you desire to
importfromstartup.pyor from any other script, because that is the only way to guarantee that any script you write will have access to those symbols whether the script is run from the PythonScript Scripts menu or by beingimported as a module from another script. Adding the linefrom Npp import *(or a reduced version if you don’t need all the Npp symbols) into all your scripts is not an onerous task on your part, and importing modules and symbols is an expected aspect of any Python development, not just PythonScript development.(A copy of this has also been posted in your issue)
-
@PeterJones I fully understand that I need to import symbols from other modules by typing
import ....Unfortunately (now I understand why) I did not get an error message while executing the script via the menu. My complain is not the need of importing modules I am using in my script. My complain is that I want to get same error messages independent how I can start a python script.
I currently see two possible solutions to achieve this:
- hide (unload or what ever possible) the console module for scripts started via the menu
- “preload” the console module to be available for all script started via the startup.py (user or system)
Of course the should be done for all “preloaded” modules available in scripts started via the menu.
This should simply remind beginners to
importthe modules needed.Kind Regards
Thomas -
@tho-gru said in Integration of a script writen in Python:
@PeterJones I fully understand that I need to import symbols from other modules by typing
import ....Not fully, because your option 2 below seemed to indicate you still think it’s possible to pre-load it.
I currently see two possible solutions to achieve this:
- hide (unload or what ever possible) the console module for scripts started via the menu
Then change the
from Npp import *from the machine and userstartup.pytoimport Nppyourself. That way, the errors will come from the beginning for you, without influencing other people who do not want to have to do the imports when using the PythonScript console in immediate mode. (If you do this, calls to the standard objects inside thestartup.pyscripts will have to be prepended withNpp.)- “preload” the console module to be available for all script started via the startup.py (user or system)
Of course the should be done for all “preloaded” modules available in scripts started via the menu.
As already explained in detail above, but I’ll try to phrase it differently, because you have not understood, despite saying that you understand: The
startup.pycurrently preload the Npp instance variables into the__main__namespace, and thus for every script started from the menu, those constants are pre-loaded. But that cannot affect scripts that youimportinto yourstartup.pyscript, because that’s not the way Pythonimportworks – when you import a module in a given, it imports into the current namespace seen by that file;startup.pyare in the__main__namespace, so they import symbols into the__main__namespace; scripts run from the menu are in the__main__namespace, so they import symbols into the__main__namespace; but a script likeTgrUrlAltClick.pythat is imported into some other script (like usingimport TgrUrlAltClickinstarup.pyto import a script rather than a standard module) is in its own namespace, so none of the functions defined insideTgrUrlAltClick.pywill see the__main__namespace symbols because functions insideTgrUrlAltClick.pyare in theTgrUrlAltClicknamespace in this condition.What you are asking in point#2 is the equivalent of saying that when you have the following situation, it should work instead of throwing a NameError exception.
C:\usr\local\apps\python-3.9.6-embed-amd64>type mainFile.py #!python # encoding=utf-8 from sys import path from MyOtherScript import myFunction myFunction() C:\usr\local\apps\python-3.9.6-embed-amd64>type MyOtherScript.py #!python # encoding=utf-8 print("Hello from namespace '{}'!\n".format(globals()['__name__'])) def myFunction(): print(path) C:\usr\local\apps\python-3.9.6-embed-amd64>python mainFile.py Hello from namespace 'MyOtherScript'! Traceback (most recent call last): File "C:\usr\local\apps\python-3.9.6-embed-amd64\mainFile.py", line 6, in <module> myFunction() File "C:\usr\local\apps\python-3.9.6-embed-amd64\MyOtherScript.py", line 7, in myFunction print(path) NameError: name 'path' is not definedThis should simply remind beginners to
importthe modules needed.Beginners don’t import their scripts into
startup.py; beginners run their scripts manually from the menu. People who want to do more advanced actions, like automatically running their script fromstartup.pyby importing the script via a command instartup.pyneed to learn about things like namespaces and importing, and need to actually understand those concepts … or at least be willing to take the advice to “alwaysfrom Npp import *in every script if you’re going to do anything but run your script from the menu”. -
@PeterJones said in Integration of a script writen in Python:
People who want to do more advanced actions,
Also, people who want to do more advanced actions read the manual that’s included with PythonScript plugin, which says quite explicitly in the Npp Module section of the Introduction:

Honestly, beginners, not just people moving on to advanced topics, should have also at least read the whole introduction.
The manual was quite explicit: if you want to write a module (a new file) that is called from someplace else (which is exactly what you do when you import your script into
startup.py), then you need to include thefrom Npp import *inside your module.We’ve told you. The manual has told you. Good luck.