尝试在Docker中运行Python脚本时出现“ImportError:无法导入名称…”

2024-05-13 06:00:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python脚本,我想在Docker容器中运行,但它因错误而停止:

 File "main.py", line 1, in <module>
   from pushover import init, Client
ImportError: cannot import name 'init' from 'pushover' (/usr/local/lib/python3.8/dist-packages/pushover/__init__.py)

在PyCharm中,脚本运行良好。Dockerfile如下所示:

RUN apt-get update -y && apt-get install git python3 python3-pip -y; \ 
   git clone https://(path to my script) ; \
   pip3 install pushover
WORKDIR "/FNotify/"
CMD  python3 main.py; 

我在脚本中的导入如下所示:

from pushover import init, Client
import os
from time import sleep

Tags: installdockerfrompyimportgit脚本client
1条回答
网友
1楼 · 发布于 2024-05-13 06:00:29

你把wrong package搞混了&correct package,那么接下来应该安装什么:

pip3 install python-pushover

结果是:

# python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pushover import init
>>>

另外,python-pushover网页给出了与您相同的示例用法:

from pushover import init, Client

init("<token>")
Client("<user-key>").send_message("Hello!", title="Hello")

相关问题 更多 >