On 31:43 of Python 3 Metaprogramming youtube video David Beazley advances to a new line of Python IDLE (31:43) without forcing an execution. What keyboard key or combinations of keys is used to make it work?
If you open an indent block in the IDLE, hitting Enter will not execute the line.
>>> class A(Base):
...
Hitting Enter after an empty line will execute the whole block of code.
>>> class A(Base):
... pass
...
>>>
He's not typing anything special; Idle doesn't end the class definition (started by the class keyword) until you enter a blank line.
In the video you linked, David is not using Python IDLE (Integrated Development Learning Environment), which is an instance of an IDE, he is using the Python Interpreter which is a binary executable for the interactive Python shell for executing python statements.
In all of these interactive interpreters, when you have an expected indent, usually in loops, conditions and in your case a class, the line ending in a colon : will not execute when you hit the Enter key it will allow you to keep typing in the next line until you hit Enter twice.
Related
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
This is a basic example for defining a function, and when I run this code on Python 3.8.5 by VSC, the result is like below.
>>> def greet(name):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
It says that there is an indentation error but I really don't know why. And also, line 2 is not just blank but according to the result, line 2 seems like blank.
for number in range(5):
print("Thank you")
>>> for number in range(5):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
Same error for 'for'. Can anyone help?
Right now, you are running the Python script line-by-line by placing the VS Code cursor on each line of code and then pressing shift+enter. This utilizes the Python Interactive window afaik. It works fine for running individual lines but if you want to run a collection of lines that include indentation e.g. a for loop or function definition, then select all of those lines together and then press shift+enter. You would need to do this for code that's indented, for example:
def greet(name):
print("Hello, " + name + ". Good morning!")
Alternatively, if you have read Getting Started with Python in VS Code and specifically installed the Python extension for VS Code then you will be able to click the Run Python File in Terminal button (it looks like a Play icon) to run your Python script.
Alternatively, you can launch a Terminal Window in VS Code and manually run the Python script using python3 hello.py (or potentially python hello.py, depending on your Python installation).
You should not press Shift+Enter to run the code in the terminal line by line.
Because the REPL will add an Enter to start a new line automatically like this:
You can select all of the codes then send them to the REPL:
I made a simple Python 3 script which takes an input from the user. But while entering the input, if I press left arrow key, instead of going left it prints ^[[D . It happens with all arrow keys. But it doesn't happen in Terminal or Python Interactive Shell, it only happens when I run a Python script from Terminal and need to enter an input.
I use Ubuntu 19.10 and Anaconda distrubition which runs Python 3.7.
operation = input("Enter the expression: ")
How can I fix this?
Import the readline package before using input
import readline
operation = input("Enter the expression: ")
https://docs.python.org/3/library/readline.html
Settings made using this module affect the behaviour of both the
interpreter’s interactive prompt and the prompts offered by the
built-in input() function.
Importing it will be enough to activate input line editing (arrow keys will move the cursor around instead of printing ^[[D, etc.). Other functions in the readline module can be used to set up tab completion and a history file.
In - Python, inside the the IDLE, in the File Editor Window,
How do you run just a selected single line of code in the script, without having the rest of the lines of the program being runned ?
You'll have to run your line of code form command line:
With the -c (command) argument (assuming your file is named foo.py):
$ python -c 'import foo; print foo.hello()'
You can select and copy a single statement in an Idle editor (or anywhere else, for that matter), switch to the Idle Shell, and paste on the line with the >>> prompt as the bottom. (You can hit to get a clean prompt.) Then hit return just as it you had entered into the shell directly. This works for multiline statements. Being able to do this with a menu selection, hotkey, or right-click selection is on my todo list, as you are not the first to ask about this.
Basically, what we call "debugger" should be fulfilled your requirement. Many IDEs are provided "debugger tools" including IDLE. To use a debugger tool you can just simply
add break lines (any lines of code you want a program to stop while program is running),
explore your variables (when program is stop you can print out values of your variables at that time)
Click/type step to go to the next breakpoint.
This is just a roughly procedure that should be fit to your requirement.
see more: debug
My class assignment is to read a file called key.txt on a server which is in the same directory of a python script it is running on port 2323. The code running in the script is as follows:
while 1: print eval(raw_input("Enter Math:"))
I'm connecting with PuTTY and every time I run any code, the connection instantly drops if the code I pass is invalid. It gives no explanation, but I assume the eval function couldnt parse my code.
Here are some of the things I've tried and their outputs:
Entering open('key.txt', 'r').read() (or any explicit code) killed the connection
Using chr(#) to pass in commands, ex. hello = chr(104)+chr(101)+chr(108)+chr(108)+chr(111). The server just spits back whatever I type
Using compile by entering compile('print "Hello!"', 'buttfile', 'exec'), with the output <code object <module> at 0x7f6270ac0db0, file "buttfile", line 1>
Those are the only two ways I can think of that allows me to pass in code. I wrote a small cpp program to convert whatever I type into the char combinations, as well as including newlines so I can enter multiline code with the chr() method.
So my question is how would I execute code to read a file through python's eval function?
If you are connecting to a linux system you can do it in two commands:
__import__("os").system("locate key.txt")
This assumes that the locate db is up to date.
Then when you know the location just use:
__import__("os").system("cat /location/of/file/key.txt")
Which will output the key to the screen.
I have 3 lines and I want something like in visual studio where I can execute first line, and then by mouse move execution step to 3rd line and execute it without executing second line.
So in this case
print('a')
print('b')
print('c')
I want to have in an input:
a
c
Now I'm using wing ide 4 trial and it hasn't this option.
Wing IDE has a contributed add-on script that supports this:
http://wiki.wingware.com/DebugMoveProgramCounter
Drop it into the 'scripts' directory in your user settings directory (which varies in location but is listed 5th in Wing's About box) then select Reload All Scripts from the Edit menu.
Probably easiest to assign a key binding to the command jmp_line (in Keyboard / Custom Key Bindings preference), since it works relative to caret position.
You can do this with the "Run to Line" command in Eclipse Pydev IDE, which is also free and open source. Note that the debugger cannot jump to a line inside certain blocks, like for/while loops, finally clause, etc.
Given your example, use the following sequence of commands:
Set a breakpoint at print('a').
Run your script in debug mode, menu Run > Debug.
The script should stop execution at print('a'). Run Step Over to execute that line.
When the execution pointer advances to the start of the print('b') line, move the cursor down to print('c') (or whatever line you want to execute next) and select menu Run > Run to Line. The execution pointer should jump to the start of that line.
Now run Step Over or Resume to execute print('c').
Output of the above sequence:
a
c
It is also possible to do the same thing from the command-line using Python debugger pdb, specifically the jump [lineno] command.