Nevo
Nevo

Reputation: 63

Pip3 Install Module For All Users

Not sure if I should be asking this here or over on Linux stack overflow but here it goes.

I'm relatively new to python and I've been struggling to get this python script to autostart on an aws machine. I have two modules I need installed "discord.py" and "watson-cloud-developer". Pip3 installs the aforementioned modules with no error. When trying to run a service that runs a script that runs the python script (gotta love systemd) I get an error telling me that the discord module isn't installed, see below.

Systemctl error

    ● 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
>>>

I'm more than happy to provide any additional info.

EDITS:

service:

[Service]
ExecStart=/usr/local/sbin/startbot.sh

bash script:

#!/bin/bash
python3 /home/ubuntu/discordBot/main.py

python scripts:

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)

I know that the python scripts aren't very well written but they do work, the current hitch in the giddy up lies solely in where pip is installing the modules, for one reason or another they can't be accessed when being ran by systemd.

Upvotes: 2

Views: 1808

Answers (1)

gilch
gilch

Reputation: 11681

I suspect that your startup scripts are launching a different Python than the one you have installed discord to.

Try adding the line,

import sys; print(sys.executable, sys.prefix)

to your main.py, before the import discord. And also try running that in your python3 shell. This should print off where the Python executable and standard library are installed, respectively. If they're different in main.py than in the shell, that's your issue.

Also try

$ which python3
$ which pip3

Once you know the path to the Python executable you're actually running, you can use that Python's pip with

$ foo -m pip install discord

where foo is the full path to the Python executable you printed off with sys.executable in your main.py.


You can also try installing discord to a virtual environment.

$ python3 -m venv foo
$ source foo/bin/activate
$ pip install discord  # install all your other requirements too

where foo is some path you can install the virtual environment to. Then in your launch script, do the source activate before running main.py. This ensures that python will run in the same foo environment you just created.

#!/bin/bash
source foo/bin/activate
python /home/ubuntu/discordBot/main.py

Note that in an active virtual environment, you use python and pip even if you created the environment with python3.

Upvotes: 1

Related Questions