Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a binary with a python module, is it good practice to do this?

I am trying to write my first python module that can be easily distributed and installed on different machines (hopefully by just cloning the repository and running setup.py). The problem is that my module has a dependency on a small binary file; I am using the Selenium module as one of my dependencies and it needs a webdriver for chrome (http://chromedriver.storage.googleapis.com/index.html?path=2.22/).

What I would like to do is to include the different binaries in my repo, have python determine the system platform, choose the correct binary, add it to the system path, and then install the module as normal.

Is this easy to do? And more importantly, is this actually good practice or is there a better alternative? It seems like a very annoying thing to have to separately download the binary file and add it to the system path for each machine I want to run my module on but I would rather not go against convention.

like image 952
Mark C. Avatar asked Oct 30 '22 00:10

Mark C.


1 Answers

Based on ivan_pozdeev and dmitryro's comments, this is the solution I have come up with at the moment:

Within my project directory is a folder bin/ with the subfolders linux32/, linux64/, mac32/, win32/. Inside each of those subfolders is the appropriate executable (chromedriver / cromedriver.exe).

I have added the following lines to my setup.py script:

import platform

# Determine the correct platform for the webdriver
system = platform.system()
arch, _ = platform.architecture()
if system == 'Linux':
    if arch == '64bit':
        webdriver = 'bin/linux64/chromedriver'
    else:
        webdriver = 'bin/linux32/chromedriver'
if system == 'Windows':
    webdriver = 'bin/win32/chromedriver.exe'
if system == 'Darwin':
    webdriver = 'bin/mac32/chromedriver'

Then, with the call to the setup() function, I simply add the keyword argument

scripts=[webdriver],

After some brief testing, this seems to copy the correct executable to the system's path and installs the python module as normal.

like image 66
Mark C. Avatar answered Nov 15 '22 06:11

Mark C.