Ubuntu AttributeError: ‘module’ object has no attribute ‘SSL_ST_INIT’

If you install libraries with

pip

command you might find yourself in the following situation:

root@srv:~# pip
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 13, in <module>
    from pip.exceptions import InstallationError, CommandError, PipError
  File "/usr/lib/python2.7/dist-packages/pip/exceptions.py", line 6, in <module>
    from pip._vendor.six import iteritems
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 64, in <module>
    vendored("cachecontrol")
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/__init__.py", line 9, in <module>
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/wrapper.py", line 1, in <module>
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/adapter.py", line 3, in <module>
  File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/__init__.py", line 53, in <module>
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 54, in <module>
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
    from OpenSSL import rand, crypto, SSL
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 124, in <module>
    SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'


After a distribution upgrade from Ubuntu 14.04 to 16.04 some of the previously pip installed modules stopped working properly and they cannot be upgraded by the installer. So it is not a good practice to install packages with pip if they have Ubuntu packages.

The solution is just

remove the offensive directory and install the library, again!

Here we do not use “rm” to delete the directory, but we move it to another location (for example, “/root”) to backup it and to remove it from its default location. In such cases it’s always better to backup and remove the original location than to just remove the directory, who knows we may need it if something goes really wrong!

root@srv:~# sudo su
root@srv:~# mv /usr/local/lib/python2.7/dist-packages/OpenSSL /root/
root@srv:~# pip install -U pyOpenSSL
Collecting pyOpenSSL
  Downloading https://files.pythonhosted.org/packages/96/af/9d29e6bd40823061aea2e0574ccb2fcf72bfd6130ce53d32773ec375458c/pyOpenSSL-18.0.0-py2.py3-none-any.whl (53kB)
    100% |████████████████████████████████| 61kB 221kB/s 
Collecting six>=1.5.2 (from pyOpenSSL)
  Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Requirement already up-to-date: cryptography>=2.2.1 in /usr/local/lib/python2.7/dist-packages (from pyOpenSSL)
Collecting enum34; python_version < "3" (from cryptography>=2.2.1->pyOpenSSL)
  Downloading https://files.pythonhosted.org/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl
Requirement already up-to-date: asn1crypto>=0.21.0 in /usr/local/lib/python2.7/dist-packages (from cryptography>=2.2.1->pyOpenSSL)
Requirement already up-to-date: cffi!=1.11.3,>=1.7 in /usr/local/lib/python2.7/dist-packages (from cryptography>=2.2.1->pyOpenSSL)
Requirement already up-to-date: idna>=2.1 in /usr/local/lib/python2.7/dist-packages (from cryptography>=2.2.1->pyOpenSSL)
Collecting ipaddress; python_version < "3" (from cryptography>=2.2.1->pyOpenSSL)
  Downloading https://files.pythonhosted.org/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl
Requirement already up-to-date: pycparser in /usr/local/lib/python2.7/dist-packages (from cffi!=1.11.3,>=1.7->cryptography>=2.2.1->pyOpenSSL)
Installing collected packages: six, pyOpenSSL, enum34, ipaddress
  Found existing installation: six 1.10.0
    Not uninstalling six at /usr/lib/python2.7/dist-packages, outside environment /usr
  Found existing installation: pyOpenSSL 16.0.0
    Uninstalling pyOpenSSL-16.0.0:
      Successfully uninstalled pyOpenSSL-16.0.0
  Found existing installation: enum34 1.1.2
    Not uninstalling enum34 at /usr/lib/python2.7/dist-packages, outside environment /usr
  Found existing installation: ipaddress 1.0.16
    Not uninstalling ipaddress at /usr/lib/python2.7/dist-packages, outside environment /usr
Successfully installed enum34-1.1.6 ipaddress-1.0.22 pyOpenSSL-18.0.0 six-1.12.0
root@srv ~ # pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

Additional output

You can see the problem cannot be resolved with update or reinstall, because the pip finds the directory and expect to load the module:

root@srv:~# dpkg -l|grep pip
ii  libpipeline1:amd64                   1.4.1-2                                    amd64        pipeline manipulation library
ii  python-pip                           8.1.1-2ubuntu0.4                           all          alternative Python package installer
ii  python-pip-whl                       8.1.1-2ubuntu0.4                           all          alternative Python package installer
ii  python3-pip                          8.1.1-2ubuntu0.4                           all          alternative Python package installer - Python 3 version of the package
root@srv:~# apt install python3-pip python-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-pip is already the newest version (8.1.1-2ubuntu0.4).
python3-pip is already the newest version (8.1.1-2ubuntu0.4).
The following packages were automatically installed and are no longer required:
  linux-headers-3.13.0-24 linux-headers-3.13.0-24-generic linux-image-3.13.0-24-generic linux-image-extra-3.13.0-24-generic
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.
root@srv:~# python -V
Python 2.7.12
root@srv:~# pip2 install pyOpenSSL
Traceback (most recent call last):
  File "/usr/bin/pip2", line 9, in <module>
    load_entry_point('pip==8.1.1', 'console_scripts', 'pip2')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 542, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2569, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2229, in load
    return self.resolve()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2235, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 13, in <module>
    from pip.exceptions import InstallationError, CommandError, PipError
  File "/usr/lib/python2.7/dist-packages/pip/exceptions.py", line 6, in <module>
    from pip._vendor.six import iteritems
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 64, in <module>
    vendored("cachecontrol")
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/__init__.py", line 9, in <module>
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/wrapper.py", line 1, in <module>
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/adapter.py", line 3, in <module>
  File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/__init__.py", line 53, in <module>
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 54, in <module>
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
    from OpenSSL import rand, crypto, SSL
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 124, in <module>
    SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'

root@srv:~# pip2 install -U pyOpenSSL
Traceback (most recent call last):
  File "/usr/bin/pip2", line 9, in <module>
    load_entry_point('pip==8.1.1', 'console_scripts', 'pip2')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 542, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2569, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2229, in load
    return self.resolve()
  File "/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2235, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 13, in <module>
    from pip.exceptions import InstallationError, CommandError, PipError
  File "/usr/lib/python2.7/dist-packages/pip/exceptions.py", line 6, in <module>
    from pip._vendor.six import iteritems
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 64, in <module>
    vendored("cachecontrol")
  File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/__init__.py", line 9, in <module>
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/wrapper.py", line 1, in <module>
  File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/adapter.py", line 3, in <module>
  File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/__init__.py", line 53, in <module>
  File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/contrib/pyopenssl.py", line 54, in <module>
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
    from OpenSSL import rand, crypto, SSL
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 124, in <module>
    SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
root@srv:~# ls -altr /usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py
-rw------- 1 root root 65521 Jun 23  2016 /usr/local/lib/python2.7/dist-packages/OpenSSL/SSL.py

Here we have another potential problem, the directory is readable ONLY for root. Probably bad the installation by the automation system (used cf-engine, which by default execute with root and only root permissions for newly created files by the process?).
Also be careful which installation you execute and the files and directories which it uses – you can have multiple pip installation in /usr/lib or /usr/local/lib and so on.

Leave a Reply

Your email address will not be published.