Forums

Flask import errors

I have been getting errors when attempting to connect to my site. The error when visiting the site is: Error code: Unhandled Exception

Below is the error log that is generated:

2020-03-02 23:25:20,658: Error running WSGI application
2020-03-02 23:25:20,663: TypeError: __init__() got an unexpected keyword argument 'options'
2020-03-02 23:25:20,663:   File "/var/www/dota2draftassistant_pythonanywhere_com_wsgi.py", line 16, in <module>
2020-03-02 23:25:20,663:     from flask_app import app as application  # noqa
2020-03-02 23:25:20,663: 
2020-03-02 23:25:20,663:   File "/home/dota2draftassistant/mysite/flask_app.py", line 3, in <module>
2020-03-02 23:25:20,663:     from heroTool import playStyles, draftSherpa
2020-03-02 23:25:20,663: 
2020-03-02 23:25:20,663:   File "/home/dota2draftassistant/mysite/heroTool.py", line 9, in <module>
2020-03-02 23:25:20,663:     browser = webdriver.Chrome(options=chrome_options)

I've followed the advice on https://help.pythonanywhere.com/pages/DebuggingImportError/ and haven't been able to resolve my issue.

When I try and manually run the wsgi file I get the following errors:

23:37 ~ $ python -i /var/www/dota2draftassistant_pythonanywhere_com_wsgi.py
Traceback (most recent call last):
  File "/var/www/dota2draftassistant_pythonanywhere_com_wsgi.py", line 16, in <module>
    from flask_app import app as application  # noqa
  File "/home/dota2draftassistant/mysite/flask_app.py", line 3, in <module>
    from heroTool import playStyles, draftSherpa
  File "/home/dota2draftassistant/mysite/heroTool.py", line 9, in <module>
    browser = webdriver.Chrome(options=chrome_options)
TypeError: __init__() got an unexpected keyword argument 'options'

Any assistance would be greatly appreciated.

I think you may not be using the most recent version of selenium. What version are you using?

It appears you are correct. Although I thought I upgraded selenium through PIP when I was provided the new chrome browser solutions.

Help on package selenium:

NAME selenium

DESCRIPTION # Licensed to the Software Freedom Conservancy (SFC) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The SFC licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License.

PACKAGE CONTENTS common (package) selenium webdriver (package)

VERSION 2.53.6

FILE /usr/lib/python3.8/site-packages/selenium/init.py

(END)

When I try and upgrade selenium I get this error:

18:48 ~ $ pip install -U selenium                                                                                                                                                                                              
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More detai
ls about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Looking in links: /usr/share/pip-wheels
Collecting selenium
  Using cached https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: urllib3 in /usr/local/lib/python2.7/dist-packages (from selenium) (1.24.3)
Installing collected packages: selenium
  Found existing installation: selenium 2.53.6
    Uninstalling selenium-2.53.6:
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/selenium-2.53.6.dist-info/top_level.txt'
Consider using the `--user` option or check the permissions.
18:48 ~ $ pip install -U python
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More detai
ls about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Looking in links: /usr/share/pip-wheels
Requirement already up-to-date: python in /usr/lib/python2.7/lib-dynload (2.7.12)

See our help page: http://help.pythonanywhere.com/pages/InstallingNewModules/

Awesome! Adjusting which pip I installed selenium to did the trick. Thanks!

Alright... so that fixed the error I was encountering, and I was able to successfully use my program once, but then I get an "Internal Server Error" served. Here are the error logs:

2020-03-03 23:20:01,743: Exception on / [POST]
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 158, in _new_conn
    conn = connection.create_connection(
  File "/usr/lib/python3.8/site-packages/urllib3/util/connection.py", line 80, in create_connection
    raise err
  File "/usr/lib/python3.8/site-packages/urllib3/util/connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 597, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.8/http/client.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1276, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1225, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1004, in _send_output
    self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 944, in send
    self.connect()
  File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 181, in connect
    conn = self._new_conn()
  File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 167, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fd25bc966a0>: Failed to establish a new connection: [Errno 111] Connection refused
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/dota2draftassistant/mysite/flask_app.py", line 13, in landingPage
    result = draftSherpa(chosenStyle)
  File "/home/dota2draftassistant/mysite/heroTool.py", line 13, in draftSherpa
    browser.get("https://www.dotabuff.com/heroes/winning?date=week")
  File "/home/dota2draftassistant/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/home/dota2draftassistant/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/dota2draftassistant/.local/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/dota2draftassistant/.local/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/usr/lib/python3.8/site-packages/urllib3/request.py", line 70, in request
    return self.request_encode_body(method, url, fields=fields,
  File "/usr/lib/python3.8/site-packages/urllib3/request.py", line 150, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/usr/lib/python3.8/site-packages/urllib3/poolmanager.py", line 324, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 663, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 663, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 663, in urlopen
    return self.urlopen(method, url, body, headers, retries,
  File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 637, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "/usr/lib/python3.8/site-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=60272): Max retries exceeded with url: /session/eae70c70a5684d6a0e414b3391460b4b/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd25bc966a0>: Failed to establish a new connection: [Errno 111] Connection refused'))

Any ideas?

It looks like the browser is crashing for some reason. What code are you using to start Selenium and create your browser object? This is code that has worked for us:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)

try:
    browser.get("https://www.google.com")
    return "Page title was '{}'".format(browser.title)

finally:
    browser.quit()