Obnoxious CryptographyDeprecationWarning because of missing hmac.compare_time function everywhere - python

Things were running along fine until one of my projects started printing this everywhere, at the top of every execution, at least once:
local/lib/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a 2.7.x release that supports hmac.compare_digest as soon as possible.
I have no idea why it started and it's disrupting the applications'/tools' output, especially when it's being captured and consumed by other tools. Like many difficulties throughout time, I'm fairly certain it is related to urllib and, by association, requests. Worse, I have so many projects and cross-dependencies that I can't possibly update all of the imports and branches with the call to warnings.filterwarnings() to suppress the warning.
I have Python 2.7.6 . Apparently this goes away in 2.7.7 . Only, I have some systems that have 2.7.6 where I do not see the warnings. So, something may or may not be disabling them in one version and I might've inadvertently replaced it with another version.
My Ubuntu, Python, urllib, requests (with the security option), cryptography, and hmac are all identical versions/builds on systems that do print the warning and systems that do not.
There appears to be no relevant warnings or announcements online and it seems like any related project is static/stable by this point (even though 'hmac' can be installed via PIP, it hasn't changed in eight years).

I hit this error for quite sometime. For my environment, it was a pain to upgrade Python to a higher version than 2.7.6. The easier solution was to downgrade cryptography module using pip:
pip2.7 install cryptography==2.2.2
I think the best solution is to upgrade your python version though

This answer is for Python3
I got here by looking for an answer while using Paramiko. For those still looking for a simple answer. I got these CryptographyDeprecationWarning suppresed with the these lines of code before importing Paramiko:
import warnings
warnings.filterwarnings(action='ignore',module='.*paramiko.*')
I hope this helps

If you want to be more selective about suppressing JUST that particular deprecation warning:
import warnings
from cryptography.utils import CryptographyDeprecationWarning
warnings.filterwarnings("ignore", category=CryptographyDeprecationWarning)

I started getting this warning for a straightforward requests.get call. This warning is printed when the module cryptography.hazmat.primitives.constant_time is loaded, and so this should typically only come once per Python program. If you are seeing it many times, it must be because a Python program (like a utility) is getting executed multiple times. You just have to identify that program and add the below code to the main entry point:
import cryptography
from cryptography import utils
with warnings.catch_warnings():
warnings.simplefilter('ignore', cryptography.utils.DeprecatedIn23)
import cryptography.hazmat.primitives.constant_time

I fixed this for all my local projects and tools using Python 2 by removing the warning from the python source code [your_installation_path]\Python\Lib\site-packages\cryptography\__init__.py
Just remove this snipped at the end of the file and delete the __init__.pyc file so its recompiled with the changes:
if sys.version_info[0] == 2:
warnings.warn(
"Python 2 is no longer supported by the Python core team. Support for "
"it is now deprecated in cryptography, and will be removed in the "
"next release.",
CryptographyDeprecationWarning,
stacklevel=2,
)

For Python3 only:
Per an apparent Paramiko update this worked for me and solve the similar problem/symptoms I was experiencing:
pip3 install --upgrade paramiko
This installed paramiko 2.6.0 on my system, replacing 2.4.2:
$ pip3 install --upgrade paramiko
[...]
Installing collected packages: paramiko
Found existing installation: paramiko 2.4.2
Uninstalling paramiko-2.4.2:
Successfully uninstalled paramiko-2.4.2
Successfully installed paramiko-2.6.0
$
My Python2 environment appears to be messed up, so I'm not able to test this on Python2.

When you do pip2.7 install cryptography==2.2.2, it appears the error might still occur.
I believe you need also sudo pip2.7 install --upgrade pip
Aso, as of 5/5/19 the newest appears to be cryptography=2.6.1

Related

Python PIP Install on Windows missing sub-directories?

My 11-year-old son is trying to follow Carol Vorderman's "Python Games for Kids" book, but is hitting a fundamental problem installing then using Actors module (p.52-onwards of book), on Windows 10. No instructions are provided for installing or importing this. We installed 'actors' (0.5.1b1) using pip:
pip install actors
The install "works" happily, no error is reported, and a (basic) actors installation appears. (We also tried python -m pip install actors, with exactly the same result).
However, any attempt to import actors; e.g.,
from actors import Actor
fails with:
ModuleNotFoundError: no module name 'actors.internal'
of line 29 of actors\__init__.py . Which is fair enough, because it does try to import messages from actors.internal, which does not seem to exist.
Things I checked following the import:
the downloaded .tar.gz file includes a load of subdirectories, including internal; but this doesn't make it into the disc. So it looks like pip isn't handling the .tar.gz file correctly? But it seems hard to believe that such a basic failure has gone undetected.
the disc has 116G free, so it's not running outta space.
I checked other answers (python pip install not working on windows, pip install on a shared directory (windows)), but they do not seem to apply here.
This is a bug in the distribution. It lists packages=['actors'], but it must list all subpackages (internal and utils) too.
The bug was reported in 2016 and still is unresolved. So we can guess the package is abandoned and there is not much you can do to fix it (other than forking and fixing it yourself).
You need to run your program through Pygame Zero, since it adds a few things for you (like opening a window, handling OS events, defining what an Actor or a Sprite are, etc).
Install it with: pip install pgzero
And then instead of running your file normally via python my_file.py
You should run it with: pgzrun my_file.py
If that doesn't work you can try putting import pgzrun on first line of the program and pgzrun.go() on the last line and then running the file normally with: python my_file.py.
Source for the fixes

python2-gobject on Arch linux ImportError: No module named gobject

I have some example file (download URL) to understand how to create Twisted chat with GUI.
In this particular file I have an exception ImportError: No module named gobject.
It's true, I have only gi and already installed:
sudo pacman -S python2-gobject
So I decide that this code for python3, and again fail. After pip install twisted I can't run code: ImportError: cannot import name 'gtk2reactor' appears.
How to run this code at least.
And how to prevent this in future, because I have the same error in many science packages for python.
P.S. installing from source impossible either.
make returns a lot of errors even if ./configure completes fine.
You may want to clarify your question a bit, but if you can't run code after you pip, or if your pip is broken a uninstall/reinstall of pip may be your best bet.
If you did successfully download the package, then I would dive into where you installed it and make sure the package is for the correct version of python, and that it is installed.
Juhaz from Freenode told me that the code in examples was pretty old and uses unmaintained bindings.
In case someone starts the same was as I am this question would be helpfull.
Try to look at wkPython, for example this post.

Python 2.7.5 on Cygwin64: requests installation fails

Our project is a mostly J2EE based development with the automatic functional and integration tests written in Python. The test environment is Linux nonetheless developers use Windows 7 (64-bit). We would like to be able to execute the functional tests on the developer machines as well (before comitting). Unfortunately the pexpect-windows-portability issue would leave us no choice but:
To do some serious refactoring on our test libraries to be able to use both winpexpect (or wexpect) and pexpect depending on the os settings.
Or to use cygwin. Guess what, with this second option we seem to have an issue :-) Using Python 2.7.5 on Cygwin64 installing the requests package results in error:
pip says it can not find a file after downloading and extracting the library
easy install doesn't throw an explicit error, but leaves everything in the temporary dir
after copying the files under the site-packages directory a simple import requests in python causes the interpreter to exit
Has anybody encountered this problem? With Cygwin-32 requests install smoothly. (however we have some other issues - see my next post ;-))
Thank you in advance: Joe, the public
Also ran into the same issues when trying to install requests, all the options on http://docs.python-requests.org/en/latest/user/install/#install did not work. I went to https://github.com/kennethreitz/requests and then clicked on "Download Zip" and I got requests-master.zip.
Update: This should be fixed in Cygwin.
This was a bug in CPython that has been fixed in their master branch. I've pushed a candidate package to the Python maintainer for Cygwin, but you might try this hotfix.
I downloaded from "https://github.com/requests/requests" and then i just ran the setup.py from the requests-master folder ( this was placed in cygwin folder). After that I went to cygwin terminal and then I ran python --> import requests. Voila it worked.

