【问题标题】:How to explicitly add a project to the Python path in CircleCI 2.0?如何在 CircleCI 2.0 中将项目显式添加到 Python 路径?
【发布时间】:2018-09-08 15:29:59
【问题描述】:

我不确定如何在 CircleCI 2.0 中正确设置 PYTHONPATH 以允许构建运行。这是一个以前在 CircleCI 1.0 上成功构建的 Django 项目,所以我开始使用自动生成的 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 上构建时有效),我正在努力在文档中查找任何内容。

【问题讨论】:

  • 您的工作目录中有您的 GitHub 名称,但您的 python 路径没有。这是故意的吗?
  • 这就是我认为问题所在 - 因为它位于工作目录之上,是由 CircleCI 配置生成器自动生成的; Python 路径取自原始配置文件(并由自动生成器放置在此文件中)。不是故意的!
  • 我已经有一段时间没有做过任何 Django,但我会尝试将你的 PYTHONPATH 设置为 ${HOME}/mygithubname/myproject/myproject
  • 谢谢,我刚试过,但不幸的是同样的错误
  • 最后一个想法 - 可能删除第二个 myproject: ${HOME}/mygithubname/myproject 作为 PYTHONPATH,我隐约记得 django 在以后的版本中更改了嵌套 python 路径。

标签: python django pytest circleci circleci-2.0


【解决方案1】:

在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添加项目文件夹和文件夹,用tests添加文件夹

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2018-02-21
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多