不确定我是否应该在Linux堆栈溢出时在这里还是在这里问这个问题,
我是python的新手,我一直在努力让这个python脚本在aws机器上自动启动。我有两个模块需要安装“ discord.py”和“ watson-cloud-developer”。 Pip3正确安装了上述模块。尝试运行运行脚本的服务时,该脚本运行的是python脚本(请注意systemd),我收到一条错误消息,提示未安装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
>>>
我很乐意提供任何其他信息。
编辑:
服务:
[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运行。
答案 0 :(得分:0)
我怀疑您的启动脚本所启动的Python与您安装的不一致的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
。