I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).
Related
So, the shop where I work use Excel 4.0 for all its inventory management and orders.
Since the guy before me left without explaining anything, I inherited an old system that works, but is... eh.
The bosses don't want to change to a new Excel nor another program, so I must do what I can with what I have.
Now, I've made a script in python 2.5.4 (this version is needed because the newer versions won't work on the Windows 98 computer they use...) to automate some processes that would be impossible with Excel 4.0 macros, and the script works perfectly for what I need.
But since the bosses want to "only work with Excel", and won't want to go outside of Excel and click the script icon to start it (or, heaven forbid, open cmd and start it manually), I would need to put a button in Excel to start the script.
I've tried to sift through the macros available, but except perhaps "Initiate" (which I don't wholly understand as of now), I can't think of a macro to interact with the script, and haven't found much help with what's available online...
SO, could anyone please help me in making the macro for the button? The only thing the button would need to do is to start the python script, there's no other interactions needed, the rest is done by the script.
Like, the script "foo.py" is in the same folder as "bar.xls", and I only need a button in "bar.xls" to launch "foo.py".
Thanks.
Okay, I found a roundabout way, so I'm gonna share it with y'all.
MacroName
=LAUNCH("cmd",1)
=SEND.KEYS("foo.py~";TRUE)
=SEND.KEYS("exit~")
=RETURN()
It opens a cmd instance, show it for a split second (can't use SEND.KEYS without it being the active app), writes the name of the python script and presses enter, before quitting.
I would like if it didn't need to show the cmd window, but it works for now. Perhaps there'll be another way, but if anyone else wanna do what I did, it does work.
You probably need to get the book out - Excel 4 came with one book called the Function Reference which listed all the commands available.
Commands that we used back in the day were:
EXEC: starts another program
EXECUTE: runs commands in another program called by Initiate
INITIATE: sets a channel to a program
SEND.KEYS: sends keystrokes to a program (we used to send data to a slow server this way...)
Not sure what will be on the web for Excel macro 4, it was retired as vba came out and Excel moved over...
I still use my copy of the book, but it would be worth finding, although the help should list the commands as well. I just used the book as I had macros running...
From the Python Idle shell, the output from the help() command is not paged. Is there any way to achieve this?
As a workaround I've tried to use pydoc.pager() but with no success. The following works in a normal Python shell started from the terminal but not in the Python Idle shell.
import pydoc
def ihelp(thing):
'''Render text documentation, given an object or a path to an object, and
sends the resulting text to a pager.'''
pydoc.pager(pydoc.render_doc(thing))
Edit: Just to avoid misunderstandings. The ihelp() functions gets the help text both in a terminal Python shell and in the IDLE shell. Paging only works in the terminal shell.
You code works in delivering help output but not in paging it. The pager executes in the user code run process and does not know how to interact with an IDLE shell in a separate gui process. I don't believe you can change this.
To address this issue, IDLE has a new feature, starting with 3.6.7 and 3.7.1 (the release candidates are available now, the final releases will be out in a week or so). IDLE squeezes 'long' output to a button. (By default, 'long' is 50 lines, but user can modify this. Shorted output can be squeezed by right-clicking on it.)
>>> help(int)
[ Squeezed text (241 lines) ] # <== tkinter button
>>>
You can expand the text in place, with a double click, or into the clipboard or a separate non-modal view window, with a right click. A separate window lets one scroll or page up and down in the help entry while writing a statement in the Shell.
Hi all Python developers!
In Eclipse with PyDev it is possible to edit a Python file while debugging. On save, the PyDev debugger will reload the updated code into the running program and uses my new code. How can I do the same thing in JetBrains PyCharm (using Community Edition)?
Eclipse / PyDev writes an output like this when I do that:
pydev debugger: Start reloading module: "MyWidget" ...
pydev debugger: Updated function code: <function close at 0x055F4E70>
pydev debugger: reload finished
I searched settings and web and could not find any hint. Very glad about any idea. Thx.
Edit: I found out in Eclipse/PyDev one has to be in debug mode to be able to use this feature. I tested in PyCharm, but there was no reload done.
PyCharm does not support edit and continue in either the community edition or the professional edition but here is a workaround that I have found while debugging.
Since you can run arbitrary code in the console and/or the expression evaluator, in a lot of cases, you can execute changes to the code without having to restart the application. This isn't exactly like edit-and-continue (which is a feature I really like in Visual Studio and should be part of Pycharm) but it goes a long way towards avoiding having to restart the program from scratch after a change to see if the new code works as expected.
Let me illustrate a couple of the techniques I use:
Let's say you have the following code (with a couple of typos/bugs to illustrate the techniques)
test_value = [10,9,8,7,6,55,4,3,2,1]
for i in range(0,10):
if test_value[i] == i:
print "found the value: " + i
If you run this code, first it errors because you can't print string plus integer but also I wanted to match on 5, not have 55 in the array. So here we go.
Set a break point on the for statement like this and run the code in the debugger.
When it breaks into the debugger, you realize that it should be 5 not 55. Rather than restarting, you can change line 1 to test_value = [10,9,8,7,6,5,4,3,2,1] then select the line, right click and choose Execute Line in Console... which will change the value of test_value to be the array with a 5. Now, the if statement on line 4 becomes true on the value 5. This will then trigger the syntax error on line 5.
Now if you want to make sure you have the syntax correct you can change line 5 to print "found the value: " + str(i), select the line and choose Evaluate Expression... from the right button context menu. When you click Evaluate, the result will show up either in the dialog (or in this case, since it is a print command, in the console)
Now that I've fixed these two issues, I can run the code successfully on the second pass rather than possibly multiple passes it might have taken if I didn't use these techniques. These techniques really pay off if you find a bug deep in the code where it took a while to set up.
Obviously, this is a very contrived example, but hopefully this shows how you can use both Evaluate Expression... and Execute Line in Console... to your advantage while debugging without having to restart your application each time you find a bug in the code.
Also, if you happen to be using Django, PyCharm (professional) will re-launch the server if you make changes to the code. So if you are looking at your web page and notice a problem, you can make a change to the code and switch back to the web page and as you do, either the running application or the debugged application will re-launch and the new code will be running when you refresh the page. Again, not really edit-and-continue but a pretty rapid way to make a change and test.
After all I found a useful and acceptable workaround for my question. It works in PyCharm Community Edition 3.1.2 and I assume it will do in commercial edition as well. I tested on a mid-scale project using Python 2.7.6, PySide (Qt) with one main window and 20+ widgets, tabs, whatever. Follow these steps...
Work in PyCharm on a python project :-)
Execute your code in Debug mode (did not tried Release so far)
Edit some code in one your modules imported during the life of your program
Make your program pause. To achieve this, you can click the "Pause" button of in PyCharms Debug view and then any place in your applications main window where it would need to do something (for example on a tab header). If you have a long a running task and no UI, you may place a breakpoint in a place your program often comes by.
In the Debug view, switch to the Console tab. There is a button on the left Show command line. Click this.
In the console, type in reload(MyModifiedModule) if this call fails, write import MyModifiedModule and try again.
Click resume in PyCharm.
Try the code you fixed.
There are some restrictions on this... It won't fix changes in your main method or main window, cause it won't be created again. In my tests I could not reload widgets from Qt. But it worked for classes like data containers or workers.
May the force be with you as you try this and do not hesitate to add your experiences.
I have the commercial version of PyCharm and just tried testing a simple python script. The script is the following:
for i in range(0,100):
print i
I ran the code in debug mode and placed a break point at the "print i" statement. When the debugger stopped during the first iteration I changed the code to look like this:
for i in range(0,100):
print i
print 'hello'
PyCharm did not reload/re-compile the altered script. Given this simple test my best guess would be that PyCharm does not dynamically reload .py files.
You can add hot reloading features by installing Reloadium plugin.
https://plugins.jetbrains.com/plugin/18509-reloadium
Example use (gif)
It also works without pycharm.
More details:
https://github.com/reloadware/reloadium
IDLE is being very dodgy as to when it will actually show an Auto-complete menu. As of late it hasn't been working at all, or, more specifically, only works during an interactive session.
I've been using Code Blocks for C, and have gotten really used to the very nice auto-complete features, so it's a bit frustrating not having them -- especially while trying to learn a new frame work and what class is associated with which methods, etc, etc..
Is there an easy fix to get IDLE auto-complete working again? I'm using python version 2.7.
Is there perhaps a simple editor I should look into? I've tried Vim, which was a little too heavy for my simple needs, Ninja, which I couldn't get to work for anything, and Sublime text 2, which I couldn't get my wxpython stuff to play with. What would a nice option be? Anything similar to Code Blocks would be cool, although, I'd be perfectly happy with IDLE if it would consistently work!
REQUESTED CLARIFICATION:
OK, so it seems I may have some of my terminology backwards. By non-interactive, I mean, for instance, right clicking on a python file and selecting "edit with IDLE". This brings up what I guess could be described as a text editor. You can enter all you code here. Once ready, you then hit F5, or select Run, and it launches (what I've been referring to as) the interactive terminal. It's here that you can type in code, press return, and instantly have that code evaluated.
What my question is referring to it the former, the part of IDLE where you edit the code. Sometimes while typing, after a . it will display the available methods, or after an open parenthesis it'll give hints as to the values expected. but the thing is, sometimes it does these things, sometimes it doesn't.
The only thing i found so far is that if an editing session of IDLE is connected with python shell (called "interactive mode" in the question, i.e. after an attempt to run the edited script) then "non-interactive" IDLE can autocomplete based on values in interactive window. For example, if I type
a = [];
a.appen
and then hit < Tab > it will do nothing, but if I previously type
a = []
in corresponding python shell, IDLE will autocomplete correctly.
So my only suggestion is import same modules with same names in python shell window in order to make them "visible" for non-interactive IDLE editor.
I had the same problem with IDLE, because I want to learn Qt and therefore autocompletion is very useful.
As it says in the settings of IDLE, you can trigger the autocomplete with "Control + Space", e.g. after a "QtGui.". Then a menu opens where you can arrow-scroll through the entries.
using the 'IDLE Editor window', you need to save and execute your code first.
The application running, turn back to the Editor window to use the auto-completion.
In my case, I had to open Options menu -> Extensions tab on the editor and look to make sure AutoComplete and other relevant options were enabled. They actually were, but by just clicking on 'Apply' even though I dint have to change anything did the trick for me.
Sometimes it has to do with the time you have to wait to get a suggestions.
When you go to options > extensions > general at completions popup wait you can change it to about 500ms.
In my case it was 2000ms by default.
Can someone help me out please...I'm trying to start my first programming project. It will be implemented in python.
I need to have a textbox (which i am using wxpython for). If the user enters any text into this text box, then I want it to appear as arabic. I wanted to this by automagically changing the users Keyboard to an arabic layout when the cursor lands in the given text box.
So i found this pywin32 module, which has a function LoadKeyboardLayout()
So i am trying to test this in IDLE, to see if I can make it accept arabic text into IDLE, to see if it works. So I enter, into IDLE:
win32api.LoadKeyboardLayout('00000401',1)
This then returns, 67175425, the decimal equivalent of hex:'4010401' whcih I believe is the locale ID for Arabic. SO I think wow! I've done it, but when I try typing after this, in the IDLE window, it continues to type normal english characters.
Can someone please explain my errors and guide me towards a good solution.
UPDATE
Okay, I've been trying to solve this problem ever since posting the damn question.
No luck.
Then, I thought, "ok, screw it, instead of testing it quicly in IDLE, I will just try it out, in situ, in my source code for the project."
WTF - it worked first time, giving exact behaviour that I wanted.
Then I tried it in a different IDE, in the interpreted window, and again, IT WORKED straight away!
So clearly my issue is with IDLE, in its interpreting mode.
Can anyone explain why it doesn't work in the IDLE shell???
Keyboard layout setting in Windows is per-process (and inherited from the parent process)
IDLE runs your Python script in a background process separate from its GUI
So you have successfully changed the keyboard layout of the background Python process that is running your script, but not of IDLE's GUI.