1

I am having module import errors in Jenkins despite setting my path and python path based on questions here on stack overflow

I have tried this

Jenkins: putting my Python module on the PYTHONPATH

and this

Python module import failure in Jenkins

This same command runs on my local machine without any import issues but fails on Jenkins

The command

#!/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

The build error

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 <module>
        from apps.registration.tokens.token import GenerateToken
    ImportError: No module named registration.tokens.token

My file structure

overview

myapp/
├── apps
│   ├── __init__.py
│   ├── accounts
│   ├── registration
├── myapp
│   ├── __init__.py
│   ├── celery.py
│   ├── settings
│   ├── urls.py
│   └── wsgi.py
├── manage.py

view into the module directories

myapp/apps/registration/tokens
├── __init__.py
└── token.py

myapp/apps/accounts/
├── __init__.py
├── models.py

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

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

How do I fix this import error, any assistance is appreciated

Community
  • 1
  • 1
Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41

1 Answers1

1

So finally figured it out

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

#!/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

hope this helps someone who gets stuck like i did

Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41