pip3 installs inside virtual environment with python3.6 failing due to ssl module not available

I followed the below steps for python3.6 installation in ubuntu 14.04 and virtualenv pip installs works fine.

Python 3.6 Installation:

sudo apt-get install python3-dev libffi-dev libssl-dev
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz  
tar xvf Python-3.6.0.tgz
cd Python-3.6.0
./configure --enable-optimizations  
make -j8  
sudo make altinstall
python3.6

If seeing the following error --

zipimport.ZipImportError: can't decompress data; zlib not available make: *** [altinstall] Error 1

try:

sudo apt-get install zlib1g-dev

Validation:

Create virtualenv in python3.6:

python3.6 -m venv testenv
source testenv/bin/activate
pip install pep8

using pip:

(testenv) vagrant@pvagrant-dev-vm:~$ pip install pep8
*Collecting pep8
  Downloading pep8-1.7.0-py2.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 4.1MB/s
Installing collected packages: pep8
Successfully installed pep8-1.7.0*

(testenv) vagrant@pvagrant-dev-vm:~$ pip list

pep8 (1.7.0) 
pip (9.0.1)
setuptools (28.8.0)

I stumbled upon the same issue when I tried to create a virtual environment utilising python3.6.0. Here is my solution for Mac OS X 10.12.2 (Py_minion comment was pretty close):

Setup

I created the environment by the following steps: downloading python3.6.0 running

./configure --prefix=<some_path>`
make
make install
mkvirtualenv --python=<some_path/bin/python3.6> foo

So basically similar to: https://stackoverflow.com/a/11301911/1286093

An indication if you have the same issue as I had would be a similar line when running make

The necessary bits to build these optional modules were not found: _ssl

Solution

Install openssl

brew install openssl
brew unlink openssl && brew link openssl --force

Change Module/Setup or Module/Setup.dist

You can find those files in the directory of the downloaded Python version.

Comment in and, if necessary change, lines 209 - 211 (I had to change the SSL variable to my openssl location).

SSL=/usr/local/opt/openssl <---- THIS DEPENDS ON YOUR INSTALLATION
_ssl _ssl.c \
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    -L$(SSL)/lib -lssl -lcrypto

Given that this was the location of openssl

Set environment variables

export CFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib"

make and install again

Running

./configure --prefix=<some_path>`
make
make install
mkvirtualenv --python=<some_path/bin/python3.6> foo

again did the trick for me