Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install using pip a development version of a python package but with stable dependencies

Tags:

python

pip

Background

The pip install command installs by default the newest stable version of a python package (stable versions as specified by PEP426)

The flag --pre for the pip install command tells pip also consider release candidates and develompent versions of python packages. As far as I understand, though, pip install --pre packageA it will install a dev version of packageA, but also dev version of all its dependencies.

The question is:

Is it possible to use pip to install a development version of a package but stable versions of all its dependencies?

Attempted solutions

One thing I have tried is to install the stable version of the package (with stable dependencies), and then reinstall the dev version without dependencies: pip install packageA pip install --pre --no-deps --upgrade --force-reinstall packageA The problem, though, is that if the development version of packageA adds a new dependency, it will not be installed.

I am missing anything? Thanks!

like image 822
mgab Avatar asked Nov 07 '22 07:11

mgab


1 Answers

I write a script to do this(pip_install_dev_and_stable_of_dependencies.py):

#!/usr/bin/env python
import os
import sys


def get_installed_packages():
    with os.popen('pip freeze') as f:
        ss = f.read().strip().split('\n')
    return set(i.split('=')[0].strip().lower() for i in ss)


def install_pre_with_its_dependencies_stable(package):
    already_installed_packages = get_installed_packages()
    os.system('pip install --pre ' + package)
    dependencies = ' '.join(
        p for p in get_installed_packages()
        if p not in already_installed_packages | set([package])
    )
    os.system('pip uninstall -y ' + dependencies)
    os.system('pip install ' + dependencies)


def main():
    for p in sys.argv[1:]:
        install_pre_with_its_dependencies_stable(p)


if __name__ == '__main__':
    main()

Usage:

(venv)$ chmod +x pip_install_dev_and_stable_of_dependencies.py
(venv)$ ./pip_install_dev_and_stable_of_dependencies.py pandas

This script do the following things:

# Step 1. get the packages that already installed
pip freeze
# Step 2. install the dev version of packageA
pip install --pre packageA
# Step 3. pick out the dependencies (compare with Step 1)
pip freeze
# Step 4. uninstall all the dependencies of packageA
pip uninstall depend1 depend2 ...
# Step 5. install the stable version of dependencies
pip install depend1 depend2 ...
like image 160
Waket Zheng Avatar answered Nov 14 '22 22:11

Waket Zheng