regular expression working in mac but giving error in linux. Linux has all python package but version is different
Mac python version = 3.7.9
linux python version = 3.6.8
line 99
pattern = re.compile(rb"neighbor \d+\.\d+\.\d+\.\d+")
^
SyntaxError: invalid syntax
As mentioned in existing comments, it seems likely you are using the wrong version of python
$ python2 -c 'rb""'
File "<string>", line 1
rb""
^
SyntaxError: invalid syntax
$
$ python3 -c 'rb""'
$
What is the output of python -V, or if you are running this from some non-command line source
import sys; print(sys.version)
if you insert it on the line above your code?
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 sample.py python file, which contains this line:
python -c "import numpy"
Run Command:
$ python sample.py
When executed, the script got the error:
SyntaxError: invalid syntax
Can anyone help me what is the issue with this line?
The content is not python, it's a command.
The content of sample.py should be just
import numpy
python -c ... calls python from a shell, and executes -c's argument by python. If you want that in a script, use a shell script, not a python script.
Alternatively, if you're running it with python anyway, drop the python -c, and just keep the python code (import numpy).
Installed both Python 3.6 and 2.7 on my Win10 system, on trying to use the Python launcher for 2.7 I get the following error:
C:\WINDOWS\system32>py -2
File "C:\Python36-32\Lib\site.py", line 177
file=sys.stderr)
^
SyntaxError: invalid syntax
Relevant environment variables are:
PATH - Python3.6; Python3.6\Scripts\
PYTHONHOME - Python3.6
PYTHONPATH - Python3.6\Lib
What do I need to do to get the Python launcher to work with Python 2.7
you should use C:\WINDOWS\system32>"C:\python27\python.exe" yourscript.py .
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.
I have installed yolk 0.4.3 using pip. But when I tried yolk -l to display all installed packages, it showed a syntax error
File "C:\Python32\Lib\site-packages\yolk\cli.py" line 262
print "%s %s (%s)" % (project name,dis,version,
^
syntax error :invalid syntax
It seems you're using the package yolk (which only works Python 2). To install yolk for Python 3 use the yolk3k package:
pip install yolk3k
It looks like you are running a python 2 library with python 3 the versions need to match.
You have Python 2.x syntax with print when you are using Python 3.x (notice the "Python32" in "C:\Python32\Lib\site-packages\yolk\cli.py"). Below is an example written in Python 3.x:
>>> print "%s" % "a"
File "<stdin>", line 1
print "%s" % "a"
^
SyntaxError: invalid syntax
>>> print("%s" % "a")
a
>>>
As you can see, you need to use Python 3.x syntax (namely, treat print as a built-in, not a keyword).
To fix your problem, make sure your version of yolk works with your version of Python.