Heroku上のPythonでPhantomJSを使うまで


##開発環境と本番環境でPhantomJSの呼び出し方が異なるため、ホスト名で振り分け
全く必須でなく、開発環境でもPhantomJSが使えるようにパスを通せばいいだけなのですが、諸々の事情により開発環境にはパスが通せなかったため、暫定対応としていかを実施。

    if os.uname()[1] == "local host name":
        driver = webdriver.PhantomJS(executable_path='/Applications/phantomjs-1.9.2-macosx/bin/phantomjs')
    else:    
        driver = webdriver.PhantomJS()

## 実行でエラー
何も考えずに上記のソースをHerokuへアップロードし実行すると、案の定、以下のエラーが発生。

```
app[web.1]:     driver = webdriver.PhantomJS()
app[web.1]:   File "/app/.heroku/python/lib/python3.4/site-packages/selenium/webdriver/phantomjs/service.py", line 76, in start
app[web.1]:     raise WebDriverException("Unable to start phantomjs with ghostdriver: %s" % e)
app[web.1]: selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver: [Errno 2] No such file or directory: 'phantomjs'
```

Herokuに入って確認すると、PhantomJSができていない。

```
bash-3.2$ heroku run bash
Running bash on hogehoge... up, run.3889
~ $ pwd
/app
~ $ ls
app.py		 Procfile  python-stack    requirements.txt  views
ghostdriver.log  python    python-version  runtime.txt
~ $ PhantomJS
bash: PhantomJS: command not found
~ $ Phantomjs
bash: Phantomjs: command not found
```

## Herokuに関する設定

buildpackの設定を実施する。

1. $ heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
2. touch .buildpacks
3. echo "https://github.com/heroku/heroku-buildpack-python" >> .buildpacks
4. echo "https://github.com/stomita/heroku-buildpack-phantomjs" >> .buildpacks

ここまで実施したら、Herokuへアップロードを実施。

```
bash-3.2$ git push heroku master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 335 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Fetching custom git buildpack... done
remote: -----> Multipack app detected
remote: =====> Downloading Buildpack: https://github.com/heroku/heroku-buildpack-python
remote: =====> Detected Framework: Python
remote: -----> Installing runtime (python-3.4.3)
remote: -----> Installing dependencies with pip
remote:        Collecting bottle==0.12.8 (from -r requirements.txt (line 1))
remote:          Downloading bottle-0.12.8.tar.gz (69kB)
remote:        Collecting Jinja2==2.8 (from -r requirements.txt (line 2))
remote:          Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB)
remote:        Collecting lxml==3.4.4 (from -r requirements.txt (line 3))
remote:          Downloading lxml-3.4.4.tar.gz (3.5MB)
remote:        Collecting MarkupSafe==0.23 (from -r requirements.txt (line 4))
remote:          Downloading MarkupSafe-0.23.tar.gz
remote:        Collecting selenium==2.48.0 (from -r requirements.txt (line 5))
remote:          Downloading selenium-2.48.0.tar.gz (805kB)
remote:        Collecting wheel==0.26.0 (from -r requirements.txt (line 6))
remote:          Downloading wheel-0.26.0-py2.py3-none-any.whl (63kB)
remote:        Installing collected packages: bottle, MarkupSafe, Jinja2, lxml, selenium, wheel
remote:          Running setup.py install for bottle
remote:          Running setup.py install for MarkupSafe
remote:          Running setup.py install for lxml
remote:          Running setup.py install for selenium
remote:        Successfully installed Jinja2-2.8 MarkupSafe-0.23 bottle-0.12.8 lxml-3.4.4 selenium-2.48.0 wheel-0.26.0
remote: 
remote: =====> Downloading Buildpack: https://github.com/stomita/heroku-buildpack-phantomjs
remote: =====> Detected Framework: PhantomJS
remote: -----> Extracting PhantomJS 1.9.8 binaries to /tmp/build_57462938b8344afe2a9119824547eac1/vendor/phantomjs
remote: -----> exporting PATH and LIBRARY_PATH
remote: Using release configuration from last framework (PhantomJS).
remote: -----> Discovering process types
remote:        Procfile declares types     -> renderer, web
remote:        Default types for Multipack -> console
remote: 
remote: -----> Compressing... done, 59.0MB
remote: -----> Launching... done, v8
remote:        https://hogehoge.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/hogehoge.git
   71335d7..c897730  master -> master
```


Herokuのbashへリモートでログインすると「vendor」フォルダができていることがわかる。

```
bash-3.2$ heroku run bash
Running bash on hogehoge... up, run.6077
~ $ pwd
/app
~ $ ls
app.py		 Procfile  python-stack    requirements.txt  vendor
ghostdriver.log  python    python-version  runtime.txt	     views
~ $ 
```

この状態でURLへアクセスし、無事動作を確認しました。