Force python to use an older version of module (than what I have installed now)

My employer has a dedicated module1 we use for internal unit / system test; however, the author of this module no longer works here and I have been asked to test some devices with it.
The problem is that pyfoo requires an ancient version of twisted (v8.2.0) and it imports twisted in 33 different files. I tried running pyfoo's unit tests under v11.0.0 and I don't even see TCP SYN packets2. Unfortunately, I have already got twisted v11.0.0 installed on my lab linux server and I have my own code that depends on it.
To solve this problem, I have only come up with the following options:
Option A. Install a new version of python, install virtualenv, and then install an old version of twisted under the virtualenv. Only run the tests requiring pyfoo under this new version of python.
Option B. Edit all 33 of the files with the following: DIR = '../'; sys.path.insert(0, DIR) and install the old version of python in the appropriate directory below the source.
Option C. Attempt to fix pyfoo to use v11.0.03
Are there any options I am missing? Is there a more elegant way to solve this problem, besides Option A, above?
**END-NOTES:**
Let's call it pyfoo for sake of argument
The unit tests connect to one of our local lab servers and exercises basic telnet functionality
This option is almost a non-starter... pyfoo is not trivial, and I have a short deadline for this work.
A better version of option B. would be to replace
import twisted
by
import pkg_resources
pkg_resources.require("Twisted==8.2.0")
import twisted
which will arrange for the correct version of twisted to be imported, so long as it's installed, and raises an exception otherwise. This is a more portable solution.
This won't work, though (nor would any other variaton of option B), if twisted gets imported before the pkg_resources.require gets called; twisted will already be in sys.modules
OP Edit: Minor syntax correction, per pkg_resources docs
If SingleNegationElimination's solution doesn't work, be aware that you don't need to replace all 33 instances of the import; you only need to modify sys.path at the entry points; e.g. you could target just your module's __init__.py files.
There you would insert e.g.
import sys
sys.path.insert(0, DIR)
I can't tell you what is best in your situation, but you might be able to consider:
Option D: run it in a virtual machine (eg. with Windows 7)
Option E: install old version of python/twisted on another machine
It took me a bit of trial and error to fix my situation; which involved the accepted answer and it's additional comments (mentioning adding _requires_)
__requires__= 'twisted==8.2.0'
import pkg_resources
pkg_resources.require("twisted==8.2.0")
import twisted
You should do uninstall and install before import.
First,
!pip uninstall igraph -y
!pip uninstall python-igraph -y
!pip install python-igraph==0.9.6
!pip install cairocffi
Then,
import igraph
print(igraph.__version__)
% 0.9.6

