Mac can't find chromedriver when running python script from launchctl - python

I've been trying to schedule one of my python scripts through launchctl and am coming up against an error. The intention is that this runs at a given time past the hour, every hour.
I've created my plist, loaded it in launchctl but it's not working properly. After doing some digging I debugged and got the following error messages:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/petersmith/Documents/Python/thenewsproject/link_getters/link_getter.py", line 3, in
import myfile
File "/Users/directories/myfile.py", line 51, in
browser = webdriver.Chrome('chromedriver', options=chrome_options)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in init
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
I've updated the items in bold when pasting.
This is the line that it's fussing about:
browser = webdriver.Chrome('chromedriver', options=chrome_options)
Now, chromedriver lives in my usr/local/bin folder and I've checked my path file and see:
-bash: /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin: No such file or directory
You can see in bold that it's there so I'm a little lost.
Any ideas?

I found the original answer from peetysmith was correct for me. However, the terminal command they provided to update the PATH variable in Launchd did not work for me (I'm using High Sierra).
I found the below worked:
sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
You need to restart afterwards and no need to reload the plist file (unless you edited it).

OK - so after a huge amount of searching I finally figured this out. There were actually two things causing me a problem when getting this working and they both related to the way that launchctl executes the work on behalf of the user.
The key things to remember are that launchctl will execute a process on your behalf and that means:
It has it’s own PATH variable (which you need to update with your chromedriver location), and
It executes your script from it’s own working directory, rather than the one your python script lives in. This will give you an issue for any other files you want to include or relative paths you use in your script(s)
To amend launchctl’s path you therefore need to run the following command from the terminal:
launchctl setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Can’t remember if you then needed a restart or not but definitely unload and then reload your plist file
launchctl load ~Library/LaunchAgents/your.own.plist
You should then be good to go.
Fixing the second point is then a simple key-string pair to add yo your plist file
<key>WorkingDirectory</key>
<string>/Users/you/path/to/mypythonfile/</string>
Hope this is helpful for others!

Related

I am new to coding and just do not understand what this error code means

I have only recently begun coding with Python 3. I don't know how to phrase the question that I mean to ask as I don't understand any part of these errors that were returned.
Traceback (most recent call last):
File "C:\Users\Liam McAuley\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\Liam McAuley\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Liam McAuley\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Liam McAuley\AppData\Local\Programs\Python\Python38\shrek.py", line 13, in <module>
driver = webdriver.Chrome('chromedriver.exe')
File "C:\Users\Liam McAuley\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Liam McAuley\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Can anyone help, please?
Your system cannot find the chromerdriver executable. As you are using Windows, I'd recommend using a full path definition of the executable:
driver = webdriver.Chrome(executable_path="C:\Users\Liam McAuley\path\to\actual\chromedriver.exe")
Alternatively, you can add chromedriver.exe to your path by typing this command into Command Prompt (make sure you are running as administrator!)
setx CHROMEDRIVER=C:\Users\Liam McAuley\path\to\actual\chromedriver.exe
From reading the trace it may seem as if there are two things that are going wrong. 1st one is that it seems as if python cannot find the file you are specifying so perhaps check your path. the second one is that you may not have chromedriver on your PATH. if you're using windows you can add it to the path via the environment variables tab. you can also follow the steps found here
Selenium tries to access chromdriver which is a Webdriver it uses to execute the
driver = webdriver.Chrome('chromedriver.exe')
In order to use the chromedriver, it has to be on path. You can get the chromedriver here. As for how to add stuff to path, you can either use SETX temporarily
setx CHROMEDRIVER=path/to/chromedriver.exe
or you can create a folder for the driver and then add that folder permanently to path by following these instructions here

Format error in python Selenium package

I'm running on Linux Debian 9 and PyCharmwith web scraping purposes; I'm currently using Python 3.5 as interpreter.
The script is the following:
from selenium import webdriver
import time
import datetime
from selenium.webdriver.common.keys import Keys
Till here, the script works fine, by importing the packages properly; when I try to set the driver by running the following line:
driver = webdriver.Firefox(executable_path='/home/quant/Desktop/DataDownload/venv/bin/geckodriver')
I get the following error message, relative to a format problem:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/quant/Desktop/DataDownload/venv/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 157, in __init__
self.service.start()
File "/home/quant/Desktop/DataDownload/venv/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1282, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
Browsing on the web, I found that probably this problem comes out when the executable program has not been unzipped and made executable correctly; to do that, I followed the steps below, by running them on the terminal:
(1).zip file download from the official repository in the github website:
wget [here][1]
(2) unzipped the file:
cd /home/quant/Downloads
tar -xvzf geckodriver-v0.21.0-arm7hf.tar.gz
(3) made the file executable:
chmod +x geckodriver
(4) moved the file on the following path:
mv geckodriver /home/quant/PycharmProject/DataDownloads/venv/bin/
Could someone help me to understand what's wrong, please?
Thanks in advance all!!
[Errno 8] Exec format error
This means you are trying to run a version of geckodriver that is compiled for a different architecture... you downloaded the ARM version (geckodriver-v0.21.0-arm7hf.tar.gz) and are most likely running on a x86/amd64 machine.
Solution:
go back to the geckodriver releases page and download the correct version for your system: https://github.com/mozilla/geckodriver/releases.
For example, if you are running 64 bit Linux, you want to download: geckodriver-v0.21.0-linux64.tar.gz

How to put geckodriver into PATH? [duplicate]

This question already has answers here:
Selenium using Python - Geckodriver executable needs to be in PATH
(36 answers)
Closed 6 years ago.
I'm on OS Sierra and i'm running Python 3.5.2. I have selenium installed and i'm following a book called "Automate the Boring Tasks With Python"
My code is
from selenium import webdriver
>>> browser = webdriver.Firefox()
I keep receiving the error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
I have searched far and wide for solutions to my issue. Many people have the same issue.. but none of the solutions are working. I have geckodriver copied everywhere in my Python folders. I've tried using terminal and I have tried specifying the path within the code and it still gives me errors. I hope somebody can help me. I'm sorry if this is in the wrong format, I don't know what i'm doing.
I faced this same problem and here's how I fixed it:
Download the geckodriver from here
Extract and unzip and move the geckodriver file to /usr/local/bin/ directory
Run python program with selenium Firefox webdriver.
You will need to add the binary to your $PATH. You can do this by:
export PATH=$PATH:/path/to/geckodriver
"I have geckodriver copied everywhere in my Python folders."
Ensure the geckodriver executable is found in one of the paths when you run:
import sys
print sys.path
And the problem should be resolved.

OSError: [Errno 8] Exec format error selenium

Trying to learn how to use selenium, I managed to overcome first error which involved chrome driver not being in the path name but it has thrown up another error.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/williamneal/Scratch/Titanic/chromedriver')
driver.get("http://www.bbc.com")
The error:
Traceback (most recent call last):
File "<ipython-input-1-84256e62b8db>", line 5, in <module>
driver = webdriver.Chrome('/Users/williamneal/Scratch/Titanic/chromedriver')
File "/Users/williamneal/anaconda/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/Users/williamneal/anaconda/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/Users/williamneal/anaconda/lib/python3.5/subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "/Users/williamneal/anaconda/lib/python3.5/subprocess.py", line 1544, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
There is a potential solution here, which involves installing Chrome Drivers via Home Brew but that option is not available to me.
Any ideas?
Looks like this is complaining about the format of chromedriver binary.
It might be because of platform and chromedriver format mismatch. For example windows requires chromedriver.exe while there are different formats for linux and mac.
If you don't want to install through package manager, just download chromedriver from https://sites.google.com/a/chromium.org/chromedriver/downloads
Note : Choose file as per your os
Then place it anywhere on the os and pass that path as an argument. You can also set webdriver.chrome.driver environment variable if you don't want to pass the location every time.
FYI you could also encounter this issue if you did not unzip the chromedriver before adding it to your PATH.

Python script works fine on Linux, on Windows, causes WindowsError: [Error 5] Access is denied

I have a simple python script that works fine on Linux, I moved it to a Windows machine and when I attempt to run it, I get the following exception message:
Traceback (most recent call last):
File "C:\path\to\my\script.py", line 57, in <module>
retcode = subprocess.call(command)
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
Here is the snippet of code that throws the exception:
print 'command is:',command
retcode = subprocess.call(command)
The console out put is as follows:
command is: ['c:\python27', 'C:\path\to\script.py', '--mode=2', '--check-temp=false', '--all-seasons=true', '--added=1', '--max-temp=2000', '--source=2', '--loc=XYZ']
Unhandled exception while debugging...
Anyone knows how to fix this?
I am running python v2.7.3 on Windows XP Professional
According to the documentation, the first item in the argument sequence (in this case, the first element of command) is interpreted as the program to execute.
Looking at the first element of command, it would appear that you're trying to execute a directory. Windows (somewhat non-intuitively) returns an access denied error whenever you try to read from a directory as if it were a file, and the same thing happens if you try to execute one.
Instead of c:\python27 you probably want c:\python27\bin\python.exe or something similar. At any rate, you need to be pointing at the executable, not at the directory. I'm not sure why this works for you on Linux.
Your program doesn't have access to the file... check permissions on the file you're trying to access, then go from there... (ie, either elevate the Python interpreter's permissions, or reduce access required to said resource) - either way - tread carefully.

Categories

Resources