gpt4 book ai didi

python - 如何修复 jenkins 中的 python 模块导入错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:14 24 4
gpt4 key购买 nike

尽管根据堆栈溢出问题设置了我的路径和 python 路径,但我在 Jenkins 中遇到了模块导入错误

我已经尝试过了

Jenkins: putting my Python module on the PYTHONPATH

还有这个

Python module import failure in Jenkins

同样的命令在我的本地计算机上运行,​​没有任何导入问题,但在 Jenkins 上失败

命令

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

构建错误

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

我的文件结构

概述

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

查看模块目录

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

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

我什至尝试将工作区目录和 virtualenv 路径附加到 PATH 和 PYTHONPATH,我什至还将模块目录添加到 PATH 和 PYTHONPATH

当我在服务器本身上运行命令时,我遇到了同样的错误。这是否是由于我的 virtualenv 是由管理员用户创建的,但现在 Jenkins 正在尝试使用它,但所有包都加载了

如何修复此导入错误,感谢您的帮助

最佳答案

终于明白了

您需要在测试期间创建虚拟环境,因此这是最终有效的命令

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

希望这可以帮助那些像我一样陷入困境的人

关于python - 如何修复 jenkins 中的 python 模块导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com