I am entirely new to this (this website as well as programming), so this is probably horribly worded. I should also note that I am using python on an RPi2 so I can't paste anything. The problem is that python ignores the assignment operator when I type it into the editor, but understands it when typed into the shell. For example, if I type
x = 5
x
into the shell, the shell will respond with
5
Whereas if I type the same thing into the editor window and run the module, the shell responds with the nothing, just the restart bar and then the three arrows.
==========RESTART==========
.>>>
I can't find any information on this and I never encountered this problem using python on my desktop.
When you just type x in your program, you basically do nothing. If you want to print it, use print x to explicitly tell python to print it. Note that having a plain x in your module would be a valid python statement (though if you were using pyflakes or pylint or other some such tool, it would cry out loud that you are not doing anything in that statement).
In the shell, this typing x works simply because it is the shell which supports this, it is the shell's feature.
Related
This is a dumb question but I'm new to Python and couldn't figure out why my codes only works with Jupyter notebook and not in other IDEs or Text Editors. For example, I realized I always have to use the "Print" function to get my output in PyCharm and Sublime Text3 while I can just run values without the print function in Jupyter Notebook.
Here's a simple example:
In Jupyter Notebook, I can simply run the codes without the print function
x = 10
y = 20
x+y
if i run this, i still get my output, which is 30.
But if I do the same thing in PyCharm or Sublime Text3, I don't get the output. It just says [Finished in X.Xs] without printing my output and I always have to use the print function to get the output.
x=10
y=20
print(x+y)
I wonder what causes the difference. At first, I thought it was because of the type of software I was using and realized both Pycharm and Jupyter Notebook are IDEs. Do I need to change settings in order to make my codes work in Pycharm or Sublime Text3?
Thank you.
Jupyter Settings is like that. It evaluates your last line of code, if it is 'None' prints nothing but if not None, would just get printed. Only the last uncommented expression or line gets printed nothing intermediate.
Python essentially operates in two modes - script execution mode and interactive mode, otherwise known as REPL (Read, Evaluate, Print, Loop). Jupyter is set up by default to run in REPL mode, where the results of operations are printed to stdout (see this question to change this behavior).
In contrast, PyCharm and Sublime Text execute scripts straight through in non-interactive mode, and only print to stdout when explicitly directed to do so, such as with a call to print(). PyCharm has a REPL mode, as far as I know, and in Sublime you can use the SublimeREPL plugin, available from Package Control.
I run my script with Python Console in PyCharm and expect it to show global variables that are being used by script itself. But somehow it seems like it uses some different set to work with / different scope / namespace.
Here is a sample script:
def addb():
global b
b=1
def checkb():
print('b' in globals())
I go to 'Run > Edit Configurations... > myscript' and check 'Run with Python console' there, press Run - Python Console opens - check Show Variables button and list of some global variables appears on the right. Then I write in prompt and get this response:
>>>'b' in globals()
False
>>>checkb()
False
Everything fine, no 'b' variable in variables list on right pane as well. Then I write and get this response:
>>>addb()
>>>checkb()
True
>>>'b' in globals()
False
Also, still no 'b' variable in variables list on the right pane. And this goes further, of course. This variable seems to be clearly existing and have appropriate value for functions and methods inside module, but not for Python Console. This is the most obvious example of such behavior. It goes further and bring undesirable consequences when I use some functions to change global variables state and use them in console. The variables changes their value "inside" of script, but remain the same state in console, etc.
What am I expecting to have is all actual script global variables' state be visible in variables list on the right pane of Python Console and be accessible from console so
>>>addb()
>>>'b' in globals()
would yield
True
I checked this in Python own console directly with
python -i test.py
and had exactly what I expected to have like I described above. So it's exactly PyCharm Python Console issue.
It looks completely like a bug behavior, but I just can't believe it is since it is too big and obvious not to notice so I just believe I missed something and decided to ask here first. Can't find anything relating this case in Google as well so I just stuck here. Would appreciate any guide or confirmation of it's buggy nature so I can raise an issue. Please let me know if you can't reproduce this issue as well.
I use PyCharm 2018.3.4 (Community Edition) Build #PC-183.5429.31, built on January 29, 2019 and Python 3.7.2.
In a gdb python script, how do I set or modify the commands list for a breakpoint?
I can do this trivially by hand. For example to change the commands list for breakpoint 2, at the prompt I can enter:
(gdb) commands 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>info reg rax
>set $rax=0x1234
>end
Yet, from a gdb python script I cannot seem to execute a multiline command.
gdb.execute("commands 2\ninfo reg rax\nset $rax=0x1234\nend\n")
Just gets me an output like
(gdb) source blah.py
>
and it is sitting there for more input. It won't move on until I type end and press enter. Then it just gives complaints making it clear it has not correctly parsed anything after the commands 2 of that string.
Trying to input each line separately doesn't help. For example the script:
gdb.execute("commands 2")
gdb.execute("info reg rax")
gdb.execute("set $rax=0x1234")
gdb.execute("end")
waits for more input from the user during the first execute, and so has similar problems. And while mostly wishful, the following doesn't work either:
gdb.execute(["commands 2","info reg rax","set $rax=0x1234","end"])
It is easy to programatically get the list of breakpoints with gdb.breakpoints(). And these objects have a property commands, which I can see any commands I set by hand or from a native gdb script. However if I try to modify this property, it fails. So once something is set by hand, or a gdb script, there appears to be no way for a python script to edit it. The API document is missing a lot of helpful information, but it does state https://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html#Breakpoints-In-Python Variable: Breakpoint.commands ... This attribute is not writable.
And no, I don't consider it a useful answer to "never use gdb scripts" or "never enter commands by hand" and "instead always write a python gdb.Breakpoint subclass with Breakpoint.stop() set to do something special, and rewrite all existing scripts to use such features". I can only seem to find information for that workaround. But I'm not willing to give up current methods of interaction just due to a gdb quirk.
Since I can easily run commands to do what I want by hand, there must be a way to modify or set the breakpoint commands from a gdb python script. How do I do this?
I don't think there is a way at present.
As you found, gdb.execute doesn't support multiple lines. This is just a limitation in the Python layer that nobody ever fixed. (I didn't see a bug for it so I filed bug 22730.
Also, a breakpoint's commands field is not assignable. I filed bug 22731 for this.
Your source idea, while horrible, would work fine.
I'm using python.el
If I choose 'debugger' from the menu, and enter 'python -m pdb myfile.py', gud starts, and in a split frame I see the (Pdb) prompt in one, and my python code in the other with a caret on the first line, indicating that it's ready to go. For example 'n' steps to the next line and the caret moves accordingly.
If instead I enter 'python -m ipdb myfile.py', the frame splits, and one split is labeled gud, but there's no ipdb console evident. In other words, this way of starting ipdb doesn't seem to work. Ipdb works just fine if I manually insert a breakpoint into my python code using ipdb.set_trace(), except that it does not use the gud interface. Is this intentional so that ipdb's stack trace will work nicely?
If so, that's fine, but is there a way to start ipdb from emacs without manually adding a set_trace() command?
The basic problem here is that gud is looking for a (Pdb) prompt and ipdb doesn't prompt this way. There are three ways to fix this: fix ipdb to give a (Pdb) prompt, fix gud not to need to look for (Pdb) or (my favorite) use something else either on the gud side or on the ipdb side.
The problem with fixing up gud is that it is rather old and to my mind a bit creaky using global variables and not making use of Emacs Lisp data structures available other than lists and cons cells. A total rewrite of gud is called realgud, it is currently in MELPA and ELPA. And ipdb is supported.
The last option is to use something else, so let me suggest the Python trepan debugger which is already integrated into realgud (but not gud since I consider that a dead end). Although the backtraces it gives are not exactly like ipdb's, it does colorize them and the source code.
And recent versions of trepan3k backtraces will even show, on demand, you where in the line you are. So if you had say two calls of a function, like fib() it would distinguish which of the calls function was the one in progress.
When I am working with a Python Interpreter, I always find it a pain to try and copy code from it because it inserts all of these >>> and ...
Is there a Python interpreter that will let me copy code, without having to deal with this? Or alternatively, is there a way to clean the output.
Additionally, sometimes I would like to paste code in, but the code is indented. Is there any console that can automatically indent it instead of throwing an error?
Related
Why can I not paste the output of Pythons REPL without manual-editing?
IPython lets you show, save and edit your command history, for example to show the first three commands of your session without line numbers you'd type %hist -n 1 4.
WingIDE from Wingware will let you evaluate any chunk of code in a separate interpreter window.
IPython will let you paste Python code with leading indents without giving you an IndentationError. You can also change your prompts to remove >>> and ... if you wish.
I have a vim macro to "paste while cleaning interpreter prompts and sample output [[==stuff NOT preceded by prompts" and I'll be happy to share it if vim is what you're using. Any editor or IDE worth that name will of course be similarly easy to program for such purposes!
Decent text editors such as Notepad++ can make global search and replace operations that can replace >>> with nothing.