Jenkins: putting my Python module on the PYTHONPATH

前端 未结 3 1948
终归单人心
终归单人心 2020-12-21 01:51

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/co         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 02:14

    • 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
    

提交回复
热议问题