Signature method in Inspect module for python 2 - python

I am trying to run a Python 3 library in Python 2. It uses inspect module and signature method which is not implemented in Python 2 version of the module.
signature = inspect.signature(initializer)
There is no implementation in __future__ that can help (at least, I haven't found one).
How can I replace this method?

Package funcsigs on PyPI is a backport of PEP-362 (which adds signature introspection) to Python 2.6+. So change the line in question to
import funcsigs
signature = funcsigs.signature(initializer)

inspect2 is backport of the entire Python 3.6 inspect module to Python 2.7. Like funcsigs it is also available from PyPI, and inspect2 is more recently maintained. (As I'm writing this, inspect2 was last updated in 2019, while funcsigs was last updated in 2016.)

Related

pyFirmata gives error: module 'inspect' has no attribute 'getargspec'

I'm trying to use pyFirmata, but I can't get it to work. Even the most basic of the library does not work. I guess there is something wrong with the library code.
from pyfirmata import Arduino,util
import time
port = 'COM5'
board = Arduino(port)
I get this error:
Traceback (most recent call last):
File "c:\Users\Public\pythonpublic\arduino.py", line 5, in <module>
board = Arduino(port)
^^^^^^^^^^^^^
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\__init__.py", line 19, in __init__
super(Arduino, self).__init__(*args, **kwargs)
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 101, in __init__
self.setup_layout(layout)
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 157, in setup_layout
self._set_default_handlers()
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 161, in _set_default_handlers
self.add_cmd_handler(ANALOG_MESSAGE, self._handle_analog_message)
File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 185, in add_cmd_handler
len_args = len(inspect.getargspec(func)[0])
^^^^^^^^^^^^^^^^^^
AttributeError: module 'inspect' has no attribute 'getargspec'. Did you mean: 'getargs'?
As already pointed out in another answer, the pyFirmata modules is currently documented to run on Python 2.7, 3.6 and 3.7. This doesn't mean it won't work on other versions, but probably that it hasn't been tested on other versions by the author and it isn't officially supported. So it may or may not work on newer Python versions.
Your error message is caused by a missing function inspect.getargspec(). This function is part of the Python Standard Library, but has been deprecated since Python 3.0 (which came out in 2008). Unfortunately the author wasn't aware of this or simply didn't bother to fix it, so now the code doesn't work anymore with the latest version of Python.
In the Python documentation, you can see that the function is still available in version 3.10, but not in version 3.11.
To solve this you have a number of options:
Downgrade to Python 3.10, which is currently still a good option (Python 3.10 is "alive" until 2026-10-04). I don't know if all other functionality does work. I guess it will, but you would have to find out yourself.
Downgrade to Python 3.7, which is claimed to be supported. Given that Python 3.7 is also still alive (until 2023-06-27), that's a reasonable option as well.
Create an issue for the pyFirmata module and hope the author will fix the problem. Note that an issue has already been created by someone in 2019 but apparently without effect. You could leave a comment there confirming that this has now broken for real.
Clone the library and fix it yourself (and create a Pull Request to get it in the official library).
Find another, similar, library that does work with Python 3.11.
Write the code yourself.
Downgrading to a Python version between 3.7 and 3.10 is certainly the simplest option, and leaving some feedback to the author will give you a chance it will be fixed in the future, in case your planning to use your script for a longer time.
According to the first line of pyFirmata docs:
It runs on Python 2.7, 3.6 and 3.7
You are using Python 3.11. The inspect (core library module) has changed since Python 3.7.

How to get rid of 3rd collections.abc DeprecationWarning

My application is flooded with warnings from a 3rd package
transformers/modeling_deberta.py:18: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
from collections import Sequence
How can I suppress those warnings?
I've tried:
export PYTHONWARNINGS="ignore::DeprecationWarning"
warnings.filterwarnings(action="ignore")
warnings.filterwarnings(action="ignore", category=DeprecationWarning)
warnings.filterwarnings(action="ignore", category=DeprecationWarning, module=r".*transformers.*")
warnings.filterwarnings(action="ignore", category=DeprecationWarning, module=r".*collections.*")
warnings.filterwarnings(action="ignore", message=r".*collections.abc.*")
Update
The following options are not feasible:
Remove the 3rd package that generates those warnings. It is irreplaceable.
Downgrade to python 3.3
Maybe I should just wait for the 3rd package to upgrade. Just wonder if there is any other option to suppress specific 3rd party warnings in python.
The warning tells you that you are getting some resources from a location which was correct prior to Python 3.3 and will not work at all starting from Python 3.9. You are using a Python version between 3.3 and 3.9, which means that this will still work for you for the time being, but you will need to refactor your code so you import ABCs from collections.abc instead of from collections. Unless you refactor your code in the manner the error suggests, you will be stuck with Python versions prior to 3.9, which limits your possibilities, will disallow the usage of any goodies implemented after those versions and will increasingly see libraries incompatible with your project due to they being too modern for your project.
You can get rid of the warnings by downgrading your project to a Python version prior to 3.3, but that's a direction you should strive to avoid if possible. The best solution is to refactor your project to comply to the terms of modern Python versions and if you use packages that prevent you from doing so, then you might want to upgrade those packages. If there is no upgrade that would solve this issue, then it is well worth asking your question whether it is a higher cost to implement their functionality in a more modern way in terms of labor on your part or is it a higher cost in terms of technological shortage if you get stuck with old Python versions.
I found my answer from here
Solution: make sure the following code runs before the 3rd package import.
If multiprocessing is used, the code has to be called in each process.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from collections import Sequence

Python-telegram-bot syntax error for the line "_id_attrs: Tuple[Any, ...] = ()"

I have installed the python-telegram-bot package through pip and pip3 on ubuntu and tried to load it with:
from telegram.ext import Updater, CommandHandler
I then get:
File "/usr/local/lib/python3.5/dist-packages/telegram/base.py", line 42
_id_attrs: Tuple[Any, ...] = ()
^
SyntaxError: invalid syntax
I get a similar error for the python 2.7 version. The "base.py" file has a line with "_id_attrs:", I've been using python for a while and never have seen a variable name followed by a colon. What is meant to make this line readable to python.
You will have to update your python version to at least 3.6. The syntax (from the error) is introduced in python 3.6 (check PEP-526).
Also, It's clearly stated in the library's Documentation that you must use v3.6+.
This library provides a pure Python interface for the Telegram Bot
API. It's compatible with Python versions 3.6+. PTB might also work on
PyPy, though there have been a lot of issues before. Hence, PyPy is
not officially supported.

Debian packaging of Python code

I am trying to create a debian package of my Python code, but I am very confused about some fields in debian/control files. Namely, what
X-Python-Version, XS-Python-Version and XB-Python-Version stands for?
Thanks in advance.
It is described here: https://www.debian.org/doc/packaging-manuals/python-policy/ch-module_packages.html
The optional X-Python-Version (preferred) or XS-Python-Version field in the general paragraph (the first one, for the source package) of debian/control specifies the versions of Python (not versions of Python 3) supported by the source package. Similarly, X-Python3-Version is used to specify the versions of Python 3 supported by the package.
The use of XB-Python-Version in the binary package paragraphs of debian/control file has been deprecated and should be removed
So you should only use X-Python-Version and perhaps X-Python3-Version.
To specify what version of Python is required to build your package, you want Build-Depends as described here: https://www.debian.org/doc/packaging-manuals/python-policy/ap-build_dependencies.html
For example:
Build-Depends: python (>= 2.6.6-9)

Install libxml2 bindings for python3

The Problem
I’m trying to build libxml (libxml2-2.7.8) bindings for Python 3.2. When I run the following:
./configure --with-python=/usr/bin/python3.2
The compilation of libxml2-2.7.8 works...but the Python bindings for libxml2-2.7.8 don’t!
Attempted Fixes
Ported setup.py and generator.py (using 2to3, and some basic patches on generator.py)
Patched libxml.c and types.c as follows:
replace PyInt* by PyLong*
replace PyString* by PyBytes*
Current Situation
Unfortunately, it still wasn’t enough. I ran python3.2 setup.py build and got the following error:
types.c:594:17: error: ‘PyInstanceObject’ undeclared (first use in this function)
I can’t find the Python3 equivalent of PyInstanceObject!
Has anyone managed to compile libxml2 bindings for Python 3?
Did I miss something??? Can anybody help? :(
The PyInstanceObject is a part of the support for old style types, which is gone in Python 3, and has been deprecated since Python 2.2. The trick here is to first update the bindings to use new-style classes, and then port to Python 3.
(Or use lxml, which wraps the libxml2 in a Pythonic XML classes).
I'm not 100% sure what the replacement is, I've never done old-style classes in C, but I think it's simply a PyObject.

Categories