我正在构建一个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
来实现,但是没有成功。
有什么建议吗?
我找到了解决方案。与在本地计算机上手动完成的操作类似,我只定义了2条命令行即可在CircleCI中完成:
echo "../../../../packages/regression_model" >> env/lib/python3.7/site-packages/extra.pth
echo "../../../../packages/ml_api" >> env/lib/python3.7/site-packages/extra.pth
并且在完整的yml文件下面,以防它可能对其他人有所帮助。
version: 2
jobs:
test_regression_model:
working_directory: ~/project
docker:
- image: circleci/python:3.7.6
steps:
- checkout
- run:
name: Runnning tests
command: |
virtualenv env
. env/bin/activate
pip install --upgrade pip
pip install -r packages/regression_model/requirements.txt
echo "../../../../packages/regression_model" >> env/lib/python3.7/site-packages/extra.pth
echo "../../../../packages/ml_api" >> env/lib/python3.7/site-packages/extra.pth
chmod +x ./scripts/fetch_kaggle_dataset.sh
./scripts/fetch_kaggle_dataset.sh
sudo apt-get install unzip
unzip packages/regression_model/regression_model/datasets/house-prices-advanced-regression-techniques.zip -d packages/regression_model/regression_model/datasets/
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