5

I am using Jenkins to test a Python module nodepy that I develop. However, I get errors like the following:

File "/var/lib/jenkins/jobs/NodePy/workspace/convergence.py", line 6, in workspace.convergence
Failed example:
    from nodepy import rk, convergence, ivp
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/doctest.py", line 1289, in __run
        compileflags, 1) in test.globs
      File "<doctest workspace.convergence[0]>", line 1, in <module>
        from nodepy import rk, convergence, ivp
    ImportError: No module named nodepy

How do I set the PYTHONPATH in Jenkins (so that my module is importable)?

I'm not even sure which directory I should add. It seems that Jenkins puts things in jenkins/jobs/nodepy/workspace/, so the directory doesn't even have the right name for python to find it.

EDIT:

The python module nodepy is a git repository that I have configured my jenkins job to watch and checkout.

dnozay
  • 23,846
  • 6
  • 82
  • 104
David Ketcheson
  • 4,035
  • 2
  • 30
  • 25

4 Answers4

11
  • either add it to your parameterized job params list (and fill it in or provide a default)
  • or if you have admin access to jenkins, you may add it to the system variables there (go to Manage Jenkins, then System Configuration)
  • or use sys.path.append within your script.

e.g.

import sys
import os
# jenkins exposes the workspace directory through env.
sys.path.append(os.environ['WORKSPACE'])
import nodepy

or, in your jenkins build configuration, configure the build step with (if it is a shell script one):

export PYTHONPATH=$WORKSPACE:$PYTHONPATH

Package issue

/var/lib/jenkins/jobs/NodePy/workspace/convergence.py

this is a problem, because there is no nodepy directory. so even if you put the correct syspath, your package structure will not be right. what you can do is change how your workspace looks like, from:

/var/lib/jenkins/jobs/NodePy/workspace/convergence.py
/var/lib/jenkins/jobs/NodePy/workspace/ivp.py
/var/lib/jenkins/jobs/NodePy/workspace/rk.py

to

/var/lib/jenkins/jobs/NodePy/workspace/nodepy/__init__.py
/var/lib/jenkins/jobs/NodePy/workspace/nodepy/convergence.py
/var/lib/jenkins/jobs/NodePy/workspace/nodepy/ivp.py
/var/lib/jenkins/jobs/NodePy/workspace/nodepy/rk.py

EDIT: Extracting files in correct subdirectory

Your workspace is going to be

/var/lib/jenkins/jobs/NodePy/workspace/

You don't need to change the workspace directory, it is keyed from your job name (NodePy) and your jenkins configuration, you just need to create the nodepy directory in the workspace, and have your files go there. You can either change your jenkins job configuration and have it checkout the git repo nodepy in the correct subdirectory or you can move the files yourself:

mkdir .nodepy
# .nodepy is hidden, * doesn't capture hidden files.
mv * .nodepy
mv .nodepy nodepy
Community
  • 1
  • 1
dnozay
  • 23,846
  • 6
  • 82
  • 104
  • Since you get the nodepy package files through jenkinks SCM plugin, rather than manually, you need to set the correct option. I see you've found that option. – dnozay Oct 22 '12 at 18:10
  • Here I am looking at a 3 year old answer because I could not find anything else on Google so I wanted to comment that `sys.path.append(os.environ['WORKSPACE'] + /path/to/file)` is the one that worked. `export pythonpath` works on local but did nothing for me in Jenkins. Please enlighten if anyone has a more updated solution – otgw Dec 03 '15 at 15:42
  • for jenkins admin, add what to my system variables – Harper Koo Oct 12 '16 at 07:57
  • Thank you, your solution really helped me today! Initially I didn't have "__init__.py" in my directory since I am on Python 3.7. Once I added it back per your suggestion. Jenkins was able to find the module. – Tracy Xia Jan 14 '21 at 18:53
0

You should install nodepy as part of your testing procedure. Jenkins and other test-driven development tools need to be able to update your installed code whenever it changes (as well as verify that your install procedure is working correctly), and the right way to do this is with either a .pth link or by installing it after checkout. If you want to simply use the checked-out repository from github, you can insert the following shell command from the workspace directory:

pip install -e ./nodepy
Aron Ahmadia
  • 2,267
  • 2
  • 19
  • 22
0

It turns out all I needed to do was follow the answer to this question in order to have Jenkins clone my repo into a subdirectory workspace/nodepy/. Since Jenkins runs the build in workspace, it then finds the package.

When I first did this, it somehow created an infinite recursion of nodepy/nodepy/nodepy/... directories. After I cleared out the workspace, this problem went away.

Community
  • 1
  • 1
David Ketcheson
  • 4,035
  • 2
  • 30
  • 25
0

in python3 installation process, make sure that the installation is for all the users

Customize installation -> Next -> Install for all users

Damaor
  • 39
  • 3