Automatic update of Python module

Preface

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.

Device ①

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)

Device ②

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

Device ③

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.

How to find the corresponding chromedriver-binary version

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

Automatic update of Python module
python chromedriver automatic update
Python update (2.6-> 2.7)
Introduction of Python
Automatic collection of stock prices using python
Python module import
Pass the path of the imported python module
Automatic update method with python Pyinstaller exe
Make a relation diagram of Python module
Clogged with python update of GCP console ①
Basics of Python ①
Basics of python ①
Check the path of the Python imported module
Copy of python
Python collections module
Introduction of Python
Clogged with python update of GCP console ② (Solution)
[Python] [chardet] Automatic detection of character code of file
[Python of Hikari-] Chapter 08-04 Module (Installation of external library)
Character encoding when using csv module of python 2.7.3
[Python] A rough understanding of the logging module
Try using the collections module (ChainMap) of python3
At the time of python update on ubuntu
[Python] Class type and usage of datetime module
Python / Automatic low wrench unfitting of experimental data
Get the update date of the Python memo file.
[Python] Operation of enumerate
Python module (Python learning memo ④)
python memorandum (sequential update)
Create a Python module
Unification of Python environment
Copy of python preferences
Basics of Python scraping basics
python original module import
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
[python] Get the list of classes defined in the module
Let's use the Python version of the Confluence API module.
The story of automatic language conversion of TypeScript / JavaScript / Python
Sample of getting module name and class name in Python
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
Sort Python module imports alphabetically
How Python module import works
Towards the retirement of Python2
Summary of python file operations
Summary of Python3 list operations
Python --Quick start of logging
Recommendation of binpacking library of python
[Python] ModuleNotFoundError: No module named'urlparse'
Run automatic jobs in python
[python] Value of function object (?)