如何设置python中本地模块的路径以在CircleCI中识别?

我正在构建一个 python 模块。为了定义它的路径,一个.pth文件被定义如下:


# creation of the virtual environment

python -v venv env


# activation of the newly creation virtual environment

source env/bin/activate

为了设置我的模块的路径(我的模块位于 packages/regression_model/regression_model),我创建了这个.pth文件 env/lib/python3.7/site-packages/regression_model.pth,其中包含:


# env/lib/python3.7/site-packages/regression_model.pth

../../../../packages/regression_model

现在,在我的项目中的任何地方,我都可以regression_model通过以下命令导入我的模块:


import regression_model

实际上我的目标是使用CircleCI来部署我的项目。


CircleCI配置如下:


version: 2

jobs:

  test_regression_model:

    working_directory: ~/project

    docker:

      - image: circleci/python:3.7.6

        environment: # environment variables for primary container

          PYTHONPATH: ~/project/packages/regression_model:~/project/packages/ml_api

    steps:

      - checkout

      - run:

          name: Runnning tests

          command: |

            virtualenv venv

            . venv/bin/activate

            pip install --upgrade pip

            pip install -r packages/regression_model/requirements.txt

            chmod +x ./scripts/fetch_kaggle_dataset.sh

            ./scripts/fetch_kaggle_dataset.sh

            python packages/regression_model/regression_model/train_pipeline.py

            py.test -vv packages/regression_model/tests


workflows:

  version: 2

  test-all:

    jobs:

      - test_regression_model

我面临的问题是 CircleCI 表明我的模块无法导入


Traceback (most recent call last):

  File "packages/regression_model/regression_model/train_pipeline.py", line 4, in <module>

    from regression_model import pipeline

ModuleNotFoundError: No module named 'regression_model'

为了解决这个问题,regression_model必须完全按照在本地完成的方式定义该模块的路径。那么问题来了:如何在 CircleCI 中定义路径?


我试图通过使用环境变量来做到这一点,PYTHONPATH但没有成功。


有什么建议么?


浮云间
浏览 65回答 1
1回答

幕布斯7119047

我找到了解决方案。与在我的本地机器上手动完成的操作类似,我只定义了 2 个命令行来在 CircleCI 中完成它:echo "../../../../packages/regression_model" >> env/lib/python3.7/site-packages/extra.pthecho "../../../../packages/ml_api" >> env/lib/python3.7/site-packages/extra.pth在完整的 yml 文件下方,以防万一它可以帮助其他人。version: 2jobs:&nbsp; test_regression_model:&nbsp; &nbsp; working_directory: ~/project&nbsp; &nbsp; docker:&nbsp; &nbsp; &nbsp; - image: circleci/python:3.7.6&nbsp; &nbsp; steps:&nbsp; &nbsp; &nbsp; - checkout&nbsp; &nbsp; &nbsp; - run:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: Runnning tests&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; command: |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; virtualenv env&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; . env/bin/activate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pip install --upgrade pip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pip install -r packages/regression_model/requirements.txt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "../../../../packages/regression_model" >> env/lib/python3.7/site-packages/extra.pth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "../../../../packages/ml_api" >> env/lib/python3.7/site-packages/extra.pth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chmod +x ./scripts/fetch_kaggle_dataset.sh&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ./scripts/fetch_kaggle_dataset.sh&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sudo apt-get install unzip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unzip&nbsp; packages/regression_model/regression_model/datasets/house-prices-advanced-regression-techniques.zip -d packages/regression_model/regression_model/datasets/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; python packages/regression_model/regression_model/train_pipeline.py&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; py.test -vv packages/regression_model/testsworkflows:&nbsp; version: 2&nbsp; test-all:&nbsp; &nbsp; jobs:&nbsp; &nbsp; &nbsp; - test_regression_model
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python