If you leave the Python module as it was delivered, the delivery may get stuck.
This time, we will use a module called chromedriver-binary as an example. The role of chromedriver-binary is to allow you to operate the chrome browser via Python. chromedriver-binary must be installed for chrome browser. The chrome browser will update without permission, so you have to do nothing Eventually, chromedriver-binary will not be supported and deliveries will stop. You can use the automatic update of the module in such a place.
Previously, I was able to use the method of diverting the code of \ _ \ _ init \ _ \ _. Py of pip. It has become unusable since the new version of pip. If you look at the Pycharm console etc., you will find guidance on the new method.
Translated "Run the pip command directly" I think it was a story like that.
If you can easily summarize the mechanism It means that you should execute "pip install ***" around the subprocess. It has been confirmed that it can be used this way even if you are using venv.
When the above is summarized in a function, it looks like this.
installer.py
import sys
import subprocess
def install_module(module: str, ver: str = None):
"""
Install the specified module (version can be specified)
:param module:Module name
:param ver:Module version
:return:
"""
#Add options if version is specified
if ver is not None:
ver = f"=={ver}"
else:
ver = ""
#Do the installation work
try:
command = f"pip install {module}{ver}"
subprocess.call(command, shell=True)
except Exception:
sys.exit(-1)
Put the above code in the program and you're done! !! It's not as simple as ... Since the heading is "Mechanism ①", there are still ② and ③.
Use it as follows.
sample_1.py
try:
driver = webdriver.Chrome()
except:
install_module("chromedriver-binary", "86.0.4240.22.0")
It will install the specified module when it flows to except ... The contents of the driver variable are still unset.
sample_2.py
try:
driver = webdriver.Chrome()
except:
install_module("chromedriver-binary", "86.0.4240.22.0")
driver = webdriver.Chrome()
If so, there seems to be no problem in the code, but ... driver = webdriver.Chrome () This code Which chromedriver-binary is it based on? -Is it the chromedriver-binary at the timing when import chromedriver-binary is declared? -Is it a newly installed chromedriver-binary? It depends on the implementation method, but as far as I have verified, it seems to work with the former.
You can reload it by using importlib.reload (module name) etc. There is also a way to restart the py file itself, The part that has nothing to do with the main part of the program becomes a lot of trouble. Restarting the py file will terminate the py file process once. If there is something in the system that waits for the end of the process There are many troublesome things such as starting the next process even though the py file process is not finished.
The simplest thing I thought about was to make the following batch.
run.bat
For checking the py module.py
py for main processing.py
If you use a batch file (1) If you wait for the end of processing and proceed to the next, wait for the end of the batch file before proceeding to the next. (2) The main process can be streamlined by making the module check and the main process separate py files. ③ When you install with .py for module confirmation You can keep the module up to date when you run the main processing .py
If you can practice gimmick ① and gimmick ②, you can use it for installing / updating modules. Unfortunately it's still not enough to use for updating the chromedriver-binary. As far as I've verified around 2018, chromedriver-binary doesn't work with the latest installed. You need to find out which version you want to install and give it as an argument.
You can see it by giving the major version of the chrome browser to the URL of ↓ and opening the URL https://chromedriver.storage.googleapis.com/LATEST_RELEASE_{chromeブラウザのメジャーバージョン} Example: https://chromedriver.storage.googleapis.com/LATEST_RELEASE_86
So how do you find out the version of the chrome browser? C:\Program Files (x86)\Google\Chrome\Application Or C:\Program Files\Google\Chrome\Application If you look in, you can find the folder with the version number. You can get the version number by cutting this out well.
All you need is a major version, so you only need to retrieve the major version, which is good with string manipulation. In summary, it looks like this.
installer.py
import sys
import re
import requests
import subprocess
def get_lastrelease_chromedriver():
"""
Get the latest chromedriver version for your chrome browser
:return:
"""
#Get the installed chrome browser version
try:
res = subprocess.Popen('dir /B /O-N "C:\Program Files (x86)\Google\Chrome\Application" | findstr "^[0-9].*\>"', stdout=subprocess.PIPE, shell=True).communicate()
target = str(res[0])
pattern = r'[0-9]+\.'
match = re.search(pattern, target)
if match is None:
# [For 32bit]
res = subprocess.Popen('dir /B /O-N "C:\Program Files\Google\Chrome\Application" | findstr "^[0-9].*\>"', stdout=subprocess.PIPE, shell=True).communicate()
if len(res) < 1:
raise Exception("chrome browser version check command failed")
target = str(res[0])
pattern = r'[0-9]+\.'
match = re.search(pattern, target)
if match is None:
sys.exit(-1)
chrome_ver = match.group(0).replace('.', '')
except:
return None
#Ask the web service for the chrome driver version
try:
api = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_{}"
ver = requests.get(api.format(chrome_ver)).text
except:
return None
return ver
If you incorporate it into the module confirmation .py, it will look like ↓
For module confirmation.py
try:
driver = webdriver.Chrome()
except:
ver = get_lastrelease_chromedriver()
install_module("chromedriver-binary", ver)
You should now be able to automatically update your chromedriver-binary.
Recommended Posts