読者です 読者をやめる 読者になる 読者になる

EC2上にPython実行環境を作った話

大したことはしてない。

パッケージ更新とpip

sudo yum -y update
sudo easy_install pip

virtualenvとvirtualenvwrapper

あとはまあイアン先生の言うとおりに。
http://www.ianlewis.org/jp/virtualenv-pip-fabric

sudo pip install virtualenv
sudo pip install virtualenvwrapper
vim .bashrc

export WORKON_HOME=$HOME/.virtualenvs
source`which virtualenvwrapper.sh`
mkvirtualenv myenv

できたけど、Python2.7を使いたいぞ

Python2.7を入れる

cd /usr/local/src/
sudo wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
sudo tar xzf  Python-2.7.3.tgz
./configure --with-threads --enable-shared --prefix=/usr/local/python-2.7.3

Cコンパイラが入ってなかった

sudo yum -y groupinstall 'Development tools'

もう一度

./configure --with-threads --enable-shared --prefix=/usr/local/python-2.7.3
make
sudo make install
sudo vim /etc/ld.so.conf.d/python2.7.conf
/usr/local/python-2.7.3/lib

[ec2-user@ip-255-255-255-255 Python-2.7.3]$ /usr/local/python-2.7.3/bin/python --version
Python 2.7.3

さっき作った環境のインタプリタを2.7.3にする

[ec2-user@ip-255-255-255-255 ~]$ virtualenv --python=/usr/local/python-2.7.3/bin/python ~/.virtualenvs/myenv/

Running virtualenv with interpreter /usr/local/python-2.7.3/bin/python
New python executable in /home/ec2-user/.virtualenvs/myenv/bin/python
Installing setuptools............................done.
Installing pip...............done.
[ec2-user@ip-255-255-255-255 ~]$ workon myenv
(myenv)[ec2-user@ip-255-255-255-255 ~]$ python --version
Python 2.7.3

できた。

botoでメール送ろうとしても送れない

(myenv)[ec2-user@ip-255-255-255-255 myenv]$ python ses_test.pyTraceback (most recent call last):
  File "ses_test.py", line 25, in <module>
    ,return_path='shrkwh@gmail.com' # bounceメールを返す場所
  File "/home/ec2-user/.virtualenvs/myenv/lib/python2.7/site-packages/boto/ses/connection.py", line 248, in send_email
    return self._make_request('SendEmail', params)
  File "/home/ec2-user/.virtualenvs/myenv/lib/python2.7/site-packages/boto/ses/connection.py", line 98, in _make_request
    data=urllib.urlencode(params)
  File "/home/ec2-user/.virtualenvs/myenv/lib/python2.7/site-packages/boto/connection.py", line 829, in make_request
    return self._mexe(http_request, sender, override_num_retries)
  File "/home/ec2-user/.virtualenvs/myenv/lib/python2.7/site-packages/boto/connection.py", line 723, in _mexe
    connection = self.get_http_connection(request.host, self.is_secure)
  File "/home/ec2-user/.virtualenvs/myenv/lib/python2.7/site-packages/boto/connection.py", line 596, in get_http_connection
    return self.new_http_connection(host, is_secure)
  File "/home/ec2-user/.virtualenvs/myenv/lib/python2.7/site-packages/boto/connection.py", line 616, in new_http_connection
    connection = httplib.HTTPSConnection(host,
AttributeError: 'module' object has no attribute 'HTTPSConnection'

Pythonを入れたときにSSLが有効でなかった
openssl-develを入れてもっかいビルド

sudo yum install -y openssl openssl-devel
cd /usr/local/src/Python-2.7.3

ちゃんとlibを指すように修正

vim Modules/Setup

SSL=/usr
make clean
make
sudo make install

完了したので試す

workon myenv
cd ~/myenv/
python ses_test.py 

はい。できましたー