webdriver_manager icon indicating copy to clipboard operation
webdriver_manager copied to clipboard

'NoneType' object has no attribute 'write'

Open eugenio-jefferson opened this issue 3 years ago • 1 comments

Hi, this error is happening "'NoneType' object has no attribute 'write'", when downloading msedgedriver.

2022-08-16 11:21:18,919 - INFO - ====== WebDriver manager ======
2022-08-16 11:21:20,851 - INFO - Get LATEST edgedriver version for 104.0.1293 Edge
2022-08-16 11:21:22,810 - INFO - About to download new driver from https://msedgedriver.azureedge.net/104.0.1293.54/edgedriver_win64.zip
2022-08-16 11:21:23,038 - WARNING - 'NoneType' object has no attribute 'write'

eugenio-jefferson avatar Aug 16 '22 14:08 eugenio-jefferson

This error occurs in version 3.8.3, in version 3.8.1 it not occurs. I not tested in version 3.8.2

eugenio-jefferson avatar Aug 16 '22 18:08 eugenio-jefferson

Try 3.8.5+

aleksandr-kotlyar avatar Feb 02 '23 12:02 aleksandr-kotlyar

Hi @aleksandr-kotlyar and @eugeniojefferson04 ,

Same problem here, AttributeError: 'NoneType' object has no attribute 'write' even on version 3.8.5. Here is the stacktrace :

Traceback (most recent call last):
  [...]
  File "webdriver_manager\chrome.py", line 39, in install
  File "webdriver_manager\core\manager.py", line 30, in _get_driver_path
  File "webdriver_manager\core\download_manager.py", line 28, in download_file
  File "webdriver_manager\core\http.py", line 35, in get
  File "webdriver_manager\core\utils.py", line 289, in show_download_progress
  File "tqdm\std.py", line 1109, in __init__
  File "tqdm\std.py", line 1361, in refresh
  File "tqdm\std.py", line 1509, in display
  File "tqdm\std.py", line 350, in print_status
  File "tqdm\std.py", line 343, in fp_write
  File "tqdm\utils.py", line 89, in __getattr__
AttributeError: 'NoneType' object has no attribute 'write'

Important note : The code is executed in an application bundled with pyinstaller.

Yours faithfully, Birmania

Birmania avatar Feb 08 '23 18:02 Birmania

I am wondering if the problem may come from tqdm which seems to have this kind of problem when the code ie executed in a separate thread on Windows OS.

See : https://github.com/tqdm/tqdm/issues/794

Birmania avatar Feb 08 '23 19:02 Birmania

I am wondering if the problem may come from tqdm which seems to have this kind of problem when the code ie executed in a separate thread on Windows OS.

See : tqdm/tqdm#794

@Birmania hi! Seems so.

Ok. Let's triage it. Just disable that feature then and try again! ;-)

aleksandr-kotlyar avatar Feb 08 '23 22:02 aleksandr-kotlyar

Hi @aleksandr-kotlyar,

Unfortunately, I disabled the feature and the problem came back today. 👎

However, I may found the cause : We run webdriver_manager and so tqdm through a GUI no console interface, resulting in sys.stderr and sys.stdout being None. It seems to be known on Windows platform : Cf. https://bugs.python.org/issue1415

By default, tqdm output download status on sys.stderr (https://github.com/tqdm/tqdm/blob/v4.64.1/tqdm/std.py#L973) so as soon as print_status is called, we obtain a write call on NoneType.

As a workaround, we can override these outputs by NullWriter, found on the original issue link (or looking at this https://github.com/pyinstaller/pyinstaller/issues/3503#issuecomment-1291840135) :

class NullWriter:
    def write(self, data):
        pass

# Override stdout and stderr with NullWriter in GUI --noconsole mode
# This allow to avoid a bug where tqdm try to write on NoneType
if sys.stdout is None:
    sys.stdout = NullWriter()

if sys.stderr is None:
    sys.stderr = NullWriter()

Problem solved ! No more error occuring. 👍

Note : This error only occurs when ChromeDriverInstall need to downloaded Chrome. As soon as it have been downloaded once, it seems to reuse cache, making this error difficult to analyze.

Yours faithfully, Birmania

Birmania avatar Feb 10 '23 18:02 Birmania

@Birmania very cool you have found the root cause in sources and shared it here. Thank you for that great job.

Unfortunately we probably will not fix this in webdriver-manager because its never pays off to make such workarounds for the decorative unnecessary library.

The solution we can advice: disable tdqm in your webdriver-manager project. I was talking about this configuration of wdm from readme: https://github.com/SergeyPirogov/webdriver_manager#wdm_progress_bar Try disable tdqm like this. Will it work then?

Best regards, Aleksandr

aleksandr-kotlyar avatar Feb 10 '23 21:02 aleksandr-kotlyar