Using setuptools, how can I download external data

2019-07-23 08:17发布

问题:

I'd like to create some ridiculously-easy-to-use pip packages for loading common machine-learning datasets in Python. (Yes, some stuff already exists, but I want it to be even simpler.)

What I'd like to achieve is this:

  • User runs pip install dataset
  • pip downloads the dataset, say via wget http://mydata.com/data.tar.gz. Note that the data does not reside in the python package itself, but is downloaded from somewhere else.
  • pip extracts the data from this file and puts it in the directory that the package is installed in. (This isn't ideal, but the datasets are pretty small, so let's assume storing the data here isn't a big deal.)
  • Later, when the user imports my module, the module automatically loads the data from the specific location.

This question is about bullets 2 and 3. Is there a way to do this with setuptools?

回答1:

As alluded to by Kevin, Python package installs should be completely reproducible, and any potential external-download issues should be pushed to runtime. This therefore shouldn't be handled with setuptools.

Instead, to avoid burdening the user, consider downloading the data in a lazy way, upon load. Example:

def download_data(url='http://...'):
    # Download; extract data to disk.
    # Raise an exception if the link is bad, or we can't connect, etc.

def load_data():
    if not os.path.exists(DATA_DIR):
        download_data()
    data = read_data_from_disk(DATA_DIR)
    return data

We could then describe download_data in the docs, but the majority of users would never need to bother with it. This is somewhat similar to the behavior in the imageio module with respect to downloading necessary decoders at runtime, rather than making the user manage the external downloads themselves.



回答2:

Python package installation states that it should never execute Python code in order to install Python packages. This means that you may not be able to download stuff during the installation process.

If you want to download some additional data, do it after you install the package , for example when you import your package you could download this data and cache it somewhere in order not to download it at every new import.



回答3:

Note that the data does not reside in the python package itself, but is downloaded from somewhere else.

Please do not do this.

The whole point of Python packaging is to provide a completely deterministic, repeatable, and reusable means of installing exactly the same thing every time. Your proposal has the following problems at a minimum:

  • The end user might download your package on computer A, stick it on a thumb drive, and then install it on computer B which does not have internet.
  • The data on the web might change, meaning that two people who install the same exact package get different results.
  • The website that provides the data might cease to exist or unwisely change the URL, meaning people who still have the package won't be able to use it.
  • The user could be behind an internet filter, and you might get a useless "this page is blocked" HTML file instead of the dataset you were expecting.

Instead, you should either include your data with the package (using the package_data or data_files arguments to setup()), or provide a separate top-level function in your Python code to download the data manually when the user is ready to do so.