I am trying to run a function called read_distribution.py in a Python package called RSeQC. However when I run the following command:
python3 read_distribution.py -i mysample.bam -r hg38_RefSeq.bed
I get the following error:
File "distribution.py", line 278
print "%-30s%d" % ("Total Reads",totalReads)
^
SyntaxError: invalid syntax
Lines 275-282 in the read_distribution.py code look like this:
except StopIteration:
print >>sys.stderr, "Finished\n"
print "%-30s%d" % ("Total Reads",totalReads)
print "%-30s%d" % ("Total Tags",totalFrags)
print "%-30s%d" % ("Total Assigned Tags",totalFrags-unAssignFrags)
print "====================================================================="
Is this a problem with my python version? I do not know enough Python to figure out the problem so any help is appreciated-Thanks!
I bet you're using Python 3.X. Starting with 3.0, the print statement became a function, requiring parentheses to be used like when calling any function. So the code you show needs to look like this to work in Python 3.X:
print("%-30s%d" % ("Total Reads",33))
print("%-30s%d" % ("Total Tags",33))
print("%-30s%d" % ("Total Assigned Tags",12))
print("=====================================================================")
There are scripts on the internet that will convert much of your Python 2.X code to 3.X if you have a bunch more of it to convert. Alternately, if you got the code from somewhere else, maybe they have a Python 3.X version available.
I believe this package was written in Python 2, which didn't have you putting ()'s after print, in Python 3 this changed to have you put ()'s after print, You're using Python 3.
Related
I have an invalid syntax according to flake8, pylint, but the code works.
What's wrong with that code?
I did lots of Google search but couldn't find anything.
#!/usr/bin/env python
with open("test.py", "a") as output:
# E: 4, 0: invalid syntax (<string>, line 4) (syntax-error)
print("hello world", file=output)
What version of Python are you running? I'm not sure when it was implemented exactly but I don't think earlier versions of Python had a file=output parameter for the print() function so your interpreter might only be expecting a string
I am learning python34 and I read here in my course book the following line:
"The comma separating the two print() commands in this file instructs Python to no start a new
line."
I am guessing by "no" they actually mean not?
Using the next script:
#!/usr/bin/env python3
print ("Hello from a Python file!"),
print ("Welcome to Python!")
And I have the following example of its execution on mac:
$ python3 ~/Desktop/Python/Hello.py
Hello from a Python file! Welcome to Python!
But my windows8 gives me this output:
c:\f>a.py
Hello from a Python file!
Welcome to Python!
I am running the file via cmd without the Python command (as shown above), since it doesn't work otherwise...
I added manually the system Environment Variables path to Python default installation
folder (C:\Python34)
The comma doesn't work in Python3, see what's new in Python 3. Also see the docs for print function in Python 3.
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Your example would be:
>>> def test():
... print("Hello from a Python file!", end=" ")
... print("Welcome to Python!")
...
>>> test()
Hello from a Python file! Welcome to Python!
Output in Python3
Hello from a Python file!
Welcome to Python!
Output in Python2
Hello from a Python file! Welcome to Python!
This is because the comma notation has been removed as print has been made a function in 3 whereas it was a keyword in 2.
You can achieve what you want by using
print ("Hello from a Python file!",end = ' ')
print ("Welcome to Python!")
This making use of keyword arguments of the print function in python3
I am new to Python, and despite my searching, am unable how to properly access a file from a Python Script on Windows with Python 3. I am trying to use Mongosm to import openstreetmap OSM data into mongodb, but get an error when trying to access the file. How can I fix this? Thank you. According to the github instructions, all I need to do is python insert_osm_data.py <OSM filename> (instructions found here)
The error says:
C:\Users\Jusitn>python C:\Users\Jusitn\Desktop\mongosm-master\insert_osm_data
G:\OSM\planet-140430.osm
File "C:\Users\Jusitn\Desktop\mongosm-master\insert_osm_data.py", line 160
print 'node not found: '+ str(node)
SyntaxError: invalid syntax
insert_osm_data.py is intended to be used with Python 2, but you are apparently running it under Python 3. The easiest fix is to install and use a Python 2 interpreter (compared to rewriting the script for Python 3 compatibility).
I myself is a beginner, but the much I am from the error, it looks like there is a syntax error in you file, as you are using python 3 you should use this : print('node not found: '+ str(node)) in line 160 in you file insert_osm_data.py . In python 3 if you print statement is turned into a print() function.
I trie to export a 3D-model to use is in my three.js script. Unfortunately I am not familiar with python and all 3D stuff so much.
When I run the converter-script (convert_obj_three.py) from the windows shell I get an error. (The script and the model are in the same directory.)
$ c:/Python33/python convert_obj_three.py -i couch.obj -o couch.js
File "convert_obj_three.py", line 781
print "WARNING: skipping morph [%s] with different number of vertices [%d] than the original model [%d]" % (name, n_morph_vertices, n_vertices)
^
SyntaxError: invalid syntax
Is this because there is a problem with the model or do I make a mistake?
The Problem is that you use Python 3 instead of Python 2 to run the script.
Python 2:
print ''
Python 3: print is a function and no keyword so it needs brackets:
print('')
Here is the code from the exercise:
from sys import argv
script, user_name = argv
prompt = '> '
print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_input(prompt)
print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)
Now I am running Windows 7 and I am running the CMD line with the code
python ex14.py myname
I get this error:
File "ex14.py", line 3
Python ex14.py, user_name
SyntaxError: invalid syntax
There is nothing wrong with the script among visible characters.
check there is no Unicode whitespace in the source e.g., NO-BREAK SPACE character. Create a new script in the same directory:
with open('ex14.py', 'rb') as file:
s = file.read()
print(repr(s)[:60])
u = s.decode('ascii') # this line should raise an error
# if there are bytes outside ascii
check Python version to make sure it is 2.7 (to interpret correctly error messages):
$ python -V
check that the file is not saved using utf-16/32 encodings (#abarnert's suggestion in the comments).
You should see many zero bytes '\x00' in the repr() results in this case.
install python2 and in terminal type
python2 ex14.py myname
will solve the problem
you are running the script in the latest python version
and the syntax of your code is for python version 2
that's why you are getting the syntax error