importing included libraries

Help
Anonymous
2012-08-31
2013-01-25

  • Anonymous
    2012-08-31

    I am attempting to get some more sophisticated dialog boxes generated than the simple message box.  What I've got in mind is something along the lines of the interactive docked panel that the Analyze plugin has got.  It doesn't have to dock, but that would be nice.

    Basically, I'd like to be able to input multiple regular expressions, hit "go" and see a graph. 

    It looks to me like the Tkinter library should be able to do the job.  I see that it comes with the PythonScript plugin (PythonScript/lib/lib-tk) but have not been able to get the import to work.

    I am trying to get the "hello world" example from http://www.tutorialspoint.com/python/tk_canvas.htm to work with no success.

    import Tkinter
    import tkMessageBox
    top = Tkinter.Tk()
    C = Tkinter.Canvas(top, bg="blue", height=250, width=300)
    coord = 10, 50, 240, 210
    arc = C.create_arc(coord, start=0, extent=150, fill="red")
    C.pack()
    top.mainloop()
    

    Thanks!

     

  • Anonymous
    2012-08-31

    I should add, the result I'm getting from running that example is

    Traceback (most recent call last):
      File "C:\Users\phil\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\scratch.py", line 1, in <module>
        import Tkinter
    ImportError: No module named Tkinter
    
     
  • Dave W.
    Dave W.
    2012-09-01

    Phil,
    Just OTMH, that error message sounds like what happens when you have Python 3.x installed, and then try to "import Tkinter". The name of the module has changed from Python 2.x, from "Tkinter" to just "tkinter". So you get the message that the module is not found. If that is not the case, and the ONLY Python on your machine is the one that installs under NppPython, then you may simply need to modify your PYTHONPATH environment variable to point to the Python directory under Npp. (I don't recall all the exact details but hopefully someone with some more recent experience in this area can fill them in.)
    HTH,
    Dave W.

     

  • Anonymous
    2012-09-01

    Well, that's progress.  I don't have any other python installed, so I set the environment variables PYTHONHOME and PYTHONPATH.  Now the import is working but it's failing to initialize Tkinter.

    Python 2.7.1 (release27-maint-npp, Feb  6 2011, 16:58:20) [MSC v.1500 32 bit (Intel)]
    Initialisation took 78ms
    Ready.
    Traceback (most recent call last):
      File "C:\Documents and Settings\Phil\Application Data\Notepad++\plugins\Config\PythonScript\scripts\scratch.py", line 4, in <module>
        top = Tkinter.Tk()
      File "C:\Program Files\Notepad++\plugins\PythonScript\lib\lib-tk\Tkinter.py", line 1680, in __init__
        baseName = os.path.basename(sys.argv[0])
    AttributeError: 'module' object has no attribute 'argv'
    

    I am an absolute noob with regard to python.  So I'm not sure what I'm dealing with here. 

    Thanks for your help with this! 

     
  • Dave W.
    Dave W.
    2012-09-01

    Phil,
    Put the following line at the very top of your script:

    sys.argv =

    Why? Because PythonScript is distributed with a specially-built version of Python 2.7 that does not expose the standard sys.argv variable. sys.argv is the system variable that stores your filename and any command-line variables you pass in to it when you run your script from the command-line. But PythonScript assumes that since Python is already being loaded for you, and it is loading and running your script for you, that you will never have to pass in any command-line variables, that would normally be made available in the sys,argv variable ( a list object ), so it does not expose that variable to your code. However, obviously, there are standard library modules that require it, if for nothing else than to get the filename out of element 0. sys.argv normally contains either the script name or an empty string, depending on how you run your script. In the case of PythonScript, it most likely is always an empty string anyway. But at any rate, it fer sure ain't there. You can observe this by opening the PythonScript console and typing dir(sys). You will not see "argv" in the list of property names displayed.

    So to make Tkinter.__init__(…)  happy, you need to add the missing argv variable back to module sys, and provide at least the 0'th element, which in this case will be your script name.

    I tested this on my Win 7 box with Notepad++ v6.1.1 and PythonScript 0.9.2.0 with Python version: Python 2.7.1 (release27-maint-npp, Feb  6 2011, 16:58:20)  and the example dialog box did indeed pop up.

    So give that a try. It should work for you.

    Dave W.

     

  • Anonymous
    2012-09-04

    Thanks for your help with this, and the explanation for what's going on.   I figured it must have been the way PythonScript was launching the scripts but had no idea how to deal with it.

    I added the line you suggested, and that did solve the immediate problem.  The error changed to complaining about TCL not being installed.  I installed the ActiveTCL binaries, and Tkinter still didn't find it. 

    In any case, I don't think that this will be the route I need to take for my desired plugin.  I'd rather not have an outside dependency to get it working. 

    I'll definitely be using PythonScript in the future though, just not for things that require any kind of GUI.  :)  I'm VERY impressed with how easy it was to get the basic regex working to generate the datafile for gnuplot.

    Thanks again!

    Phil

     
  • Dave W.
    Dave W.
    2012-09-04

    Phil,
    You're absolutely right. I never even noticed that PythonScript does not include the tcl library. On my machine, it must have been finding it under my normal Python2.7 install.
    Yeah, I'm afraid you would not be able to depend on Tk for doing any gui under PS in this case, because most folks who use PythonScript will not have the full Python distro installed, I would bet.
    Oh well, it was worth a shot anyway. Hope you find a solution that works for your client base.

    (Meanwhile, if you happen to read this, Dave. B., you might give some thought to including the tcl library, or just some instruction to folks on how to instruct their users to install it, short of having them just install Python2.7. Still, I know there could some issues with Tk running its own event loop and possibly needing to pass in the Npp object to it through a closure or such - not a trivial endeavor I'm sure. But certainly would like to hear your thoughts on this if you get a few minutes. Thanks)
    Dave W.

     
  • Dave W.
    Dave W.
    2012-09-04

    Phil,
    I decided to play around with this concept just a bit more, because it intrigues me and, well, I'm a stubborn old warhorse.
    I don't like getting snookered, so I wanted to see just exactly what it WOULD take to get this damn thing "working" - and I use the term loosely.
    Well I got it "working". So in case you change your mind and decide you want to pursue this madness any further, and just for future reference, here's what I did:
    (You will want to go ahead and install Python 27 - I would recommend the ActiveState distro.)
    First, I renamed my Python27 directory to Python27XXX, just to make damn sure PythonScript could not find it.
    Then, I copied the tcl library out from under it to "C:\Notepad++\plugins\PythonScript\tcl".
    Then, I copied the following three tcl-related dlls:
    tcl85.dll
    tclpip85.dll
    tk85.dll
    from Python27/DLLs over to the C:/Notepad++ directory.
    And basically that's it.
    I don't want to set a system-wide PYTHONPATH environment variable (since I don't normally use one anyway), so I started up a command prompt in my C:/Notepad++ directory, set the local PYTHONPATH environment variable to PYTHONPATH=C:\Notepad++\plugins\PythonScript;C:\Notepad++\plugins\PythonScript\lib;C:\Notepad++\plugins\PythonScript\lib\lib-tk;C:\Notepad++\plugins\PythonScript\tcl;
    and then ran notepad++.exe from the commandline.
    I fired up PythonScript and ran the Tk example script and it "worked". The Tk dialog box came up.
    So - what does all this mean?
    Well, I guess that would depend on who your intended user base is. If it's just you (and your teammates), then you're in good shape. You could just install Python and leave it at that - pretty much. But if you need to distribute, then it gets more involved. It certainly would not be difficult to install Python27 on your machine (which I can't imagine you haven't done already ;-) and post the required tcl library and DLLs in a shared folder somewhere on the intranet where they could copy them to the appropriate folders, if your target user base is technical at all. If they're typical admin folks, well… I dunno. Good luck. There's usually at least one techie hanging around that can fix it up for 'em. (I don't know your general CS/IT experience level or project situation, just that you're not a "Pythonista". Yet…  ;-)
    Obviously, the instructions aren't that hard, but… sure would be better to have a little install pgm of some sort, and of course that's where things start getting a bit messy. Don't even wanna think about that right now…
    But if you need to do anything more than just displaying results of running the script - formatted data from calculations, etc. - then you're probably going to have to do some fairly serious R&D. ;-)

    Either way, be aware that while the Tk window is up, you cannot run any other PythonScripts (unless you run the Tk script on a separate thread). You can still use all the other facilities and UI of Notepad++ with no apparent problem.
    I've run a long-running Python server session (xmlrpc) on a separate thread from within PythonScript with no "apparent" problems - yet - but I have not tried a UI event loop. Especially, a Tkinter loop, which in the past at least was notorious for causing problems with "the GIL" - the Python Global Interpreter Lock - in some cases. As far as I understand, Python does not allow (at least on Windows) freely running multiple true OS threads - it controls all the "multi-threading" itself. Hence, the GIL. And, just for good measure, PythonScript itself will not allow two scripts to run "at the same time".
    (If it knows about it, that is…) ;-)
    But if that's not an issue for you then you're in good shape.

    So, there it is. I hope you found this rant entertaining and at least somewhat enlightening. I enjoyed it immensely.
    And I hope you do continue to learn Python. It's a great tool to have in your kit. Not to mention just a helluvalot of fun sometimes.

    Best regards,
    Dave W.

     
  • Hey Thanks Dave W. for looking into this, and for getting it working!  Sorry I've not chimed in earlier, I'm a bit bogged down at the moment.  

    I actually spent some time originally compiling the tcl extension with /MT, so it can be used with the python27.dll that comes with PS.  However, it appeared I needed a TCL/TK installation in order to make it work.  I'm happy to sort this out, such that we could have an extra installation if people want to do gui things.  Could you point to a sample script that should test that tcl and tk work?  It's definitely time for a new PS release. (I might regret saying that…)

    And yes, the GIL, <sigh/>, Python doesn't multi-thread. Ever.  It's a shame, but I understand their (his, the BDFL's) position, that without it would make everything lots more complicated. 

    Thanks again,
    Dave.

     
  • Dave W.
    Dave W.
    2012-09-05

    Hey! Dave. B, good to hear from ya. Been awhile. Glad to hear you're staying busy…  ;-)
    So, yeah, I think this could be some major coolness here without too much pain at all really…
    Unfortunately, I'm out of playtime myself here for the next few days (hate that). What I'd really like to do is find one of the bit larger examples from PP3E
    that actually do some user interaction, jick it around a bit and feed it a ref to Npp and see how they play together. I really don't  anticipate any problems in that regard - it's not really any different than what I do with Automation. So I'll see what I can scrounge up sometime in the next few days and wring it out best I can quickly and when I get it working I'll stick it in a dropbox and post a link to it. Meanwhile, you could just use the little basic script that Phil posted - that's what I used to test this so far - if you want to get a jump. (Oh, don't forget the sys.argv bit… Needs that.)
    Alright. So… more adventures… ;-0)
    Talk to you soon.
    Best regards,
    Dave W.

     

  • Anonymous
    2012-09-05

    What I thought would be a fairly simple n++ plugin has turned out to not be.  I haven't given up on the project, but since the point is really to make my life easier, I'm not inclined to fight this thing too hard.  It's a side project tool, so at work time investment is minimal.  Side effect is that if I do it on my own time, I'll be able to share the result here.

    The target audience is 90% me, so it isn't a total deal breaker if I have to install some extra stuff.  But if it's a little zip file that can be upacked or whatever that would be way way better.  We usually have n++ on the servers that have log files I get asked to look at.  So it would be nice to be able to easily deploy this thing. 

    It's ok if n++ locks up while the script is running, especially if the UI widget doesn't dock directly into n++.  Basically, I'm looking for a little UI to manage some regular expressions, and a way to display a generated graph.  (Events happen periodically with numeric values.  Graph that stuff over the duration of the log) 

    I'll take a look at installing python and see if I can at least get the little demo thing to pop up.  If I can make that go, then odds are good I'll be able to muster the will to take it the distance.

    Thanks again for your help on this and for the slick plugin!

     
  • Dave W.
    Dave W.
    2012-09-06

    What I thought would be a fairly simple n++ plugin has turned out to not be.

    Welcome to DevWorld! ;-D

    I haven't given up on the project, but since the point is really to make my life easier, I'm not inclined to fight this thing too hard.

    Oh, come on, Phil - where's your Don Quixote spirit? Why spend 5 days doing by hand what you can spend 5 months automating?
    ;-D

    We usually have n++ on the servers that have log files I get asked to look at.  So it would be nice to be able to easily deploy this thing.

    Ok. Forgive me if I wax obtuse briefly… but I just have to ask. Why not bring the log file(s) to the server/workstation where your app is, rather than try to deploy the app to all the servers where the log files are? :-/
    (If you tell me the PTB will not allow you to transfer files for analysis purposes, but WILL allow you to log in and install and modify software, my head will explode. Seriously. But, it won't be the first time I've /seen/heard of/been the object of/ such tomfoolery. Say, you're not by any chance contracting at the IRS are you? ) ;-D

    It's ok if n++ locks up while the script is running

    It won't. Just PythonScript will.

    I'll take a look at installing python and see if I can at least get the little demo thing to pop up.  If I can make that go, then odds are good I'll be able to muster the will to take it the distance.

    Aye, laddie! That's the spirit! You can do it!

     

  • Anonymous
    2012-09-06

    The log files are typically several hundred meg.  Copying them over is not impossible, but kind of a PITA.  If I'm able to come up with a plugin that doesn't require installing a bunch of extra stuff then I will probably be able to talk the services team into installing it.  I believe that notepad++ is already part of the standard install, so unpacking a zip on top of that shouldn't be a hard sell.  In my magical dream world, this thing would be easy enough to use that I won't have to use it.  One can dream, right?

     

  • Anonymous
    2012-09-06

    Well, I've got a victory of sorts.  The popup worked.  But I had to modify the tcl code to make it go.  There are two spots where it has require -exact Tcl 8.5.2 and require -exact Tk 8.5.2.  Commented those out and it works. 

    C:/Program Files (x86)/Notepad++/plugins/PythonScript/tcl/tk8.5/tk.tcl: version conflict for package "Tk": have 8.5.9, need exactly 8.5.2
    version conflict for package "Tk": have 8.5.9, need exactly 8.5.2
        while executing
    "package require -exact Tk  8.5.2"
        (file "C:/Program Files (x86)/Notepad++/plugins/PythonScript/tcl/tk8.5/tk.tcl" line 20)
        invoked from within
    "source {C:/Program Files (x86)/Notepad++/plugins/PythonScript/tcl/tk8.5/tk.tcl}"
        ("uplevel" body line 1)
        invoked from within
    "uplevel #0 [list source $file]"
    This probably means that tk wasn't installed properly.
    

    Not sure where it's getting the wrong version from.  I had initially installed Python 2.7.3, and got this error.  So I downgraded to 2.7.1 and still have it.  The ActivePython you recommended doesn't appear to be available in the community edition any longer since it's an old version.  so I grabbed python from python.org.   Difference in distro perhaps. 

    Thanks again!

     
  • Dave W.
    Dave W.
    2012-09-06

    Several hundred meg?!? Holy sh… They need to roll more often dude! Ok, yeah… that kinda takes care of that… In that case that's a good plan. ;-)
    Those Tk versions are a pain… Mine is still in sync because I haven't upgraded my 2.x since 2.7.1. I do remember now seeing that with the 3.x stuff.
    Glad you got it sorted out. I knew you could do it! ;-D