I'm trying to get SublimeREPL to work with Python. However, whenever I send a Python command to the REPL with the keyboard shortcut, only output from stdout is displayed. The evaluation of the command should also be displayed, but isn't. Here is a succinct example, copy and pasted from the output in the REPL after sending the commands to the REPL from Python source via the keyboard shortcut.
>>> print 'This will print out'
This will print out
>>> 'But this will not'
>>> 1+1
I would have expected the following output (running manually from BASH terminal in Python produces this):
>>> print 'This will print out'
This will print out
>>> 'But this will not'
'But this will not'
>>> 1+1
2
One last note, manually typing commands directly into the Sublime Text REPL produces the desired output, but is much slower and inconvenient.
I'm running this using SublimeText3 with the default Python 2.7.5 interpreter on Ubuntu 13.10.
This is happening because the REPL is actually evaluating your code in the exact same manner that it would if you put all those commands in a file and ran it from the command line - it is not behaving as an interactive interpreter in this case.
If you want the REPL to behave more like IDLE, for example, you need to transfer your code to it, then switch over and run it from there, simply by adding Shift to your key sequence. So, if previously you were using Ctrl,, S to evaluate a selection, just use CtrlShift,, S instead to transfer your selection to the REPL. Switch tabs and hit Enter, and it should behave as you are expecting.
Related
I'm starting in Python with Spyder (I just did some work on Jupyter and it was ok ...), but when typing any code in the Editor, the command lines are always shown "runfile ('C: /Users/alexandre.lustosa/ Without title1.py '), instead of the code.
What should I do?
Below is a very simple example of the above problem ...
Thank you!
enter image description here
Well, ok here we go:
The window in the bottom right corner (where the runfile-message appears) is the actual console of python. This is where you type in a command and get output back:
In [1]: print("hello")
hello
In [2]: a = 10
In [3]:
In [4]: print(a)
10
As you can see, just assigning a variable won't return any output, even though python has a=10 internaly saved. To actually get some output which is apparently your aim, you have to call a function which returns something. For example the already built in type() function:
In [5]: type(a)
Out[5]: int
However, you have written your code in the left window, thats not a console but a python file (in your case called titulo1.py), shown by spyder. Spyder knows it's a python-file and therefore highlights the syntax.
When you now click the run icon in the top menu bar, spyder passes your file to the python console in the bottom right corner and the code in the file is then executed.
You could also manually type in the command runfile(filename) in the console. The run-symbol just saves time.
End of story:
Spyder is successfully executing your code, but doesn't return anything, because a variable assignment (a=10) simply doesn't return anything.
Tip:
You can activate the variable explorer of spyder, which allows you to watch all currently assigned variables:
I wrote the following code in Python IDLE, using its editor
import urllib.request
print(urllib.request.urlopen('https://github.com').read().decode('utf-8'))
and then saved the code as a script. After I ran the script, Python shell displayed the page source I want.
When I changed the above code to:
import urllib.request
urllib.request.urlopen('https://github.com').read().decode('utf-8')
and then ran the script, Python shell displayed nothing. This is understandable.
The weird thing for me, however, is that if I run the above code (the one without print) interactively in a Python shell, Python shell can still display the page source, as you see:
I don't understand why.
I use Python 3.6.3.
You are using the interactive python prompt. It automatically prints return values for you so that you can see the result of what you're doing.
Try typing 3 + 2. You know this doesn't print anything either - but you will see the result.
Likewise, if you put those two lines into a file you won't get any output.
It's because the shell is printing the result of execution. If you assign the function call to a variable then it will suppress the printing.
i.e. html = urllib.request.urlopen( ... )
I haven't been able to figure out what the >>> does, even though I often see it often in source code.
You won't see it in source code, it's probably documentation. It indicates an interactive session, and things typed into the 'interpreter' are marked with this. Output is shown without the arrows.
In fact, the python documentation often has a button >>>at the top right of example code to be able to hide the arrows (and output) so that you can copy and paste the code.
Shown:
Hidden:
'>>>' is the prompt of the interactive Python interpreter, meaning that the interpreter is ready to get Python statements typed in. It's occuring quite often in examples within the documentation of a Python program, in order to show which commands can be used and what will be the result of giving these commands to the interactive interpreter. For example, in a documentation of the print statement, one could give this example:
>>> print "Hello world."
Hello world.
This would be an actual snippet of a session with the interactive Python interpreter.
An interesting feature in IPython is that it ignores leading >>>, meaning that you can copy and paste code from such documentation without needing to remove the leading >>>:
In [1]: >>> print "Hello world."
Hello world.
(The prompt in IPython is In [n]:, where n is counting the interactive commands issued.)
Here are some of my findings on >>> and consequently ... complementing the previous answers.
You only see >>> when you are running Python in interactive mode prompting/asking the user for the "next command". Technical details here.
>>> and ... are not written in stone. These are stored in sys.ps1 and sys.ps2, and therefore can be changed. Further elaborated here.
>>> import sys
>>> sys.ps1 = "$ "
$
Every standard Python has this prompt unless you compile your own Python after changing >>> and ... to what you (sanely) wish to. Apart from that there seems to be a way to change it for all future interactive sessions by changing /usr/lib/python2.7/code.py but I couldn't find any success with it.
The >>> prompt is the Python interpreter’s way of asking you, “What do you want
me to do next?”, and it is called "chevron" prompt
In case you are trying to figure out how to exit a session, run this:
quit()
I found it to be called ' REPL'
>>> def f(x):
... print x
... f('hello')
File "<stdin>", line 3
f('hello')
^
SyntaxError: invalid syntax
>>>
I'm at the prompt in the Python shell. Why doesn't the above code work?
Enter a blank line after "print x". Generally, the ... prompt indicates that Python expects further input for the current block, in this case the function f.
As pointed out by Iguananaut IPython has superior editing capabilities compared to the standard Python shell, for example tab auto completion.
You need to press enter twice after the last line to get back to a >>> prompt in which case you can enter new expressions.
Also, if you're going to by typing lots of multi-line expressions into the interpreter you should give IPython (now with funding!) a look. It supports much better editing of multi-line statements, and even better still if you use the qtconsole or the notebook.
I've seen similar questions (e.g. Running a command in a new Mac OS X Terminal window ) but I need to confirm this command and its expected behavior in a mac (which I don't have). If anyone can run the following in Python 3 Mac:
import subprocess, os
def runcom(bashCommand):
sp = subprocess.Popen(['osascript'], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
sp.communicate('''tell application "Terminal"\nactivate\ndo script with command "{0} $EXIT"\nend tell'''.format(bashCommand))
runcom('''echo \\"This is a test\\n\\nThis should come two lines later; press any key\\";read throwaway''')
runcom('''echo \\"This is a test\\"\n\necho \\"This should come one line later; press any key\\";read throwaway''')
runcom('''echo \\"This is testing whether I can have you enter your sudo pw on separate terminal\\";sudo ls;\necho \\"You should see your current directory; press any key\\";read throwaway''')
Firstly, and most basically, is the "spawn new terminal and execute" command correct? (For reference, this version of the runcom function came from this answer below, and is much cleaner than my original.)
As for the actual tests: the first one tests that internal double escaped \\n characters really work. The second tests that we can put (unescaped) newlines into the "script" and still have it work just like semicolon. Finally, the last one tests whether you can call a sudo process in a separate terminal (my ultimate goal).
In all cases, the new terminal should disappear as soon as you "press any key". Please also confirm this.
If one of these doesn't work, a correction/diagnosis would be most appreciated. Also appreciated: is there a more pythonic way of spawning a terminal on Mac then executing a (sudo, extended) bash commands on it?
Thanks!
[...] its expected behavior [...]
This is hard to answer, since those commands do what I expect them to do, which might not be what you expect them to do.
As for the actual tests: the first one tests that internal double escaped \n characters really work.
The \\n with the doubled backslash does indeed work correctly in that it causes echo to emit a newline character. However, no double quotes are emitted by echo.
The second tests that we can put (unescaped) newlines into the "script" and still have it work just like semicolon.
That works also.
Finally, the last one tests whether you can call a sudo process in a separate terminal (my ultimate goal).
There is no reason why this should not work also, and indeed it does.
In all cases, the new terminal should disappear as soon as you "press any key". Please also confirm this.
That will not work because of several reasons:
read in bash will by default read a whole line, not just one character
after the script you supply is executed, there is no reason for the shell within the terminal to exit
even if the shell would exit, the user can configure Terminal.app not to close a window after the shell exits (this is even the default setting)
Other problems:
the script you supply to osascript will appear in the terminal window before it is executed. in the examples above, the user will see every "This is a test [...]" twice.
I cannot figure out what $EXIT is supposed to do
The ls command will show the user "the current directory" only in the sense that the current working directory in a new terminal window will always be the user's home directory
throwaway will not be available after the script bashCommand exits
Finally, this script will not work at all under Python 3, because it crashes with a TypeError: communicate() takes a byte string as argument, not a string.
Also appreciated: is there a more pythonic way of spawning a terminal on Mac [...]
You should look into PyObjC! It's not necessarily more pythonic, but at least you would eliminate some layers of indirection.
I don't have Python 3, but I edited your runcom function a little and it should work:
def runcom(bashCommand):
sp = subprocess.Popen(['osascript'], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
sp.communicate('''tell application "Terminal"\nactivate\ndo script with command "{0} $EXIT"\nend tell'''.format(bashCommand))