Patch pyopenssl for sslv3 issue - python-2.7

I got a problem on a Debian 8 system with python 2.7.9-2 amd64:
marius#pydev:/usr/lib/python2.7/dist-packages/urllib3/contrib$ pip search doo
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 356, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2476, in load_entry_point
return ep.load()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2190, in load
['__name__'])
File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 74, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/lib/python2.7/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
from pip.download import path_to_url
File "/usr/lib/python2.7/dist-packages/pip/download.py", line 22, in <module>
import requests, six
File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 73, in <module>
ssl.PROTOCOL_SSLv3: OpenSSL.SSL.SSLv3_METHOD,
**AttributeError: 'module' object has no attribute 'PROTOCOL_SSLv3'**
I checked into the lib and tried to patch /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py
from .. import connection
from .. import util
__all__ = ['inject_into_urllib3', 'extract_from_urllib3']
# SNI only *really* works if we can read the subjectAltName of certificates.
HAS_SNI = SUBJ_ALT_NAME_SUPPORT
# Map from urllib3 to PyOpenSSL compatible parameter-values.
_openssl_versions = {
ssl.PROTOCOL_SSLv23: OpenSSL.SSL.SSLv23_METHOD,
**ssl.PROTOCOL_SSLv3: OpenSSL.SSL.SSLv3_METHOD,**
ssl.PROTOCOL_TLSv1: OpenSSL.SSL.TLSv1_METHOD,
}
_openssl_verify = {
ssl.CERT_NONE: OpenSSL.SSL.VERIFY_NONE,
ssl.CERT_OPTIONAL: OpenSSL.SSL.VERIFY_PEER,
ssl.CERT_REQUIRED: OpenSSL.SSL.VERIFY_PEER
+ OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
}
Could someone enlighten me how I can fix this? It would be super awesome if someone had a clue. I googled the issue and only found incomplete patches and it's messy. Probably a case for the bug tracker once this is fixed, too. I have this issue for all Python packages.

This is actually an issue with urllib3, not with pyopenssl. Debian lately compiles OpenSSL without SSLv3 support, and urllib3 just assumed that support was there.
The issue was fixed in commit b9b3b0102 which is part of the 1.10 release of urllib3.
As you are using urllib3 as part of requests, which in turn is used by pip, it should be enough to update to a recent version of requests. As of writing, the current version is 2.6.0 which contains the fix:
# pip install requests==2.6.0
You might encounter a problem upgrading requests, because of the chicken-egg problem. To fix this, you can try to temporarily remove the pyopenssl package, upgrade requests and reinstall pyopenssl.
Additionally you might want to use the following line to update pip before trying to update requests:
# sudo easy_install --upgrade pip

Another way to fix the problem is to use
sudo easy_install --upgrade pip
and use pip normally afterwards

I've received the same error.
In conjunction with the previous answers:
sudo easy_install --upgrade pip
I've also had to run:
sudo pip uninstall pyopenssl
sudo pip install mozdownload
This fixed my error, maybe it can help others. I came here by googling:
AttributeError: 'module' object has no attribute 'PROTOCOL_SSLv3'

I ran into this problem today with Ansible. I solved it with:
pip uninstall pyopenssl
maybe this will help someone else in the same situation

I received this error after an upgrade from Ubuntu 14 to 16. The upgrade changed some Python paths, which broke some core packages. This answer essentially fixed it for me.
sudo apt-get purge python-pkg-resources
sudo apt-get -f install
sudo rm -Rf /usr/local/bin/pip
sudo apt-get install python-pip
sudo pip install -U pip

I meet the same problems and I just uninstall the version of requests and install the exact version by using the following command.
pip install requests==2.6.0
then it works

