Menu

from <something> import *

Help
2014-10-02
2014-10-04
  • John Bigreen

    John Bigreen - 2014-10-02

    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

     
    • Dave Brotherstone

      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

       
  • John Bigreen

    John Bigreen - 2014-10-04

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.