Until you use PhantomJS with Python on Heroku

Since the method of calling PhantomJS differs between the development environment and the production environment, it is sorted by host name.

It is not essential at all, and it is only necessary to pass the path so that PhantomJS can be used in the development environment, but due to various circumstances, the path could not be passed to the development environment, so we will implement it as a provisional measure.

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

Error in execution

When I upload the above source to Heroku and execute it without thinking about anything, the following error occurs as expected.

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'

When I entered Heroku and confirmed, PhantomJS was not created.

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

Settings for Heroku

Set 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

Once you've done this, upload to 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

If you log in to Heroku's bash remotely, you'll see that a "vendor" folder has been created.

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
~ $ 

I accessed the URL in this state and confirmed the operation safely.

Recommended Posts

Until you use PhantomJS with Python on Heroku
Until you can use opencv with python
Until you run python with apache
Note until you use emacs with WSL
Use selenium phantomjs webdriver with python unittest
A note on what you did to use Flycheck with Python
[Python] Use JSON with Python
Scraping with Python + PhantomJS
Until you use the Kaggle API with Colab
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Until you publish (deploy) a web application made with bottle on Heroku
Until you install Python with pythonbrew and run Flask on a WSGI server
Scraping with Python, posting on TwitterBot, regular execution on Heroku
Site notes to help you use NetworkX with Python
Use Trello API with python
Use matplotlib on Ubuntu 12 & Python
Use Twitter API with Python
Use TUN / TAP with Python
Use subsonic API with python3
Use Python on Windows (PyCharm)
Until you create a machine learning environment with Python on Windows 7 and run it
Solution when you want to use cv_bridge with python3 (virtualenv)
Until you create Python Virtualenv on Windows and launch Jupyter
Until you can install your own Python library with pip
Use Python 3 introduced with command line tools on macOS Catalina
Until you use Python 3 system on Sakura rental server (create verification environment) Part 1 Contract-Initial setting
Use Numpy, Scipy, scikit-learn on Heroku
Python: How to use async with
Make Echolalia LINEbot with Python + heroku
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Until you start Jupyter with Docker
Until you put Python in Docker
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
python + django + scikit-learn + mecab (1) on heroku
Next, use Python (Flask) for Heroku!
Use rospy with virtualenv in Python3
python + django + scikit-learn + mecab (2) on heroku
Python json.loads () returns str on Heroku
Use Python in pyenv with NeoVim
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Until you use Google cola boratory
Notes on using rstrip with python.
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Getting started with Python 3.8 on Windows
Sakura Use Python on the Internet
Use PostgreSQL with Lambda (Python + psycopg2)
Until you shake ruby with renpy
Python | What you can do with Python
[Memo] Tweet on twitter with python
Periodically run Python on Heroku Scheduler
How to use python put in pyenv on macOS with PyCall
Get the width of the div on the server side with Selenium + PhantomJS + Python
Run servo with Python on ESP32 (Windows)
Deploy Python face recognition model on Heroku and use it from Flutter ②