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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
Did you build the Python from source? If so, you need the --with-ssl option while building.
Method 3
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.
Method 4
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 🙂
Method 5
I had exactly the same problem. I fixed it without rebuilding python, as follows:
- 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.
- In this another server, check if import ssl works. It should work.
-
If it works, then try to find the _ssl lilbrary as follows:
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1938e8e95a18c98928493978493">[email protected]</a>]# find / -iname _ssl.so /usr/local/python27/lib/python2.7/lib-dynload/_ssl.so
-
Copy this file into the original server. Use the same destination folder:
/usr/local/python27/lib/python2.7/lib-dynload/ -
Double check owner and permissions:
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="285a47475c6845515b4d5a5e4d5a">[email protected]</a>]# chown root:root _ssl.so [<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b1acacb783aebab0a6b1b5a6b1">[email protected]</a>]# chmod 755 _ssl.so
- 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.
Method 6
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.
Method 7
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,
Method 8
Either install the supplementary packages for python-ssl using your package manager or
recompile Python using -with-ssl (requires OpenSSL headers/libs installed).
Method 9
On Solaris 11, I had to modify setup.py to include /opt/csw/include/openssl in the SSL include search path.
Uwe
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0