I ran into this on a new Xenial and was unsuccessful w/many answers I had seen (some I really didn't try because most of my software was more current). What finally solved it for me was: sudo pip install requests --upgrade ... which is just a twist on the other request upgrade recommendation.

Related

/bin/sh: mysql_config: not found while making docker image in django app [duplicate]

This is the error I get
(mysite)zjm1126#zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python
Downloading/unpacking mysql-python
Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded
Running setup.py egg_info for package mysql-python
sh: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
Complete output from command python setup.py egg_info:
sh: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /home/zjm1126/.pip/pip.log
(mysite)zjm1126#zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python
Downloading/unpacking mysql-python
Running setup.py egg_info for package mysql-python
sh: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
Complete output from command python setup.py egg_info:
sh: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in /home/zjm1126/.pip/pip.log
What can I do to resolve this?
It seems mysql_config is missing on your system or the installer could not find it.
Be sure mysql_config is really installed.
For example on Debian/Ubuntu you must install the package:
sudo apt-get install libmysqlclient-dev
Maybe the mysql_config is not in your path, it will be the case when you compile by yourself
the mysql suite.
Update: For recent versions of debian/ubuntu (as of 2018) it is
sudo apt install default-libmysqlclient-dev
In Mac OS, I simply ran this in terminal to fix:
export PATH=$PATH:/usr/local/mysql/bin
This is the quickest fix I found - it adds it to the path, but I think you're better off adding it permanently (ie add it to /etc/paths) if you plan to install MySQL-python in another environment.
(tested in OSX Mountain Lion)
There maybe various answers for the above issue, below is a aggregated solution.
For Ubuntu:
$ sudo apt update
$ sudo apt install python-dev
$ sudo apt install python-MySQLdb
For CentOS:
$ yum install python-devel mysql-devel
apt-get install libmysqlclient-dev python-dev
Seemed to do the trick.
If you are on MAC
Install this globally
brew install mysql
then export path like this
export PATH=$PATH:/usr/local/mysql/bin
Than globally or in your venv whatever you like
pip install MySQL-Python
Note: globally for python3 as Mac can have both python2 & 3
pip3 install MySQL-Python
You can use the MySQL Connector/Python
Installation via PyPip
pip install mysql-connector-python
Further information can be found on the MySQL Connector/Python 1.0.5 beta announcement blog.
On Launchpad there's a good example of how to add-, edit- or remove data with the library.
For centos users:
yum install -y mysql-devel python-devel python-setuptools
then
pip install MySQL-python
If this solution doesn't work, and print gcc compile error like:
_mysql.c:29:20: error: Python.h: No such file or directory
You need to specify the path of Python.h, like this:
pip install --global-option=build_ext --global-option="-I/usr/include/python2.6" MySQL-python
I was trying to install mysql-python on an Amazon EC2 Linux instance and I had to install these :
yum install mysql mysql-devel mysql-common mysql-libs gcc
But then I got this error :
_mysql.c:29:20: fatal error: Python.h: No such file or directory
So I installed :
yum install python-devel
And that did the trick.
For anyone that is using MariaDB instead of MySQL, the solution is to install the libmariadbclient-dev package and create a symbolic link to the config file with the correct name.
For example this worked for me:
ln -s /usr/bin/mariadb_config /usr/bin/mysql_config
For Linux
this works for me
yum install python-devel mysql-devel
Try sudo apt-get build-dep python-mysqldb
OSX Mavericks
Due to changes within osx mavericks & xcode development tools you may get the error on installation
clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
therefore use :
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install mysql-python
for mariadb install libmariadbclient-dev instead of libmysqlclient-dev
sudo apt-get install libmariadbclient-dev
You should install the mysql first:
yum install python-devel mysql-community-devel -y
Then you can install mysqlclient:
pip install mysqlclient
I had the same problem in the Terraform:light container. It is based on Alpine.
There you have to install mariadb-dev with:
apk add mariadb-dev
But that one is not enough because also all the other dependencies are missed:
apk add python2 py2-pip gcc python2-dev musl-dev
sometimes the error depends on the actual cause. we had a case where mysql-python was installed through the python-mysqldb debian package.
a developer who didn't know this, accidentally ran pip uninstall mysql-python and then failed to recover with pip install mysql-python giving the above error.
pip uninstall mysql-python had destroyed the debian package contents, and of course pip install mysql-python failed because the debian package didn't need any dev files.
the correct solution in that case was apt-get install --reinstall python-mysqldb which restored mysql-python to its original state.
Sequence to be followed.
pip install mysqlclient
sudo apt-get install python3-dev libmysqlclient-dev
pip install configparser
sudo cp /usr/lib/python3.6/configparser.py /usr/lib/python3.6/ConfigParser.py
Then try to install the MYSQL-python again. That Worked for me
On Mac:
brew install mysql-client
locate mysql
mdfind mysql | grep bin
then add to path
export PATH=$PATH:/usr/local/Cellar/mysql-client/8.0.23/bin/
or permanently
echo "export PATH=$PATH:/usr/local/Cellar/mysql-client/8.0.23/bin/" >> ~/.zshrc
source ~/.zshrc
Had a similar issue trying to install on OS X Server 10.6.8. Here's what I had to do.
Using:
MySQL-python 1.2.4b4 (source)
MySQL-5.6.19 (binary installer)
Python 2.7 (binary installer)
NOTE: Installing in virtualenv...
Unzip source, open 'distribute_setup.py' and edit DEFAULT_VERSION to use the latest version of distribute tools, like so:
DEFAULT_VERSION = "0.6.49"
Save. Open 'site.cfg' file and uncomment the path to mysql_config so it looks something like (reference your own path to mysql_config):
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /usr/local/mysql/bin/mysql_config
Now clean, build and make will not fail with the 'mysql_config' not found error.
Hope this helps someone else trying to make use of their old xserves :-)
Your sudo path does not know about your local path... go into superuser mode, add the path, and install it from there.
sudo su
export PATH=$PATH:/usr/local/mysql/bin/
pip install mysql-python
exit
And you're up and running on OSX. Now you have an updated global python.
if you install MySQL-python in your virtual env, you should check the pip version, if the version is older than 9.0.1, please update it
pip install --upgrade pip
write on 27 Aug, 2021
my macos version is BigSur(14.4), and mysql version is mysql8. It seems like in the higher version of mysql, they delete the my_config.h files but change to the mysql.h.
so your guys should run the following shell:
cd /usr/local/include/mysql
cp mysql.h my_config.h
after that, run pip install MySQL-python and everything will be ok.
on MacOS Mojave, mysql_config is found at /usr/local/bin/ rather than /usr/local/mysql/bin as pointed above, so no need to add anything to path.
In my case my database is running on container and my flask app is running on another container when i tried updating code app got broke with error
Complete output from command python setup.py egg_info:
/bin/sh: 1: mysql_config: not found
/bin/sh: 1: mariadb_config: not found
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-bya8e734/mysqlclient/setup.py", line 15, in <module>
metadata, options = get_config()
File "/tmp/pip-build-bya8e734/mysqlclient/setup_posix.py", line 65, in get_config
libs = mysql_config("libs")
File "/tmp/pip-build-bya8e734/mysqlclient/setup_posix.py", line 31, in mysql_config
raise OSError("{} not found".format(_mysql_config_path))
OSError: mysql_config not found
Key in stack trace is
/bin/sh: 1: mysql_config: not found
because where my flask app is running doesn't have mysql client properly configured so first i installed mysql server and then install
sudo apt-get install mysql-server-5.7 -y
Then started MySQL
mansoor#LARC-mansur:~/Documents/clients/HR/DevopsSimulator/web$ sudo systemctl start mysql
Then install flask-mysql package and this time it worked
mansoor#LARC-mansur:~/Documents/clients/HR/DevopsSimulator/web$ sudo pip3 install flask-mysqldb
This is different case but posting here because may be someone else in the world facing same issue

