I am using a bot with python to create accounts on a website. When I use it on my windows machine with python installed, it works fine. I bought a linux VPS yesterday (Ubuntu 16.04) and my script apparently isn't working anymore. This is the error I get on my linux machine:
Traceback (most recent call last):
File "roblox.py", line 2, in <module>
from utils import *
File "/home/py/newbot/utils.py", line 18
key, *values = line.split(" ")
^
SyntaxError: invalid syntax
The line in the script its referring to is this
def string_to_dict(headers):
headers_dict = {}
for line in headers.split("\n"):
if not line: continue
line = line.strip()
key, *values = line.split(" ")
key = key[:-1]
if not (key and values): continue
headers_dict[key] = " ".join(values)
return headers_dict
Any ideas what could have gone wrong?
This syntax is invalid in python 2. Run your script with python3.
As mentioned in other answers, you need to run this in Python3. The shortest answer is:
Install python3, which may not already be on your machine: $ sudo apt-get update; sudo apt-get install python3
Get in the habit of using a shebang in your script files. Add #!/usr/bin/env python3 to the first line of your script to direct the shell to invoke python3 instead of python2 -- most systems come with /usr/bin/python aliased to python2 installations.
You can find out more about sourcing files in the Bash shell (default shell for Ubuntu 16.04) here.
The longer answer -- if you're wondering why the code doesn't work in Python2 but does work in Python3 -- is that Python3 introduced extended iterable unpacking in PEP 3132, but this feature was not implemented in Python2.
Related
I am new to transcrypt. I created a test python file,test.py
def test_def(a: list):
for i in range(len(a)):
print(i)
xx = [2, 3, 4]
test_def(xx)
I have python 3.9. If I run the python file, it runs and prints as expected.
Running transcrypt gives following error
> python -m transcrypt -b -m -n .\test.py
Error while compiling (offending file last):
File 'test', line 2, namely:
Error while compiling (offending file last):
File 'test', line 2, namely:
Aborted
I am not sure what it expects and why is it giving the error, any help would be appreciated.
What version of Transcrypt are you using? Wasn't able to replicate the error using Python 3.9.0 and Transcrypt 3.9.0. You can check like this (Win):
> transcrypt -h | find "Version"
# and may as well double check that the right version of python was used:
> python --version
The Python and Transcrypt versions should match, as Transcrypt uses Python's AST that may change between versions.
Another aspect is that I first installed Transcrypt into a virtual env as follows (Win):
> py -3.9 -m venv wenv
> wenv\Source\activate.bat
> pip install transcrypt
> python -m transcrypt -b -m -n test.py
It sometimes happens that one inadvertently uses the wrong version of Python. 'py' is the Python launcher on Windows and useful to launch the right version if you have multiple versions installed. On Linux there are typically binaries in /usr/bin such as 'python3.9' or a symlink such as 'python3' that links to the most recent 3 version. Installing into a virtual env as demonstrated is also helpful, as various problems can come about otherwise.
The above compiled test.py without error (Win 10 cmd.exe). Fwiw, the file was saved as utf-8 and compile worked with and without a BOM.
i have a python3.6.0 script which works perfectly well on windows. but when i run it in bash i get syntax error. here is the code:
import itertools
lines=["Hamlet","William Shakespeare","Edited Barbara B Mowat Paul Werstine","Michael Poston Rebecca Niles","Folger Shakespeare Library","httpwwwfolgerdigitaltextsorgchapter5playHam","Created Jul 31 2015 FDT version 092","Characters Play","line 17 POLONIUS father Ophelia Laertes councillor King Claudiusthis line substituted GHOST"]
LinesMap=dict()
for line in lines:
list=[l for l in line.split(' ')]
d = dict(itertools.zip_longest(*[iter(list)] * 2, fillvalue=None))
LinesMap = {**LinesMap, **d}
print(LinesMap)
this is the error:
[reza#localhost ~]$ python New\ Text\ Document.py
File "New Text Document.py", line 16
LinesMap = {**LinesMap, **d}
^
SyntaxError: invalid syntax
On many Linux distros, python refers to Python 2, and you need to use python3 to run a Python 3 script.
Depending on your distro, you should be able to yum or apt-get a package with Python 3, which can coexist nicely alongside Python 2, partly thanks to the decision to use different names for the executables.
In more detail, apt-get install -y python3 (probably with sudo or ask your admin) should install Python 3 on Debian-based platforms (Mint, Ubuntu, what have you); and yum install python36 should install Python 3.6 on various RPM-based platforms (Fedora, CentOS, etc, again probably with sudo or something equivalent).
Try
python3 New\ Text\ Document.py
Bash is using python 2 to run this.
Well I have this code:
#!/usr/bin/env python3
for i in range(15):
print(i, end=' ')
when I run it like this in command prompt:
prog1.py
I get:
C:\Users\Ja\Desktop>prog1.py
File "C:\Users\Ja\Desktop\prog1.py", line 4
print(i, end=' ')
^
SyntaxError: invalid syntax
but when I run it like this:
py prog1.py
it works fine. It's running python2.7.15 instead of python3.6.5 (I tested it using sys.version).
( It's the same for icon clicks )
You may be under the impression that Windows understands the "shebang line":
#!/usr/bin/env python3
It does not. All Windows knows is that .py files can be run by Python. So you need to update your file association, or run the correct version of Python explicitly, passing the path to your script as an argument.
This is my python script:
!/usr/bin/python
import sys
import magic
m=magic.from_file('<file with absolute path goes here>')
print(m)
On running this from the command line:
$ python script.py
Microsoft Word 2007+
results in the output of the TYPE of the document that file is. Using python-magic.
Now running the same script from scala using below code:
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
throws the below error:
Traceback (most recent call last):
File "script.py", line 3, in <module>
import magic
ImportError: No module named magic
PS: I have both python 2.7 and python 3.6 installed on my machine and running the script using any of them from the command line runs just fine so I guess both of them are bundled with the MAGIC packages correctly.
Would highly appreciate any kind of help.
I will try to answer my own question in the best possible way.
While trying to execute the script.py from the command line using python compiler python2.7 was being used and when I tried to invoke the same script.py from the scala compiler using
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
it was using python3 which was getting unnoticed. It took me a couple of days to actually figure this thing out and finally I removed all the python2.7 and python3 libraries and installed python 2.7 again and it worked like a charm.
It works from both python3 command and Scala code for me.
This should work.
import sys.process._
def compWithLibmagic(){
val result = "python3 script.py".!!
}
I have installed PocketSphinx (python-pocketsphinx, pocketsphinx-hmm-wsj1, pocketsphinx-lm-wsj), but get this error from trying to run a piece of Python3 code to recognise speech in an audio file.
$ python3 web_speech_api.py 02-29-2016_00-12_msg1.wav
Fatal Python error: Py_Initialize: Unable to get the locale encoding
File "/usr/lib/python2.7/encodings/__init__.py", line 123
raise CodecRegistryError,\
^
SyntaxError: invalid syntax
Current thread 0x00007fe1548de700 (most recent call first):
Aborted (core dumped)
I have both Python 2.7, Python 3.5 and Anaconda installed to make things complicated and I guess the error might be due to this somehow?
I have added the lines below to my ~/.bachrc.
export PYTHONPATH=/usr/lib/python2.7
export PATH=$PATH:$PYTHONPATH
Wasn't sure whether to put python3.5 or 2.7, but 3.5 gave me an error [...] ImportError: No module named '_sysconfigdata_m'.
I have also removed the line that was automatically added for setting the path to anaconda, and don't need the Anaconda packages for this project.
$ which python
/usr/bin/python
$ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/python2.7
Here is the code as well if it is of any help:
#!/usr/bin/ python
import sys
import pocketsphinx
if __name__ == "__main__":
hmdir = "/usr/share/pocketsphinx/model/hmm/wsj1"
lmdir = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.3e-7.vp.tg.lm.DMP"
dictd = "/usr/share/pocketsphinx/model/lm/wsj/wlist5o.dic"
wavfile = sys.argv[1]
speechRec = pocketsphinx.Decoder(hmm = hmdir, lm = lmdir, dict = dictd)
wavFile = file(wavfile,'rb')
speechRec.decode_raw(wavFile)
result = speechRec.get_hyp()
print(result)
I'm very thankful for help straightening out my error and hopefully also sorting out my mess of different Python versions...