如何在CircleCI 2.0中将项目显式添加到Python路径?

问题描述 投票:0回答:1

我不确定如何在CircleCI 2.0中正确设置PYTHONPATH以允许构建运行。这是一个以前在CircleCI 1.0上成功构建的Django项目,所以我开始使用auto生成的config.yml文件。

version: 2
  jobs:
    build:
      working_directory: ~/mygithubname/myproject
      parallelism: 1
      shell: /bin/bash --login

environment:
  CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
  CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
  DATABASE_URL: 'sqlite://:memory:'
  DJANGO_SETTINGS_MODULE: myproject.settings.test
  DEBUG: 0
  PYTHONPATH: ${HOME}/myproject/myproject

docker:
- image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37
  command: /sbin/init
steps:

- checkout

- run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS

- restore_cache:
    keys:
    # This branch if available
    - v1-dep-{{ .Branch }}-
    # Default branch if not
    - v1-dep-master-
    # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
    - v1-dep-
- run: pip install -r requirements/testing.txt

- save_cache:
    key: v1-dep-{{ .Branch }}-{{ epoch }}
    paths:
    # This is a broad list of cache paths to include many possible development environments
    # You can probably delete some of these entries
    - vendor/bundle
    - ~/virtualenvs
    - ~/.m2
    - ~/.ivy2
    - ~/.bundle
    - ~/.go_workspace
    - ~/.gradle
    - ~/.cache/bower

- run: pytest

- store_test_results:
    path: /tmp/circleci-test-results

- store_artifacts:
    path: /tmp/circleci-artifacts
- store_artifacts:
    path: /tmp/circleci-test-results

run: pytest命令在CircleCI中失败,错误说明pytest-django could not find a Django project (no manage.py file could be found). You must explicitly add your Django project to the Python path to have it picked up.我知道错误意味着什么但不确定如何在版本2中修复(它在版本1上构建时有效),并且我很难在文档中找到任何内容。

python django pytest circleci circleci-2.0
1个回答
0
投票

在circleci环境变量不能用于扩展你需要使用BASH_ENV https://circleci.com/docs/2.0/env-vars/#using-bash_env-to-set-environment-variables

- run: echo 'export PYTHONPATH="${PYTHONPATH}:${HOME}/myproject/folder_with_manage.py:${HOME}/myproject/folder_with_tests"' >> $BASH_ENV

或者手动设置正确的路径,使用manage.py和带有测试的文件夹添加项目文件夹和文件夹

environment:
  PYTHONPATH: /root/myproject/:/root/myproject/folder_with_manage.py/:/root/myproject/folder_with_tests/

要检查它是否正常工作,你可以做到

- run: echo $PYTHONPATH

要么

- run: python -c "import sys; print(sys.path)"

如果你使用没有bash的图像,不要忘记做https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-shell-command

source $BASH_ENV
# run tests
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.