Pass additional options to pip when installing model (resolves #1456)

Treat all additional arguments passed to the download command as pip options to allow user to customise the command. For example:

python -m spacy download en --user
This commit is contained in:
ines 2018-05-20 15:55:07 +02:00
parent 8bcf5e9a7e
commit 46662590f2

View File

@ -16,22 +16,24 @@ from .. import about
@plac.annotations( @plac.annotations(
model=("model to download, shortcut or name)", "positional", None, str), model=("model to download, shortcut or name)", "positional", None, str),
direct=("force direct download. Needs model name with version and won't " direct=("force direct download. Needs model name with version and won't "
"perform compatibility check", "flag", "d", bool)) "perform compatibility check", "flag", "d", bool),
def download(model, direct=False): pip_args=("additional arguments to be passed to `pip install` when "
"installing the model"))
def download(model, direct=False, *pip_args):
""" """
Download compatible model from default download path using pip. Model Download compatible model from default download path using pip. Model
can be shortcut, model name or, if --direct flag is set, full model name can be shortcut, model name or, if --direct flag is set, full model name
with version. with version.
""" """
if direct: if direct:
dl = download_model('{m}/{m}.tar.gz#egg={m}'.format(m=model)) dl = download_model('{m}/{m}.tar.gz#egg={m}'.format(m=model), pip_args)
else: else:
shortcuts = get_json(about.__shortcuts__, "available shortcuts") shortcuts = get_json(about.__shortcuts__, "available shortcuts")
model_name = shortcuts.get(model, model) model_name = shortcuts.get(model, model)
compatibility = get_compatibility() compatibility = get_compatibility()
version = get_version(model_name, compatibility) version = get_version(model_name, compatibility)
dl = download_model('{m}-{v}/{m}-{v}.tar.gz#egg={m}=={v}' dl = download_model('{m}-{v}/{m}-{v}.tar.gz#egg={m}=={v}'
.format(m=model_name, v=version)) .format(m=model_name, v=version), pip_args)
if dl != 0: # if download subprocess doesn't return 0, exit if dl != 0: # if download subprocess doesn't return 0, exit
sys.exit(dl) sys.exit(dl)
try: try:
@ -75,8 +77,10 @@ def get_version(model, comp):
return comp[model][0] return comp[model][0]
def download_model(filename): def download_model(filename, user_pip_args=None):
download_url = about.__download_url__ + '/' + filename download_url = about.__download_url__ + '/' + filename
return subprocess.call( pip_args = ['--no-cache-dir', '--no-deps']
[sys.executable, '-m', 'pip', 'install', '--no-cache-dir', '--no-deps', if user_pip_args:
download_url], env=os.environ.copy()) pip_args.extend(user_pip_args)
cmd = [sys.executable, '-m', 'pip', 'install'] + pip_args + [download_url]
return subprocess.call(cmd, env=os.environ.copy())