I'm setting up Travis-CI for my project, and oddly, I can't import my project:
$ python tests/tests.py
Traceback (most recent call last):
File "tests/tests.py", line 11, in <module>
from my_module.lib.importer import build_module_list
ImportError: No module named my_module.lib.importer
In production, I just create a symlink like so:
sudo ln -s /usr/local/my_module /usr/lib/python2.7/dist-packages/my_module
But I don't know -- or want to know, really -- Travis-CI's folder structure.
This seems like a solved problem, but I'm new to Travis-CI. What's the best way to make this work, so my code is added as an importable module?
In complement of @Brian-Cain answer, you can also use
setuptools
instead ofdistutils
. As of writing,distutils
is being phased out, andsetuptools
is being used as a replacement, even thoughsetuptools
is not yet in standard library.For a quick tutorial on making a
setup.py
withsetuptools
: https://packaging.python.org/tutorials/distributing-packages/For a quick real example: https://github.com/pypa/sampleproject/blob/master/setup.py
To be quick, you can fix the problem more elegantly by adding the following to the
before_script
stage:The better way(but with a little more effort) is what Brian Cain has suggested, namely, write a
setup.py
file and addpip install .
to theinstall
stage.Or if you're using a makefile you can have command that does it as the following one.
It's more likely necessary to add
/home/travis/.local/lib/python2.7/site-packages/
toPYTHONPATH
inbefore_script
usingexport PYTHONPATH=$PYTHONPATH:/home/travis/.local/lib/python2.7/site-packages/
.This is certainly not optimal, but it worked. In my .travis.yml file, I added the following line to the
install
attribute:This basically finds the directory where Python is installed and then adds
my_module
as a symlink in there. Happy to hear a better answer, cause this one feels super fragile.Update: See the answer by @Brian Cain for a much better solution.
The answer is unequivocally to use
distutils
(and definitely notln
).B-b-but why? The complexity to do it the Right Way™ is so low! It even fits in a few lines:
From The Fine Manual -- just create a
setup.py
like this:Now you can do fantastic things like
python setup.py install
,python setup.py bdist_rpm
orpip install .
-- and not just in the Travis environment but for your project in general.