77

Ubuntu Maverick w/Python 2.7:

I can't figure out what to do to resolve the following import error:

>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/ssl.py", line 60, in <module>
   import _ssl             # if we can't import it, let the error propagate
ImportError: No module named _ssl

UPDATE: I recompiled the source. I was unable to figure out how to add the --with-ssl option the answers below mention, instead I got this to work by editing the lines regarding SSL in /Modules/Setup.dist.

6

10 Answers 10

64

Unrelated to the original question, but because this is the first Google result... I hit this on Google AppEngine and had to add:

libraries:
- name: ssl
  version: latest

to app.yaml per: https://cloud.google.com/appengine/docs/python/sockets/ssl_support

Please NOTE: This seems to work upto Python version 2.7.9 but not for 2.7.10 or 2.7.11.

2
  • I just tried this and it completely crashed my app to the extent that having commented it out I'm getting a new error and if I leave it in I get ImportError: cannot import name RAND_egd
    – HenryM
    Commented Mar 3, 2016 at 7:43
  • 1
    I've downgraded my version of python from 2.7.11 to 2.7.9 and it now seems to be working
    – HenryM
    Commented Mar 3, 2016 at 9:46
51

Since --with-ssl is not recognized anymore I just installed the libssl-dev. For debian based systems:

sudo apt-get install libssl-dev 

For CentOS and RHEL

sudo yum install openssl-devel

To restart the make first clean up by:

make clean

Then start again and execute the following commands one after the other:

./configure
make
make test
make install

For further information on OpenSSL visit the Ubuntu Help Page on OpenSSL.

2
  • 2
    If you are on CentOS the command is: sudo yum install openssl-devel Commented Nov 19, 2020 at 15:31
  • 2
    After having openssl-devel installed on my centos, still having the same error message.
    – Kemin Zhou
    Commented Sep 8, 2023 at 19:33
43

Did you build the Python from source? If so, you need the --with-ssl option while building.

7
  • 2
    I can't remember if I built from source or not. when do I add the --with-ssl? the ./configure, make, or make install step. when I added this to ./configure it said --with-ssl option was not recognized
    – J Cooper
    Commented Feb 26, 2011 at 18:43
  • 2
    It's an option to configure. In general, configuration options, compile-time-switches, and the like, are options to configure -- what it actually does is run over many of the source files, making text substitutions (and often creating new header files), before build time.
    – jelford
    Commented Jan 7, 2012 at 9:20
  • 9
    I tried to install Python 2.7 and --with-ssl does not seem to be recognized any longer...
    – Emmanuel
    Commented Oct 29, 2013 at 12:32
  • 24
    Ignore the rest of the advice internet, it's out of date. The extensions build is now controlled by setup.py now. Hack that file, and don't touch Modules/Setup.dist or --with-ssl.
    – ACyclic
    Commented Mar 7, 2014 at 23:39
  • 9
    You don't need --with-ssl anymore when building from source as it will look for ssl by default. Make sure you have openssl-devel or libssl-dev (name depending on your distro) installed and/or hack the setup.py file as @ACyclic mentioned to make sure the Python installation is pointing to the ssl headers correctly. Commented Dec 29, 2016 at 18:49
15

If you built Python from source, this is just a matter of dependencies: since you miss OpenSSL lib installed, python silently fails installing the _ssl module. You can see it in the final report of the make command:

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _sqlite3           _ssl
_tkinter           bsddb185           dbm
dl                 gdbm               imageop
sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

Installing OpenSSL lib in any of the standard lib paths (/usr/lib, /usr/local/lib...) should do the trick. Anyway this is how I did :-)

2
  • 1
    on a linux system it will work if you install openssl-devel, then reconfigure and recompile. Remember to recompile or reinstall any services that might need ssl (like tornado) to get ssl.
    – MrBooks
    Commented May 2, 2014 at 20:05
  • 1
    on Ubuntu I was able to get the dependencies required to compile the optional modules with sudo apt-get build-dep python3.4
    – gengkev
    Commented Jul 11, 2015 at 21:51
8

I had exactly the same problem. I fixed it without rebuilding python, as follows:

  1. Find another server with the same architecture (i386 or x86_64) and the same python version (example: 2.7.5). Yes, this is the hard part. You can try installing python from sources into another server if you can't find any server with the same python version.

  2. In this another server, check if import ssl works. It should work.

  3. If it works, then try to find the _ssl lilbrary as follows:

    [root@myserver]# find / -iname _ssl.so
    /usr/local/python27/lib/python2.7/lib-dynload/_ssl.so
    
  4. Copy this file into the original server. Use the same destination folder: /usr/local/python27/lib/python2.7/lib-dynload/

  5. Double check owner and permissions:

    [root@myserver]# chown root:root _ssl.so
    [root@myserver]# chmod 755 _ssl.so
    
  6. Now you should be able to import ssl.

This worked for me in a CentOS 6.3 x86_64 environment with python 2.7.3. Also I had python 2.6.6 installed, but with ssl working fine.

2
  • 1
    This fixed my problem with linuxbrew python 2.7.10 actually, you don't really need to find another server. Install linuxbrew and then install pyenv. Build same python release using pyenv and copy the _ssl.so over. I also need to remove _ssl_failed.so from ~/.linuxbrew/Cellar/python/2.7.10/lib/python2.7/lib-dynload directory. Commented Jun 1, 2015 at 5:29
  • 1
    A find is very heavy, maybe try: locate ssl.so or locate _ssl.so first. Commented Feb 10, 2017 at 15:28
3

The underscore usually means a C module (i.e. DLL), and Python can't find it. Did you build python yourself? If so, you need to include SSL support.

2

I am writing this solution for those who are still facing such issue and cant find the solution.

in my case, I am using

shared hosting (Cpanel Access) Linux CentOS.

I was facing this issue

No module named '_ssl'

I tried for all possible solutions but as you know sometimes things don't work for you and in hosting you don't have access to fully root and run queries. even my hosting provider did for me.. but NO GOOD RESULT.

so how I solved if you are using shared hosting and you have deployed your Django App using

Setup Python App

You only have to downgrade your Python Version, I downgraded from

Python 3.7.3

(As Python 3.7 does not have SSL module in it) To

Python 3.6.8

through Setup Python App.

Hope it will be helpful for someone with the same issue,

0

Either install the supplementary packages for python-ssl using your package manager or recompile Python using -with-ssl (requires OpenSSL headers/libs installed).

0

On Solaris 11, I had to modify setup.py to include /opt/csw/include/openssl in the SSL include search path.

Uwe

0

copy below file from Anaconda3\Library\bin to your exe folder

libcrypto-1_1-x64.dll
libcrypto-1_1-x64.pdb
libpng16.dll
libsodium.dll
libssh2.dll
libssl-1_1-x64.dll

Not the answer you're looking for? Browse other questions tagged or ask your own question.