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.

Sylhare
  • 5,907
  • 8
  • 64
  • 80
dwanderson
  • 2,775
  • 2
  • 25
  • 40
  • Try adding a print or log of sys.path if you can. And see how travis vs local path differ. – JL Peyret Mar 13 '16 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](https://travis-ci.org/anderson-dan-w/artificial-intelligence/jobs/115689715) and [github](https://github.com/anderson-dan-w/artificial-intelligence). 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 '16 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 '16 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 '16 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 from ai import a_star ImportError: No module named 'ai' """ – JL Peyret Mar 13 '16 at 17:04
  • FNow i am guessing that you're deep in the test hierarchy in that import. But the local execution context somehow knows that the path to ai belongs somewhere on top, say artificial-intelligence/ai.py. Maybe from where you are executing the nosetest run. Travis however lacks that info and herein lies your pbm because your test file is not in the same dir. – JL Peyret Mar 13 '16 at 17:09
  • Ok, yeah, I tried setting up a sample git repo with the most basic test, and things worked, so I'm missing something. let me try to track things down and figure out what the difference is. First thing I noticed was that I didn't have a "src" directory, meaning, all my python files were at the top-level along with, eg, requirements.txt and .travis.yml - I'm moving them down a level to see if that works better. – dwanderson Mar 13 '16 at 17:12
  • @JLPeyret Now that a) the tests work and b) I've fully understood your comment, I realize my mistake: `ai` is what I chose to call the directory I cloned from github, but that's not what TravisCI is going to call it (obviously, it was arbitrary). And I had everything at the top level, so of course it was like "what's this `ai` directory you speak of?". Now that I added a "src" directory called `ai` inside the repo. everything passes. In an effort to be fair - if you elevate that comment to an answer (and maybe include some of what I said here to round it out), I'll accept it. Thanks! – dwanderson Mar 13 '16 at 17:18
  • Ah, ah. That is interesting with the src & the repo. I just used travis once and it passed right away, but good to know about this sorta thing. I'll do the answer bit later, when I am not on a tablet. Take care. – JL Peyret Mar 13 '16 at 17:23
  • @dwanderson I solved a similar situation with a *setup.py* file config. If you need, you can see here the example into repository https://github.com/nbortolotti/tensorflow-code-experiences/. let me know if work for you. – Nicolas Bortolotti Sep 29 '17 at 07:50
  • Can you share your `travis.yml`? – MrName Jan 13 '18 at 18:58

1 Answers1

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"
Sylhare
  • 5,907
  • 8
  • 64
  • 80