PythonScript icon indicating copy to clipboard operation
PythonScript copied to clipboard

List of preloaded modules differs

Open tho-gru opened this issue 3 years ago • 3 comments

Hi,

While doing some scripting as described here it seems to be that some python modules are magically loaded during manual execution via the menu. For some reasons some of these modules are not "preloaded" when executing via startup.py.

As a small testcase I created TgrShowConsole.py:

from Npp import console

console.show()

This python script work fine when executed via the menu.

But if I execute this script on startup by adding import TgrShowConsole to the startup.py I receive the following error message:

Traceback (most recent call last): File "C:\Users\ZZunder\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\startup.py", line 22, in import TgrShowConsole File "C:\Users\ZZunder\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\TgrShowConsole.py", line 2, in console.show() NameError: name 'console' is not defined

The solution is to comment in the first line to load from Npp import console.

Because I am beginner in Python and novice in scripting notepad++ I simply thought: "The script work when executing via the menu". Therefore I assume that the script itself is correct and do not understand the error message (although it is clear as PeterJones pointed out).

Perhaps you can preload all modules available later before startup.py is executed.

My Environment

Notepad++ v8.1 (64-bit) Build time : Jun 17 2021 - 01:56:07 Path : C:\Program Files\Notepad++\notepad++.exe Command Line : "C:\Users\ZZunder\AppData\Roaming\Notepad++\plugins\config\PythonScript\scripts\TgrShowConsole.py" Admin mode : OFF Local Conf mode : OFF Cloud Config : OFF OS Name : Windows 10 Pro (64-bit) OS Version : 2009 OS Build : 19043.1052 Current ANSI codepage : 1252 Plugins : ComparePlugin.dll DSpellCheck.dll JSMinNPP.dll mimeTools.dll NppConverter.dll NppExec.dll NppExport.dll PythonScript.dll XMLTools.dll

Versions of installed Plugins: Notepad++PluginVersions

tho-gru avatar Jun 27 '21 13:06 tho-gru

@tho-gru ,

As was explained to you in the forum by @Ekopalypse before you created this issue, when you run a script from the PythonScript Scripts menu, it is in the __main__ namespace, so if your startup.py has already imported those default symbols, then the names are already defined in that namespace, and so are available to your script. However, when you have a line in startup.py that imports your script (the import 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, the TgrUrlAltClick namespace has never imported the symbols from the Npp module, and that is why the Npp-defined objects notepad, console, and editor are not available to functions in your imported script.

The PythonScript plugin cannot and will not redefine the Python language import mechanism to inject the __main__ namespace symbols into whatever scripts you happen to import via startup.py.

The PythonScript plugin cannot and will not do any magic in startup.py that will be able to recognize when an import in startup.py is 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 to import via startup.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 import from startup.py or 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 being imported as a module from another script. Adding the line from 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 will also be posted in the forum discussion)

pryrt avatar Jun 29 '21 13:06 pryrt

@pryrt 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:

  1. hide (unload or what ever possible) the console module for scripts started via the menu
  2. "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 import the modules needed.

Kind Regards Thomas

tho-gru avatar Jun 29 '21 14:06 tho-gru

For future readers, the discussion also continued in the forum, where I replied in detail to these points.

https://community.notepad-plus-plus.org/post/67510 https://community.notepad-plus-plus.org/post/67511

The short version is: we have explained how to do it, and why your suggestions won't work; the manual told you how to do it; the two explanations agree with each other.

pryrt avatar Jun 29 '21 15:06 pryrt