13

I'm trying to integrate TravisCI into my workflow, and realized I had some dependencies because of my old directory structure (not having self-contained, virtualenv-able git repos).

When I try to run nosetests locally, it runs the tests just fine; when TravisCI tries to run them, it fails, with an import error. Specifically, I have, as one of the lines in my test script:

from myproject import something

My directory structure is inside my git repo myproject is something like:

.travis.yml
requirements.txt
something.py
tests/
    test_something.py
  • I have tried getting this to fail locally (because then I'd understand the TravisCI issue, maybe), but cannot accomplish it.
  • I've tried running with regular python, and using a virtualenv which added nose to its requirements.txt, and the tests always pass locally.

I feel like I still haven't understood absolute-vs-relative imports, and I can't tell if that's coming in to play here, or if I'm just doing something obvious and dumb in my project.

Desired outcome: figure out why TravisCI is failing, and fix my repo accordingly, so that I can commit and have things build correctly, both locally and on TravisCI. If that requires more drastic changes like "you should have a setup.py that does blah-blah to the environment" or similar - please let me know. I'm new to this aspect of Python, and find the current documentation overwhelmingly unclear.

As an FYI, I found this question and adding --exe doesn't help, or seem to be the same issue.

11
  • Try adding a print or log of sys.path if you can. And see how travis vs local path differ.
    – JL Peyret
    Mar 13, 2016 at 16:12
  • Tried that, the results are the same - it runs locally just fine, and it fails on Travis, even though they have the same sys.path - if you're interested: travis output and github. So far, I'm just trying to get the a_star and decision_tree tests running, so should only focus on those.
    – dwanderson
    Mar 13, 2016 at 16:33
  • 1
    What does import ai. Print(ai._file_) say? Is ai your module? Is it a, possibly forgotten, pip-installed dependency in a requirements.txt?
    – JL Peyret
    Mar 13, 2016 at 16:50
  • Sorry, yeah, I was trying to not bog down the question with the particulars of my project; currently working on as simple an MCVE as I can. But, to answer: ai is my module, __file__ prints what I expect, namely, the directory inside my github repo, and is not anywhere in requirements.txt. Also, I have an empty $PYTHONPATH.
    – dwanderson
    Mar 13, 2016 at 16:54
  • You should post those clarifications, along with"""recent call last): File "/home/travis/build/anderson-dan-w/artificial-intelligence/tests/unit/test_a_star.py", line 6, in <module> from ai import a_star ImportError: No module named 'ai' """
    – JL Peyret
    Mar 13, 2016 at 17:04

1 Answer 1

8

I see there are no answer and I encountered the same issue, so I am posting here in hope to help somebody:

Solution 1

The quick fix for me was to add this line export PYTHONPATH=$PYTHONPATH:$(pwd) in the .travis.yml:

before_install:
  - "pip install -U pip"
  - "export PYTHONPATH=$PYTHONPATH:$(pwd)"

Solution 2

Having a setup.py which should be the default option as it is the most elegant, configured like:

from setuptools import setup, find_packages

setup(name='MyPythonProject',
      version='0.0.1',
      description='What it does',
      author='',
      author_email='',
      url='',
      packages=find_packages(),
     )

And then add this line in .travis.yml

before_install:
  - "pip install -U pip"
  - "python setup.py install"

Solution 3:

Changing the layout of the project to have the test folder under the application one (the one with your core python code) such as:

.travis.yml
requirements.txt
app
|_ tests
|   |_ test_application.py
|_ application.py

And running the test in travis with coverage and nosetest like:

script:
    - "nosetests --with-coverage --cover-package app"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.