Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Travis CI to install Python dependencies declared in tests_require?

I have Python package with setup.py. It has regular dependencies declared in install_requires and development dependencies declared in tests_require, e.g. flake8.

I thought pip install -e . or running python setup.py test will also install my development dependencies and they'll be available. However, apparently they're not and I struggle to setup my Travis CI build right.

install:
  - "pip install -e ."
script:
  - "python setup.py test"
  - "flake8"

Build configured as above will fail, because flake8 will not be found as a valid command. I also tried to invoke flake8 from inside of the python setup.py test command (via subprocess), but also without success.

Also I hate the fact that flake8 can't be easily made integral part of the python setup.py test command, but that's another story.

like image 447
Honza Javorek Avatar asked Jan 27 '16 20:01

Honza Javorek


People also ask

How do I download Python packages with dependencies?

Download Dependencies OnlyUse the pipdeptree utility to gather a list of all dependencies, create a requirements. txt file listing all the dependencies, and then download them with the pip download command. Get the list of dependencies for a package from the setup.py file.

Does pip install dependencies of dependencies?

Pip will not flag dependency conflicts. As a result, it will happily install multiple versions of a dependency into your project, which will likely result in errors. One way to avoid dependency conflicts is to use an alternative Python package manager, like conda, poetry or ActiveState's State Tool.

How do I get PIP in Python?

Step 1: Download the get-pip.py (https://bootstrap.pypa.io/get-pip.py) file and store it in the same directory as python is installed. Step 2: Change the current path of the directory in the command line to the path of the directory where the above file exists. Step 4: Now wait through the installation process. Voila!


2 Answers

The more direct answer is that pip install will not install tests_require, intentionally separating runtime requirements from test requirements. python setup.py test creates a virtualenv-like environment to run the tests in, un-doing this afterwards. flake8 is thus unavailable once it is done.

Flake8 has setuptools integration and also integrates with pytest if you use that. pytest itself also integrates with setuptools.

like image 54
Sam Brightman Avatar answered Nov 10 '22 09:11

Sam Brightman


I prefer to keep most of the configuration in tox.ini and rely on it to install and run what is to be run. For testing I use pytest (the solution can be modified to use other testing frameworks easily).

Following files are used:

  • tox.ini: automates the test
  • .travis.yml: instructions for Travis
  • setup.py: installation script to install the package to test
  • test_requirements.txt: list of requirements for testing

tox.ini

[tox]
envlist = py{26,27,33,34}

[testenv]
commands =
    py.test -sv tests []
deps =
    -rtest-requirements.txt

.travis.yml

sudo: false
language: python
python:
    - 2.6
    - 2.7
    - 3.3
    - 3.4
install:
    - pip install tox-travis
script:
 - tox

test_requirements.txt

Just ordinary requirements file whith what ever you need in there (e.g. flake8, pytest and other dependencies)

You may see sample at https://github.com/vlcinsky/awslogs/tree/pbr-setup.py

The fact it uses there pbr, coverage and coverall is not relevant to my answer (it works with or without pbr).

like image 33
Jan Vlcinsky Avatar answered Nov 10 '22 11:11

Jan Vlcinsky