SyntaxError when pasting multiple lines in Python [duplicate] - python

This question already has an answer here:
Pasting multiple lines in MacOs Python shell returns SyntaxError
(1 answer)
Closed 1 year ago.
I'm having trouble copy-pasting code in Python. Say I have the following code I want to paste into a Python interpreter:
string_one = "Help"
string_two = "Me"
Locally, running 3.8.5 on a Mac OSX 10.15.7, if I paste that code, I get SyntaxError: multiple statements found while compiling a single statement.
However, if I ssh into another machine, open up Python (3.7.4), and paste those two lines, I get no error. Also, when I paste those two lines into a Python 2.7.16 shell, I get no error. What could possibly be the problem here?
Python 3 (problem shell)
Python 2 (non-problem shell)

It seems that there is a bug in readline (which is used by Homebrew to install python)
Short answer:
echo "set enable-bracketed-paste off" >> ~/.inputrc
Long answer:
https://github.com/Homebrew/homebrew-core/issues/68193
(This was erroneously edited into the question; I have rolled back that edit, and am pasting the solution as an actual answer instead, and tagging as Community Wiki.)

Related

Can't run python program from command prompt even though python 3.9 is installed [duplicate]

This question already has answers here:
Python command not working in command prompt
(24 answers)
Closed 1 year ago.
Command prompt
I tried to run this python code:
#two brown number are an ordered pair in the form (m; n) such that n!+1=m^2
from math import factorial as fact
m=2
n=2
for i in range(10**2): # range of values to check
# checks current values and changes them according to results
if (fact(n)+1)==m**2:
print(m,n)
m+=1
n+=1
elif (fact(n)+1)<m**2:
n+=1
elif (fact(n)+1)>m**2:
m+=1
from the command prompt but it showed me the message on the image above, even though I already installed python 3.9 from the official website.
The python stuff I installed
Anyone knows how to fix this or do I have to reinstall python from the Microsoft Store?
Rename your file containing the code.
There must not be spaces and then use the command:
python3 filename.py
To call files with spaces use the quotation marks around them.
python ".\brown numbers.py"
Generally, it is better to avoid spaces in files names, but sometimes it is unavoidable. In such situations you must explicitly tell Python what is the full file name.
To check if Python is installed correctly run:
python --version

Pycharm not importing standard library [duplicate]

This question already has answers here:
Difference Between Python's IDLE and its command line
(4 answers)
Closed 1 year ago.
But they work just fine in IDLE. For example,
import time
time.asctime()
works fine in IDLE, but is not working in Pycharm.
I have tried changing the interpreter but still doesn't work. Other packages that I installed myself from cmd like numpy are working fine.
Any help will be appreciated
EDIT : When I add the package time by pressing + in the interpreter settings, it says
ERROR: Could not find a version that satisfies the requirement time
If you get no error message, and you have shown us your whole program then your problem is that you are expecting time.asctime() on its own to produce output the way it does in IDLE.
But to get the same effect in a program that PyCharm runs, you need something like print(time.asctime()).
To get PyCharm to do what IDLE does in this case, you need to ask PyCharm to open a Python console.

Opening Python code with a specific version [duplicate]

This question already has answers here:
Multiple Python versions on the same machine?
(11 answers)
Closed 3 years ago.
while there are multiple versions of Python installed on a Linux machine, is there a way to mention in the script to be open with a specific version, say, 3.8 even if we issue the #python script.py as opposed to python3.8 script.py ?
I don't want to use Linux alias command. I wanna know if that is possible to be accomplished within the script
Use shebang. #!/usr/bin/python
In the first line of the code with the serial number of Python you want at the end.
You'll need to then run your script like ./my_script.py
You can select the interpreter by adding
#!/usr/bin/env pythonXX
as the first line in your script, provided the version is in the path.
You can also invoke the interpreter directly with the script as the argument.
pythonXX script.py
Depending on your situation
pythonXX -E script.py
might be better. That way the interpreter ignores paths set by environmental variables.
For more details view
https://docs.python.org/3/using/cmdline.html

VS Code - can you display a Python shell [duplicate]

This question already has answers here:
How to execute Python code from within Visual Studio Code
(34 answers)
Closed 4 years ago.
When using Python IDLE I find the Python shell, with the >>> prompt very useful for testing syntax. Is there a way of getting a Python shell integrated in VS Code?
Maybe the answer is just to open Python IDLE in another window.
Apparently this is a duplicate of another question, but I did several searches on the words Python shell and didn't find anything that seemed relevant. Sorry.
Many thanks to Jaxi for the answer that you need to type Python in the Terminal Window.
stated here you can use ctrl+backtick to open a terminal window, then from there just type python and it will give you the python shell:
https://code.visualstudio.com/docs/editor/integrated-terminal

Python3.6 on Windows MINGW64 console doesn't run the interpreter [duplicate]

This question already has answers here:
Python not working in the command line of git bash
(20 answers)
Closed 5 years ago.
I'm experiencing the same issue as this question with Python 3.6. It works well under the Windows console, but it fails to launch or work under MINGW64. Even when I use the direct path of /c/Python36/python.exe, nothing happens. I don't get the interactive REPL with the usual Python interpreter version and >>> prompt, as I do in the Windows console.
Any pointers on how to debug this?
Ah - further search yielded this stackoverflow question that contained the answer - use python -i

Categories

Resources