Python docker 容器中的 cronjob 导入错误
Python import error with cronjob in a docker container
我有一个 python 脚本,在 docker 容器中每分钟 运行 使用 cronjob
这是我的 Dockerfile
FROM python:3.8.3 AS base
RUN apt-get update
RUN apt-get -y install software-properties-common cron vim
RUN apt-get update
RUN apt-get -y install python3-pip
RUN pip3 install pandas
RUN pip3 install sklearn
RUN pip3 install SQLAlchemy
RUN pip3 install ConfigParser
RUN pip3 install psycopg2
RUN pip3 install numpy
RUN pip3 install xgboost
RUN pip3 install xlrd
RUN pip3 install matplotlib
FROM base AS publish
WORKDIR /app
COPY . /app
COPY crontab /etc/cron.d/crontab
COPY main.py /app/main.py
RUN chmod 0644 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab
CMD ["cron", "-f"]
这是我的 crontab 文件
#run python script every minutes
* * * * * python /app/main.py > /proc/1/fd/1 2>/proc/1/fd/2
这是我的代码的开头:
import os
print("Code is running")
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import DataFrame
from pandas import concat
from calendar import monthrange
from datetime import date
import pickle
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, String, Integer, LargeBinary
from my_ml_library.regressor import Regressor
.......
当我从这个 docker 文件和 运行 构建图像时,脚本每分钟打印一次:
Code is running
Traceback (most recent call last):
File "/app/main.py", line 3, in
import numpy as np
ImportError: No module named numpy
另一方面,如果我 运行 我的代码在 docker 容器中而不使用 cron,它可以正常工作。我如何才能 运行 我的代码正确?
您需要在crontab文件中指定Python安装的绝对路径。
#run python script every minutes
* * * * * /usr/local/bin/python /app/main.py > /proc/1/fd/1 2>/proc/1/fd/2
您可以找到 Dockerfile here.
main.py
import numpy as np
a = np.arange(15).reshape(3, 5)
print(a)
我有一个 python 脚本,在 docker 容器中每分钟 运行 使用 cronjob 这是我的 Dockerfile
FROM python:3.8.3 AS base
RUN apt-get update
RUN apt-get -y install software-properties-common cron vim
RUN apt-get update
RUN apt-get -y install python3-pip
RUN pip3 install pandas
RUN pip3 install sklearn
RUN pip3 install SQLAlchemy
RUN pip3 install ConfigParser
RUN pip3 install psycopg2
RUN pip3 install numpy
RUN pip3 install xgboost
RUN pip3 install xlrd
RUN pip3 install matplotlib
FROM base AS publish
WORKDIR /app
COPY . /app
COPY crontab /etc/cron.d/crontab
COPY main.py /app/main.py
RUN chmod 0644 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab
CMD ["cron", "-f"]
这是我的 crontab 文件
#run python script every minutes
* * * * * python /app/main.py > /proc/1/fd/1 2>/proc/1/fd/2
这是我的代码的开头:
import os
print("Code is running")
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import DataFrame
from pandas import concat
from calendar import monthrange
from datetime import date
import pickle
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, String, Integer, LargeBinary
from my_ml_library.regressor import Regressor
.......
当我从这个 docker 文件和 运行 构建图像时,脚本每分钟打印一次:
Code is running Traceback (most recent call last): File "/app/main.py", line 3, in import numpy as np ImportError: No module named numpy
另一方面,如果我 运行 我的代码在 docker 容器中而不使用 cron,它可以正常工作。我如何才能 运行 我的代码正确?
您需要在crontab文件中指定Python安装的绝对路径。
#run python script every minutes
* * * * * /usr/local/bin/python /app/main.py > /proc/1/fd/1 2>/proc/1/fd/2
您可以找到 Dockerfile here.
main.py
import numpy as np
a = np.arange(15).reshape(3, 5)
print(a)