Has anybody been able to install PyWeka?

I need to install in python 2.6 or 2.7 for windows the library PyWeka0.3dev, It says it requires setuptools, which I installed but then they told me it was a deprecated instalation library and I installed distribute, then I downloaded the PyWeka compressed package and each time I try to install it neither with setup.py nor with easy_install (where it says something like no module ez_setup). Can anybody give me a clue about how to do this?
As mentioned to you via Aardvark (yes, I am omnipresent), the module in question is broken. You can't easy_install it. It's a bug in PyWeka.
You can download the file from PyPI, http://pypi.python.org/pypi/PyWeka/0.3dev, and unpack it.
In the file setup.py, remove the following two lines:
from ez_setup import use_setuptools
use_setuptools()
And install it by running
python setup.py install
You need to have installed numpy and NamedMatrix (which has the same bug as PyWeka) first.
However, you mentioned you are on Windows. I strongly doubt that PyWeka will work on Windows. There are some Unix specific code in it.
And I still really want to know why the authors are reading files by calling cat from subprocess. That seems pretty pointless and is together with the broken install, good enough reason for me to keep far away from that module. I suspect it's authors simply have no idea what they are doing.
That, or they are geniuses.
A punk/goth approach to programming probably has the right to be..
To get the C-compliation part to work on windows you either need (1) to have Visual Studio of the same version that was used to compile the python version you are using, or (2) mingw which is a bit trickier to set up.

Categories

Resources