This is all about and issue when using the latest Python Protobuf (3.19.1) and Python 3.10, in Linux (tested in Fedora 35 and Ubuntu 20.04.
It broke our library but it can easily tested using the addressbook.proto from the Python Protobuf tutorial and tried to get the proto2 message class as follows:
import addressbook_pb2
from google.protobuf import (
descriptor_database,
descriptor_pb2,
descriptor_pool,
message_factory,
)
_DESCRIPTOR_DB = descriptor_database.DescriptorDatabase()
_DESCRIPTOR_POOL = descriptor_pool.DescriptorPool(_DESCRIPTOR_DB)
_DESCRIPTOR_DB.Add(
descriptor_pb2.FileDescriptorProto.FromString(
addressbook_pb2.DESCRIPTOR.serialized_pb
)
)
factory = message_factory.MessageFactory()
cls = factory.GetPrototype(_DESCRIPTOR_POOL.FindMessageTypeByName("tutorial.Person"))
It raises the following error:
[libprotobuf ERROR google/protobuf/pyext/descriptor_database.cc:64] DescriptorDatabase method raised an error
SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats
Traceback (most recent call last):
File "/dev/protobuf/test/test.py", line 21, in <module>
ls = factory.GetPrototype(_DESCRIPTOR_POOL.FindMessageTypeByName("tutorial.Person"))
`KeyError: "Couldn't find message tutorial.Person"
Now, it works as expected if I use an older Python Protobuf version, such as 3.18.1.
I've opened a bug https://github.com/protocolbuffers/protobuf/issues/9245, but apparently, it was not considered a bug.
Python Protobuf introduced the PY_SSIZE_T_CLEAN macro in 3.19.1 and broke something, probably by using int instead of Py_ssize_t when using # formats.
Have anyone have this issue or can confirm it?
Yes I am also getting same issue.
We changes the version as below:
*protobuf >= 3.19* # Not working
*protobuf >= 3.15.6, <= 3.20.1* # Working
There are actually two errors here:
SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats
This error is caused by Python 3.10 dropping support for old default conversions when passing data from C to Python side. In this case in the protobuf library, the error only occurs when passing an exception from C code to Python.
The python-protobuf library was fixed to work with Python 3.10 back in October 2021, and the fix should be included in python-protobuf 3.20.0 and later.
Try adding this to your script to check the version:
import google.protobuf
print(google.protobuf.__version__)
For me the error does not occur with the latest versions 3.19.4, 3.20.1 or 4.21.1, but does occur with 3.19.2 and older.
Related
Currently I'm working on implementing code in "Differentially Private Federated Learning: A Client Level Perspective" where the GitHub link is LINK.
However, I follow the instruction but got an error which is
Traceback (most recent call last):
File "sample.py", line 5, in <module>
from MNIST_reader import Data
File "/content/drive/MyDrive/Colab_Notebooks/machine-learning-diff-private-federated-learning-main/MNIST_reader.py", line 20
raise ValueError, "dataset must be 'testing' or 'training'"
SyntaxError: invalid syntax
I just run bash RUNME.sh and follow the instruction but still get an error!
!python sample.py —-m 100, sigma 1
You're welcome if you want to check the full code here.
Thanks a lot!!
Error SyntaxError: invalid syntax for line
raise ValueError, "dataset must be 'testing' or 'training'"
may suggest that code was created for Python 2 but you run Python 3.
You may have edit file MNIST_read.py and use () instead of , in line
raise ValueError("dataset must be 'testing' or 'training'")
BTW:
Some files on GitHub are 5 years old so it could be created for Python 2.
So you may excpect that code may have other problems with Python 3
Requirements shows Tensorflow 1.4.1 and it may have problem to run with your Tensorflow 2.8.4 because they change some elements.
Maybe it will be simpler to run it with Python 2
EDIT:
In GitHub in Insight / Network you can see all forks of this repo and fork created by rosdyana has last commit with title Fix code for python3.5 and maybe you should use this version.
But this version still may need to use Tensorflow 1.4.1 which may need older Python 3.5 or 3.6. I don't know if it works with the newest versions of Python.
I've tweaked a copy of one of the Nsight Systems report scripts (gpukernsum), and I now want to run it myself. So, I write:
./gpukernsum.py report.sqlite
This doesn't work; I get:
ERROR: Script 'gpukernsum.py' encountered an internal error.
$ ./gpukernsum.py report.sqlite
File "./gpukernsum.py", line 40
"""
^
SyntaxError: invalid syntax
I know this is because f"""whatever""" is Python-3 syntax, so I change the script's hash-bang line from:
#!/usr/bin/env python
to:
#!/usr/bin/env python3
and now I get:
$ ./gpukernsum.py report.sqlite
Traceback (most recent call last):
File "/path/to/./gpukernsum.py", line 7, in <module>
import nsysstats
ModuleNotFoundError: No module named 'nsysstats'
So I added the relevant directory to the lookup path:
export PYTHONPATH="$PYTHONPATH:/opt/nvidia/nsight-systems/2022.1.1/host-linux-x64/python/lib"
and now I get:
$ ./gpukernsum.py report.sqlite
near "WITH": syntax error
... and I'm stuck. The relevant area of the code is:
and not a percentage of the application wall or CPU execution time.
"""
query_stub = """
WITH
summary AS (
SELECT
coalesce({NAME_COL_NAME}, demangledName) AS nameId,
i.e. the "WITH" is part of a string literal which is an SQL query. So, what's the problem? Is Python complaining? Is sqlite complaining?
Note:
Nsight Systems 2022.1.1
CentOS 7
I'm using the original gpukernsum.py code - I have not made any changes to it (other than as described above).
My system has Python 3.9.1 for python3.
A workaround answer:
Nsight Systems bundles its own version of Python, with lib and bin directories.
If you run your script with this specific version, having set PYTHONPATH as described in your question - then the script will work. It's what Nsight itself does, after all.
As an example, executing this code with Python versions older than 3.9 will raise an exception:
from concurrent.futures import Future
f: Future[int]
TypeError: 'type' object is not subscriptable
But mypy will not complain (I'm using mypy 0.910):
Success: no issues found in 1 source file
Explicitly specifiying the Python version that mypy will use (e.g. --python-version=3.8) does not change this.
Numerous times I have fallen into the trap of writing code that uses features from newer Python versions, assuming that mypy will tell me if I made any errors, to then discover these errors only later at runtime.
How can I tell mypy to not assume features from certain Python versions (which I don't even have installed) to exist?
I'll take a guess: by running it with older mypy versions.
You could set up CI to run with every mypy version you require, and locally use virtual envs for each version
Mypy seems to already work as expected with built-in types, but fails in other cases, like Future above, or queue.Queue.
After reading Mypy docs – Annotation issues at runtime and Issue #7907 – Implement PEP 585 I'm not sure if this is a bug or intentional. Maybe the assumption is that the runtime errors can be avoided by using from __future__ import annotations.
After reading this answer it might be that this is a flaw in the Python standard library, or rather its typeshed, which promises more than is actually implemented, and mypy can't do anything about it.
Works:
# example1.py
x: list[int]
$ mypy --python-version=3.8 example1.py
example1.py:1: error: "list" is not subscriptable, use "typing.List" instead
$ mypy --python-version=3.9 example1.py
Success: no issues found in 1 source file
Works:
# example2.py
d: dict[str, int]
$ mypy --python-version=3.8 example2.py
example2.py:1: error: "dict" is not subscriptable, use "typing.Dict" instead
$ mypy --python-version=3.9 example2.py
Success: no issues found in 1 source file
Fails:
# example3.py
from queue import Queue
q: Queue[int]
$ mypy --python-version=3.8 example3.py
Success: no issues found in 1 source file
$ python3.8 example3.py
Traceback (most recent call last):
File "example3.py", line 2, in <module>
q: Queue[int]
TypeError: 'type' object is not subscriptable
Workaround:
# example4.py
from __future__ import annotations
from queue import Queue
q: Queue[int]
print('ok')
$ mypy --python-version=3.8 example4.py
Success: no issues found in 1 source file
$ python3.8 example4.py
ok
When I do pylint main.py, I get the following error:
E: 7, 0: invalid syntax (<string>, line 7) (syntax-error)
# main.py
import os
repo = os.environ.get('GITHUB_REPOSITORY')
branch = os.environ.get('GITHUB_REF')
commit = os.environ.get('GITHUB_SHA')
commit_url = f'https://github.com/{repo}/commit/{commit}'
repo_url = f'https://github.com/{repo}/tree/{branch}'
print(commit_url, repo_url)
The code is running as expected but pylint is giving this strange error. I am using Python 3.6.9 on Ubuntu 18.04.
It looks like PyLint isn't happy with your f-strings (introduced in 3.6) and is validating against the syntax of an older Python version. I'd check whether the PyLint you are using is running from the same Python environment your Python you are running the program with. I would guess it's running from your system Python, while your program is running from a virtual environment.
With pylint 2.5.3 and Python 3.8.2 the only complaint PyLint makes is about the lack of a module docstring.
************* Module main
main.py:1:0: C0114: Missing module docstring (missing-module-docstring)
-----------------------------------
Your code has been rated at 8.57/10
Use .format method like below
import os
repo = os.environ.get('GITHUB_REPOSITORY')
branch = os.environ.get('GITHUB_REF')
commit = os.environ.get('GITHUB_SHA')
commit_url = 'https://github.com/{}/commit/{}'.format(repo, commit)
repo_url = 'https://github.com/{}/tree/{}'.format(repo, branch)
print(commit_url, repo_url)
Check here, Python 3 returns "invalid syntax" when trying to perform string interpolation
I'm trying to use the dlib (v19.6) Python API to create a CNN face detector using the code:
cnn_face_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')
However, I get an ArgumentError as follows:
---------------------------------------------------------------------------
ArgumentError Traceback (most recent call last)
<ipython-input-16-c2ca0a6e8dff> in <module>()
----> 1 cnn_face_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')
ArgumentError: Python argument types in
cnn_face_detection_model_v1.__init__(cnn_face_detection_model_v1, str)
did not match C++ signature:
__init__(_object*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
What might I be doing wrong? Can I not pass the filename of the model file simply as a string?
This works for me, using this fresh release and your usage is correct!
This probably means, that you either:
did something wrong during install
installed by python setup.py install? That would be correct!
or: your python-interpreter is using some other dlib-version without your knowledge
I had a simmilar issue after python setup.py install due to python using an older version of dlib from /opt/conda/lib/python3.6/site-packages/dlib.so.
Doing a simple
mv /opt/conda/lib/python3.6/site-packages/dlib.so /opt/conda/lib/python3.6/site-packages/dlib_old.so
solved it for me.