不确定我是否应该在Linux堆栈溢出时问这个问题,但是现在就这样了。
我是python的新手,我一直在努力让这个python脚本在aws机器上自动启动。我有两个需要安装的模块“discord.py”和“watson-cloud-developer”。 Pip3安装上述模块,没有错误。当试图运行运行运行python脚本的脚本的服务时(得知系统)我收到错误,告诉我没有安装discord模块,见下文。
● discordbot.service
Loaded: loaded (/etc/systemd/system/discordbot.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2019-03-03 17:16:00 UTC; 6s ago
Process: 30567 ExecStart=/usr/local/sbin/startbot.sh (code=exited, status=1/FAILURE)
Main PID: 30567 (code=exited, status=1/FAILURE)
Mar 03 17:16:00 ip-172-31-46-72 systemd[1]: Started discordbot.service.
Mar 03 17:16:00 ip-172-31-46-72 startbot.sh[30567]: Traceback (most recent call last):
Mar 03 17:16:00 ip-172-31-46-72 startbot.sh[30567]: File "/home/ubuntu/discordBot/main.py", line 1, in <module>
Mar 03 17:16:00 ip-172-31-46-72 startbot.sh[30567]: import discord
Mar 03 17:16:00 ip-172-31-46-72 startbot.sh[30567]: ModuleNotFoundError: No module named 'discord'
Mar 03 17:16:00 ip-172-31-46-72 systemd[1]: discordbot.service: Main process exited, code=exited, status=1/FAILURE
Mar 03 17:16:00 ip-172-31-46-72 systemd[1]: discordbot.service: Failed with result 'exit-code'.
Python3 proof that discord is installed
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.
>>> import discord
>>>
我非常乐意提供任何其他信息。
EDITS:
服务:
[Service]
ExecStart=/usr/local/sbin/startbot.sh
bash脚本:
#!/bin/bash
python3 /home/ubuntu/discordBot/main.py
python脚本:
import json
from watson_developer_cloud import VisualRecognitionV3
visual_recognition = VisualRecognitionV3(
"2018-03-19",
iam_apikey="{api-key}")
def ReturnWatsonResults(urlInput):
classes_result = visual_recognition.classify(url=urlInput).get_result()
imageResults = dict()
for images in classes_result['images'][0]['classifiers'][0]['classes']:
imageResults[images['class']]=str(images['score'] * 100)
return imageResults
import discord
import watson
from discord.ext.commands import Bot
TOKEN = '{api-key}'
BOT_PREFIX = ("!", "$")
client = Bot(command_prefix=BOT_PREFIX)
@client.command(name="Memealyze",
description="Send your favorite meme and the boys at IBM will get to work telling you what they see",
brief="Neural network put to good use",
pass_context=True)
async def GetMemeContents(context):
await client.say("Sending image to the mothership, hold tight")
messageContent = ""
imageUrl = str(context.message.attachments[0]['url'])
resultDict = watson.ReturnWatsonResults(imageUrl)
for key,val in resultDict.items():
messageContent += (key + ":" + val + "%" + "\n")
await client.say("Done, the boys at IBM said they found this:\n" + messageContent)
client.run(TOKEN)
我知道python脚本编写得不是很好,但是它们确实有效,目前网络中的问题完全在于pip正在安装模块的地方,由于某种原因它们在被systemd运行时无法访问。
我怀疑你的启动脚本正在启动一个不同于你已安装discord的Python。
尝试添加该行,
import sys; print(sys.executable, sys.prefix)
在main.py
之前到你的import discord
。并尝试在你的python3
shell中运行它。这应该分别打印安装Python可执行文件和标准库的位置。如果它们在main.py
中与在shell中不同,那就是你的问题。
也试试
$ which python3
$ which pip3
一旦你知道实际运行的Python可执行文件的路径,就可以使用Python的pip
$ foo -m pip install discord
其中foo
是您在sys.executable
中使用main.py
打印的Python可执行文件的完整路径。
您还可以尝试将discord
安装到虚拟环境中。
$ python3 -m venv foo
$ source foo/bin/activate
$ pip install discord # install all your other requirements too
其中foo
是一些可以安装虚拟环境的路径。然后在启动脚本中,在运行main.py
之前激活源激活。这可以确保python将在您刚刚创建的foo
环境中运行。
#!/bin/bash
source foo/bin/activate
python /home/ubuntu/discordBot/main.py
请注意,在活动虚拟环境中,即使使用python
创建环境,也可以使用pip
和python3
。