How to fix python module import errors in jenkins
<p>I am having module import errors in Jenkins despite setting my path and python path based on questions here on stack overflow</p>

<p>I have tried this</p>

<p><a href="http://stackoverflow.com/questions/12995809/jenkins-putting-my-python-module-on-the-pythonpath?rq=1">Jenkins: putting my Python module on the PYTHONPATH</a></p>

<p>and this</p>

<p><a href="http://stackoverflow.com/questions/28532467/python-module-import-failure-in-jenkins?rq=1">Python module import failure in Jenkins</a></p>

<p>This same command runs on my local machine without any import issues but fails on Jenkins</p>

<h3>The command</h3>

<pre><code>#!/bin/bash

export PYTHONPATH=$WORKSPACE:$PYTHONPATH
export PATH=$WORKSPACE:$PATH
export DJANGO_SETTINGS_MODULE=myapp.settings.test

echo "Working directory: "
pwd

echo "path: "
echo $PATH

echo "Python path: "
echo $PYTHONPATH


/home/adminuser/.virtualenvs/myapp/bin/python myapp/manage.py jenkins --project-apps-tests  --enable-coverage --settings=myapp.settings.test
</code></pre>

<h3>The build error</h3>

<pre><code>Working directory: 
/var/lib/jenkins/jobs/myapp_QA_TESTS/workspace
path: 
/var/lib/jenkins/jobs/myapp_QA_TESTS/workspace/myapp/apps/:/var/lib/jenkins/jobs/myapp_QA_TESTS/workspace:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Python path: 
/var/lib/jenkins/jobs/myapp_QA_TESTS/workspace/myapp/apps/:/home/adminuser/.virtualenvs/myapp/bin:/var/lib/jenkins/jobs/myapp_QA_TESTS/workspace:

-------- USING TEST SETTINGS ------

Traceback (most recent call last):
 ......
    File "/var/lib/jenkins/jobs/myapp_QA_TESTS/workspace/myapp/apps/accounts/models.py", line 18, in &lt;module&gt;
        from apps.registration.tokens.token import GenerateToken
    ImportError: No module named registration.tokens.token
</code></pre>

<h3>My file structure</h3>

<p>overview</p>

<pre><code>myapp/
âââ apps
âÂ Â  âââ __init__.py
âÂ Â  âââ accounts
âÂ Â  âââ registration
âââ myapp
âÂ Â  âââ __init__.py
âÂ Â  âââ celery.py
âÂ Â  âââ settings
âÂ Â  âââ urls.py
âÂ Â  âââ wsgi.py
âââ manage.py
</code></pre>

<p>view into the module directories</p>

<pre><code>myapp/apps/registration/tokens
âââ __init__.py
âââ token.py

myapp/apps/accounts/
âââ __init__.py
âââ models.py
</code></pre>

<p>I have tried even appending the workspace directory and the virtualenv path to both PATH and PYTHONPATH, i also even added the module directory to the PATH and PYTHONPATH</p>

<p>I get the same error when i run the command on the server itself. Could this be caused by the fact that my virtualenv was created by admin user, but now Jenkins is trying to use it, But all packages load</p>

<p>How do I fix this import error, any assistance is appreciated</p>

<p>So finally figured it out</p>

<p>You need to create the virtual env during the test so this is the final command that works</p>

<pre><code>#!/bin/bash


export WORKSPACE=`pwd`

# Create/Activate virtualenv
virtualenv testenv -p /usr/bin/python3

source testenv/bin/activate

# Install requirements
pip install -r requirements/test.txt 


# Run them tests
python myapp/manage.py jenkins --project-apps-tests  --enable-coverage --settings=myapp.settings.test
</code></pre>

<p>hope this helps someone who gets stuck like i did</p>

python django jenkins