Blog Entry 13 years, 3 months ago

Failed importing SSL from Python 2.6

Usually, (first time) when working with Python from a separated environment (created by virtualenv, or something like that) we could be hit by an error due to ssl importing failure. This is how the error looks like.

Usually, (first time) when working with Python from a separated environment (created by virtualenv, or something like that) we could be hit by an error due to ssl importing failure. This is how the error looks like:

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

Okay. The reason is because Python cannot find the ssl library (in Python 2.6 and later, it doesn't require the python-ssl installed anymore.) Here is the steps we can do:

1a. Ensure of having OpenSSL installed in the common environment (/usr/lib/, /usr/local/lib,..). In Redhat distros, the packages are openssl, openssl-devel

OR

1b. Download and install the OpenSSL from source into your prefer location

2. Get into your Python source directory, edit the file ./Modules/Setup (some places written that is the Setup.dist but It won't work to me). Uncomment some lines like below:

# Socket module helper for socket(2)
_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c \
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    -L$(SSL)/lib -lssl -lcrypto

3. Rebuild the Python:

$ make
$ make install

4. Confirm that the error won't show again.

Note: Dealing with older Python could be more tricky, but it requires the python-ssl installed additionally

B?n ti?ng Vi?t: http://django.vn/blog/xu-ly-loi-import-ssl-trong-python

Recent Reads