I'm running a Python program that wants to accept raw_input which Ipython notebook does not do. (a known limitation)
What is a recommended way to achieve the functionality? (work around?) What I'd like to do is to be able to run the program, accept input and respond..(will be choices determined based on information retrieved), and also prompting for user id and password info..
Of course I'd like to do as little violence to the existing code as possible.
I found IPython.utils.io.raw_input_ext(prompt='', ps2='... ') in the Ipython docs but it calls raw_input and gets the same not implemented error
Last developement version of IPython now support raw_input in notebook. (since beginning of may 2013 for future reader)
Related
I am new to Python, have some experience in MatLab and r. My question is: Is it possible to run part of the code in .py block by block (line by line)?
In r or Matlab, I can first have some data and variables loaded in the memory first. Then experimentally I can run a line or two to try out the syntax... this is particularly useful for new learners I believe. I know there is something called the iPython which can execute Python code line by line however this is not what I am after. Thanks.
Since ipython has already been discounted, I'm not sure this answer will be better. But I will tell you the two things that I do.
I drop into the debugger at the point where I want to "try out" something, so the code will run up to that point, and then drop me into the debugger. You do this simply by inserting this code at that point:
import pdb; pdb.set_trace()
Once you've done what needs to be done, you can either press q to quit, or c to continue running the process.
I use the -i option to python. This enters interactive mode at the end of your python code. This is useful if you want to set up a bunch of data structures, and try out some code on it, instead of typing all of it into a python shell first. (that might be why you rejected ipython?)
I think what you need is a debugger.
You can use the pydev plugin for Eclipse which has a debugger.
Another option is pdb as already suggested but it's not very easy to use.
I am trying to hot swap a file in python. I am creating a game that takes a really long time to load. But I don't want to reload it every time. I am trying to change some code while the programme is in runtime.
For example:
I want to change this:
while True:
print("Hello")
to this while in runtime:
while True:
print("Hello World")
I looked hot swapping up for python and all of them are answers that I am not looking for. All the other answers change modules. I want to change the current file. Like java in eclipse. Please help!
I want to change the current file. Like java in eclipse.
When you modify Java code in Eclipse, the code is not currently running. In reality, Eclipse has a Java compiler built-in, which attempts to compile your code as you type it. That's how Eclipse is able to give you such fast feedback about syntax errors and type errors in your code. But it can't give you any information about the runtime behaviour of your code (whether or not it produces the right answer), because it doesn't run the code! You need to press the Run button for that.
So I think the question you really want to ask is not "Can I dynamically modify Python code?" (the answer to that is "Yes, but it's complicated and has caveats and is not a good idea") but "Does there exist a Python IDE which can give me feedback about syntax errors while I type?" The answer to that is an emphatic "Yes!". There's an extremely comprehensive list of options in this answer.
You can use dynamic execution using exec function.
Store the code you want to execute in a string and change it during runtime.
I'm a new Python user, and I'm sure this is a really basic question, but I can't find the answer anywhere. When people post Python code online it is often formatted like this:
In [1]: # some stuff
Out[1]:
# some more stuff
What are the In's, Out's, and the numbers? And why does my Python console not behave like that?
They are not Python code. They are IPython prompts, a popular Python add-on interactive shell.
Each line of code executed on the interactive prompt (denoted by In) is numbered, and so is the output produced (denoted by Out). You can then instruct IPython to refer back to those inputs and outputs for re-running code or re-using output.
See the Input caching system documentation>
IPython offers numbered prompts (In/Out) with input and output caching (also referred to as ‘input history’). All input is saved and can be retrieved as variables (besides the usual arrow key recall), in addition to the %rep magic command that brings a history entry up for editing on the next command line.
I am writing a command line interface in python that accepts a lot of user input. For the values that I am querying the user about, there is a significant amount of "additional information" that I could display, but would rather only display if the user needed help with how to provide a value.
So I thought I would provide my usual raw_input prompt, but also try an accept some Ctrl-H type sequences to output this help info.
Can Python accept this kind of input via raw_input in a terminal/shell? It there another more proper way to do this (preferably in the stdlib)?
No, python cannot accept this kind of input through raw_input. This is because you're thinking about sequences like: Ctrl-C, Ctrl-Z, etc. These are not keyboard inputs, these are signals that are processed by the terminal (not the program).
You can try to set up signal handlers that will do this for you, but that is not a very reliable solution (regardless of whether you're using python or something else).
The best solution for accepting this kind of input is to either use curses, or use readline (with adjustments to the configuration to handle things like Ctrl-H). Using readline will make your life much easier, but it comes with the cost that you have to license your program under the GNU GPL (or similar). Whereas curses does not have this kind of restriction.
I'm writing program in python in which user is to work with program by command-line. I'm using raw_input to get command from user. I want to have "memory" like in bash, etc, so, if you press an arrow (up or down) on your keyboard, you get previous/next command. I know about one way to do it (simply get every char typed by user and check it), but maybe you know something better / cuter :-)
greetings
If I understand what you want, you can achieve it simply by importing the readline module. This will modify the behavior of raw_input() so that it behaves more like the python interactive shell in terms of history and line editing.
Be careful though, it's possible to build python without readline so I'd suggest importing it inside a try block:
try:
import readline
except:
pass #readline not available
The built-in readline module provides this functionality.