I've tried using both Netbeans and PyCharm, but the same error keeps popping up. The error is when I use end="" as instructed by my tutorial it throws different errors depending on what IDE I'm using. Am I completely typing it incorrectly or can someone give me some advice as to what to do
__author__ = "Kevin"
__date__ = "$May 24, 2015 9:23:37 PM$"
print("Pink", end="")
print("Octopus")
The error code in Netbeans is
No viable alternative at input '='
mismatched input '""' expecting RPAREN
----
(Alt-Enter shows hints)
What's most likely happening here is that you are using a Python3 tutorial, but your interpreter in PyCharm and Netbeans is Python2.
In Python2 print was a statement, whereas in Python3 it's a function.
You should be able to fix this by importing the print function in Python2 from the future module:
from __future__ import print_function
print("Pink", end="") # now works in Python2
Alternativey, you can update PyCharm to use the Python3 interpreter. See here how to set the interpreter.
Related
I tried this code, following along with a tutorial:
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}.")
But I get an error message highlighting the last line, like so:
print(f"Let's talk about {my_name}.")
^
SyntaxError: invalid syntax
Why?
If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.
In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.
If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.
This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output in Python 3.6?.
For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.
I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here
This is a python version problem.
Instead of using
print(f"Let's talk about {my_name}."
use
print("Let's talk about {}.".format(my_name))
in python2.
Your code works on python3.7.
Check it out here:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
https://repl.it/languages/python3
Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3.
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.
I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.
I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.
I had this problem so hopefully, this answer can be of help to those looking for it.
I think they had typed
python file.py
to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type
python3 file.py
That's the solution for the problem
python2 and python3 running command
I am switched from Python 2.7 to Python 3.6.
I have scripts that deal with some non-English content.
I usually run scripts via Cron and also in Terminal.
I had UnicodeDecodeError in my Python 2.7 scripts and I solved by this.
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
Now in Python 3.6, it doesnt work. I have print statements like print("Here %s" % (myvar)) and it throws error. I can solve this issue by replacing it to myvar.encode("utf-8") but I don't want to write with each print statement.
I did PYTHONIOENCODING=utf-8 in my terminal and I have still that issue.
Is there a cleaner way to solve UnicodeDecodeError issue in Python 3.6?
is there any way to tell Python3 to print everything in utf-8? just like I did in Python2?
It sounds like your locale is broken and have another bytes->Unicode issue. The thing you did for Python 2.7 is a hack that only masked the real problem (there's a reason why you have to reload sys to make it work).
To fix your locale, try typing locale from the command line. It should look something like:
LANG=en_GB.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_ALL=
locale depends on LANG being set properly. Python effectively uses locale to work out what encoding to use when writing to stdout in. If it can't work it out, it defaults to ASCII.
You should first attempt to fix your locale. If locale errors, make sure you've installed the correct language pack for your region.
If all else fails, you can always fix Python by setting PYTHONIOENCODING=UTF-8. This should be used as a last resort as you'll be masking problems once again.
If Python is still throwing an error after setting PYTHONIOENCODING then please update your question with the stacktrace. Chances are you've got an implied conversion going on.
I had this issue when using Python inside a Docker container based on Ubuntu 18.04.
It appeared to be a locale issue, which was solved by adding the following to the Dockerfile:
ENV LANG C.UTF-8
To everyone using pickle to load a file previously saved in python 2 and getting an UnicodeDecodeError, try setting pickle encoding parameter:
with open("./data.pkl", "rb") as data_file:
samples = pickle.load(data_file, encoding='latin1')
For a Python-only solution you will have to recreate your sys.stdout object:
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
After this, a normal print("hello world") should be encoded to UTF-8 automatically.
But you should try to find out why your terminal is set to such a strange encoding (which Python just tries to adopt to). Maybe your operating system is configured wrong somehow.
EDIT: In my tests unsetting the env variable LANG produced this strange setting for the stdout encoding for me:
LANG= python3
import sys
sys.stdout.encoding
printed 'ANSI_X3.4-1968'.
So I guess you might want to set your LANG to something like
en_US.UTF-8. Your terminal program doesn't seem to do this.
Python 3 (including 3.6) is already Unicode supported. Here is the doc - https://docs.python.org/3/howto/unicode.html
So you don't need to force Unicode support like Python 2.7. Try to run your code normally. If you get any error reading a Unicode text file you need to use the encoding='utf-8' parameter while reading the file.
for docker with python3.6, use LANG=C.UTF-8 python or jupyter xxx works for me, thanks to #Daniel and #zhy
I mean you could write an custom function like this:
(Not optimal i know)
import sys
def printUTF8(input):
print(input.encode("utf-8"))
I tried this code, following along with a tutorial:
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}.")
But I get an error message highlighting the last line, like so:
print(f"Let's talk about {my_name}.")
^
SyntaxError: invalid syntax
Why?
If you get an error like this from someone else's code, do not try to fix it yourself - the other project simply does not support your Python version (One example: Using pytesseract on Python 2.7 and Windows XP). Look for an alternative instead, or check the project's documentation or other support resources for a workaround.
In particular, the built-in pip package manager has an issue where newer versions of pip require a newer Python version, so old installations cannot upgrade pip past a certain point. See Installing pip is not working in python < 3.6 or Upgrading pip fails with syntax error caused by sys.stderr.write(f"ERROR: {exc}") for details.
If an external tool warns about the problem even though Python supports the feature, update the tool. See Getting invalid syntax error when using Pylint but code runs fine for an example.
This question is specifically about the situation where any attempt to use f-strings fails (the ^ in the error message will point at the closing quote). For common problems with specific f-string syntax, see How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?, f-string formula inside curly brackets not working, Discord.py - SyntaxError f-string: empty expression not allowed, How to use newline '\n' in f-string to format output in Python 3.6?.
For details on alternate approaches to string formatting, see How do I put a variable’s value inside a string (interpolate it into the string)?.
I think you have an old version of python. try upgrading to the latest version of python. F-string literals have been added to python since python 3.6. you can check more about it here
This is a python version problem.
Instead of using
print(f"Let's talk about {my_name}."
use
print("Let's talk about {}.".format(my_name))
in python2.
Your code works on python3.7.
Check it out here:
my_name= "raushan"
print(f"Let's talk about {my_name}.")
https://repl.it/languages/python3
Python Interpreter causes the following issue because of the wrong python version you calling when executing the program as f strings are part of python 3 and not python 2. You could do this python3 filename.py, it should work. To fix this issue, change the python interpreter from 2 to 3.
f-strings were added in python 3.6. In older python versions, an f-string will result in a syntax error.
If you don't want to (or can't) upgrade, see How do I put a variable inside a String in Python? for alternatives to f-strings.
I think this is due to the old version. I have tried in the new version and the executing fine. and the result is as expected.
I believe the problem you are having here is down to you using python 2 without realizing it. if you haven't set it up on your machine to have python 3 as your default version you should execute python3 in your terminal instead of the standard 'python' command.
I had this problem so hopefully, this answer can be of help to those looking for it.
I think they had typed
python file.py
to run the program in the Mac or linux that runs the python 2 version directly because OS defaultly contain python 2 version, so we needed to type
python3 file.py
That's the solution for the problem
python2 and python3 running command
I recently wrote a Python 2.7 script (using PyDev on Eclipse) that took advantage of the built-in ConfigParser module, and the script works perfectly. But when I exported it and sent it to a colleague, he could not get it to work. He keeps getting an "unresolved import: ConfigParser" error even though we are using the exact same settings. This isn't supposed to happen as ConfigParser is built-in.
I've Googled everywhere but could not seem to find any working solution. Any help would be appreciated.
ConfigParser was renamed to configparser in python 3. Chances are he's using 3 and cannot find the old py2 name.
You can use:
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser
To see what's happening it may be nice comparing on both computers which sys.path is being used (i.e.: put at the start of the module being run the code below and compare the output in each case):
import sys
print '\n'.join(sorted(sys.path))
Now, if the error is not when running the code (i.e.: it runs fine and you get no exceptions), and he gets the error only in PyDev, probably the interpreter configuration in his side is not correct and one of the paths printed through the command above is not being added to the PYTHONPATH (it could be that he's on a virtual env and didn't add the paths to the original /Lib or has added some path that shouldn't be there -- or even has some ConfigParser module somewhere else which is conflicting with the one from the Python standard library).
I have a problem with ipython 0.10.1 in ubuntu 11.04 and python 2.7.1.
In previous versions of ipython indentation did not matter when working interactively.
Now instead any indented statement throws an exception:
In [1]: a=3
------------------------------------------------------------
IndentationError: unexpected indent (<ipython console>, line 1)
this is really very annoying specifically when I am debugging and I want to paste some code from a script which is indented, and I am forced to undent every line.
Any hint welcome.
This happens only when ipython is called with -wthread, I've just filed a bug about this.