Why do I get syntax error when I try to do a pip install on CentOS 6?

I'm trying to install a python module called cymysql on a CentOS 6 system by commands like
yum install -y python-setuptools && easy_install pip && pip install cymysql
However I received an Error message saying there're a few lines got SyntaxError: invalid syntax ( see the error code as following)---It actually happened when I run the third command pip install cymysql
Traceback (most recent call last):
File "/usr/local/bin/pip", line 11, in <module>
load_entry_point('pip==21.0', 'console_scripts', 'pip')()
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 561, in load_entry_point
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2631, in load_entry_point
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2291, in load
File "build/bdist.linux-x86_64/egg/pkg_resources/__init__.py", line 2297, in resolve
File "/usr/local/lib/python2.7/site-packages/pip-21.0-py2.7.egg/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
Not so sure what's happening here, just wondering if anyone can help? (I enclosed the pip file and main.py here but I don't know where is the init.py file).BTW, It's centOS 6 but I have installed the Python 3.6.10 on the server.When I run the command python3.6 -V, I got
python3.6.10
FIY
PIP file Code:
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==21.0','console_scripts','pip'
__requires__ = 'pip==21.0'
import re
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point('pip==21.0', 'console_scripts', 'pip')() //**THIS IS LINE 11**
)
Line 57 to 62 in Main.py
try:
cmd_name, cmd_args = parse_command(args)
except PipError as exc:
sys.stderr.write(f"ERROR: {exc}") //**THIS IS LINE 60**
sys.stderr.write(os.linesep)
sys.exit(1)
&tldr;
On an RPM-based distro, you use RPM packages as much as possible.
Never run pip as root user
You must not use pip as root user (or asking for trouble).
There is a whole infrastructure of Python packages installable through yum and this is what you should use.
You can still use pip to install Python modules, but only in very rare cases when a Python module is not available as a package. In that case you must use pip inside a virtual Python environment. Your case is such.
Make sure you have fixed yum for base, EPEL and set up SCLO repositories, since CentOS 6 is EOL.
Then:
# Ensure EPEL repository
yum -y install epel-release
# fix EPEL repo due to EOL:
curl https://www.getpagespeed.com/files/centos6-epel-eol.repo --output /etc/yum.repos.d/epel.repo
yum -y install centos-release-scl-rh
# don't forget to fix repository configuration due to EOL:
curl https://www.getpagespeed.com/files/centos6-scl-eol.repo --output /etc/yum.repos.d/CentOS-SCLo-scl.repo
curl https://www.getpagespeed.com/files/centos6-scl-rh-eol.repo --output /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
# install python 3.6 from Software collection packages (installing virtualenv will pick up both the base python and its pip package)
yum -y install rh-python36-python-virtualenv
Now you can create a virtual environment with any packages from pip, but you have to activate the software collection in your shell first, example:
. /opt/rh/rh-python36/enable
cd ${HOME}
# create "myapp" virtual environment
virtualenv myapp
# install desired packages using virtualenv's pip (will install to the virtualenv and not damage your system)
./myapp/bin/pip install cymysql
Do not go over pip version 20 on a Centos 6 system, otherwise wheels with precompiled c libraries will break.
Long story short, do this first:
pip install --upgrade pip==20.1.0
After that you can install cymysql

