How to download available Windows binaries with pi

2020-03-26 06:34发布

I have to administrate two separate Python setups, one on a Linux Machine with internet connection, and one offline Windows machine. For a given set of required packages, I would like to download the necessary files on the Linux machine, transfer them to the Windows machine and install them there.

The following works as expected:

pip3 download virtualenv --platform win_amd64 -d py_packages_20180222/ --only-binary=:all:

The file virtualenv-15.1.0-py2.py3-none-any.whl is downloaded to the desired location, and ready for transfer. However, if pip doesn't find a binary file, it produces an error:

pip3 download ipython --platform win_amd64 -d py_packages_20180222/ --only-binary=:all:
 Could not find a version that satisfies the requirement simplegeneric>0.8 (from ipython) (from versions: )
No matching distribution found for simplegeneric>0.8 (from ipython)

This is the expected output due to the setting --only-binary=:all:, however it is not my desired output. In such a case, I would like pip to download the source code instead (with a warning) and continue. I'm essentially looking for the pip option --only-binary=:if_available:. Does this exist?

EDIT: No, it doesn't. Thanks to the accepted answer, I was able to find my solution in using the following bash script:

#!/usr/bin/env python3

import subprocess

pkges = ['virtualenv', 
         'ipython']

for pkg in pkges:
    cmd = ['pip3',
           'download',
           pkg,
           '--platform',
           'win_amd64',
           '-d py_packages_20180222/',
           '--only-binary=:all:']

    result = subprocess.run(cmd)
    if result.returncode != 0:
        print("No binary found for pkg. Downlaoding source code instead")
        cmdalt = ['pip3',
                  'download',
                  pkg,
                  '-d py_packages_20180222/']
        subprocess.run(cmdalt)

标签: python pip
1条回答
欢心
2楼-- · 2020-03-26 07:20

No, according to the pip documentation, such an option does not exist:

--only-binary <format_control>: Do not use source packages. Can be supplied multiple times, and each time adds to the existing value. Accepts either :all: to disable all source packages, :none: to empty the set, or one or more package names with commas between them. Packages without binary distributions will fail to install when this option is used on them.

You could achieve the desired effect with a conditional in a small script, maybe something like this (this is just a sketch and I'm not a Python expert, though):

#!/usr/bin/env python3

import subprocess

cmd = ['pip3',
       'download',
       'ipython',
       '--platform',
       'win_amd64',
       '-d py_packages_20180222/',
       '--only-binary=:all:']

result = subprocess.run(cmd)
if result.returncode != 0:
    print("oh noes")
    # put here what should happen if the download of the binaries fails
查看更多
登录 后发表回答