I use the evil syntax "from something import *" in a script myevilscript.py, but something strange happen. If I made some change in something.py I need to relaunch Npp to my change be taken in account in myevilscript.py.
Example :
myevilscript.py : Adding "console.write(MY_EVIL_CONSTANT)"
something.py : Adding "MY_EVIL_CONSTANT='using from <something> import * syntax is bad!'"
Error on running myevilscript.py : "NameError: global name 'MY_EVIL_CONSTANT' is not defined"
And after an Npp relaunch no more error...
I don't know/find if it's an Npp/PS issue or if I miss something important with python (learning python in progress). Is there a command/workaround for this? Am I condemned to relaunch Npp for using heretical syntax ;) ?
regards,
john
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You're not condemned to relaunch Npp every time. You just need to do a
reload(module_name)
For imports, when you import something again, you get the same object, and
it doesn't check the file again. Because Python Script in n++ doesn't ever
renew the context (a bit like a single REPL session), importing something
again will not check the file, and you need to use reload.
The unit tests use this, if you want to have a look how they do it.
Cheers
Dave
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you for the tip! I use a "reload(sys.modules['something'])" syntax wich allow the script running well after two launches. You put me on the right way, I will find exactly what I need now.
Thank you again, especially for Python Script.
regards,
John.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
With :
-Npp 6.6.8
-Python Script 1.0.6.0
I use the evil syntax "from something import *" in a script myevilscript.py, but something strange happen. If I made some change in something.py I need to relaunch Npp to my change be taken in account in myevilscript.py.
Example :
myevilscript.py : Adding "console.write(MY_EVIL_CONSTANT)"
something.py : Adding "MY_EVIL_CONSTANT='using from <something> import * syntax is bad!'"
Error on running myevilscript.py : "NameError: global name 'MY_EVIL_CONSTANT' is not defined"
And after an Npp relaunch no more error...
I don't know/find if it's an Npp/PS issue or if I miss something important with python (learning python in progress). Is there a command/workaround for this? Am I condemned to relaunch Npp for using heretical syntax ;) ?
regards,
john
You're not condemned to relaunch Npp every time. You just need to do a
reload(module_name)
For imports, when you import something again, you get the same object, and
it doesn't check the file again. Because Python Script in n++ doesn't ever
renew the context (a bit like a single REPL session), importing something
again will not check the file, and you need to use reload.
The unit tests use this, if you want to have a look how they do it.
Cheers
Dave
Hi,
Thank you for the tip! I use a "reload(sys.modules['something'])" syntax wich allow the script running well after two launches. You put me on the right way, I will find exactly what I need now.
Thank you again, especially for Python Script.
regards,
John.