H5PY installed, but does not import

I am trying to import H5PY in my Python code, but the module cannot be found. I am using Ubuntu 16.04 and Python 2.7.
I have tried reinstalling h5py both with conda and with pip and individually and the following
sudo pip install cython
sudo apt-get install libhdf5-dev
sudo pip install h5py
but it still does not work.
Installing h5py tells me that it is already installed in /anaconda2/lib/python2.7/site-packages.
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/snap/pycharm-community/128/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: No module named h5py
Has anyone fixed this problem?
I found my solution!
I added found out the path where h5py is installed and added it to the path where python looks for the modules with
import sys
sys.path.append("mypath")
Hope this also helps others
For me the following worked:
sudo apt-get update
sudo apt-get install python3-h5py
Ref: https://www.devmanuals.net/install/ubuntu/ubuntu-16-04-LTS-Xenial-Xerus/how-to-install-python3-h5py.html

Unable to install any Python library in VS 2017: 'module' object has no attribute 'main' [duplicate]

I'm facing a problem with Package Installation, pip.
I am using Python 3.6.
When I try to install a package with pip I receive the following message in the cmd.
*C:\Python36\Scripts>pip intstall --user httpie
Traceback (most recent call last):
File "c:\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python36\Scripts\pip.exe\__main__.py", line 5, in <module>
ImportError: cannot import name 'main'*
It happened when I upgraded the pip, before it worked. I tried to uninstal the Python and installed this again but it does not work. I cannot find the solution, any ideas?
Firstly if you have installed pip then remove it by deleting the folder of pip inside python directory. Then install pip by this command:
$ sudo easy_install pip
This will install older version, you can download newer version after 10th when it will come.
I fixed it. I deleted the folder with pip as you said. Because I work in Windows 10, I downloaded the get-pip.py from https://pip.pypa.io/en/stable/installing/ and I run it.
You can also downgrade to pip 9.x using
python -m pip install -U "pip<10"

Ipython broke: doesn't start, says missing 'pathlib2' distribution

When i execute ipython in terminal i see following error
aman#ebex-MacBookPro:~/ipython_work$ ipython
Traceback (most recent call last):
File "/usr/local/bin/ipython", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 3141, in <module>
#_call_aside
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 3127, in _call_aside
f(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 3154, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 640, in _build_master
ws.require(__requires__)
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 941, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 828, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pathlib2' distribution was not found and is required by ipython
Just before that i followed
$ git clone --recursive https://github.com/ipython/ipython.git
$ cd ipython
$ pip install -e ".[notebook]"
From the link [How to autosave ipython notebook]
http://stackoverflow.com/questions/21517296/how-to-autosave-ipython-notebook
I tried to uninstall and install using apt-get but its still not working and throwing missing distribution error.
I installed pathlib2 though
sudo pip install pathlib2
But again while accessing ipython
I see another error
pkg_resources.DistributionNotFound: The 'backports.shutil_get_terminal_size' distribution was not found and is required by ipython
You need install some packages manually as follows:
pip install pathlib2
pip install backports.shutil_get_terminal_size
pip install enum34
For me, I also had to pip install enum34 but otherwise int32bit's answer worked.
pip install pathlib2 backports.shutil_get_terminal_size enum34
I see the same error When i execute ipython interminal. The problem was solved by installing libraries about the exception.
I launched the Jupyter notebook from Anaconda "Environments" and the dos Windows open with all the launching packages in issues appearing.
Then I just had to install packages missing and mentioned in this window in your environment one by one. You can retry the same process as many times as needed.