Reputation: 796
I am testing a (python) bot script before using it with crontab. I was advised to run a shell script that runs the python script in the crontab. In order for the shell script to run, I need to activate the venv. This is where the problem is. When I try to run run_socialbot.py, I get:
Traceback (most recent call last):
File "/home/gsb/socialbot.py", line 3, in <module>
from instapy import InstaPy
ModuleNotFoundError: No module named 'instapy'
Even though instapy is installed in the venv. And when I open up the interpreter with "python3" and try to import instapy while the venv is activated, it works fine. Here is the shell script code:
#! /bin/bash
source /home/gsb/venv/bin/activate
/usr/bin/python3 /home/gsb/socialbot.py
Can anyone help out? thanks in advance
I'm running this at a digitalocean server. Ubuntu 20.04
Upvotes: 7
Views: 7895
Reputation: 363616
Your shell script activates the venv but then it explicitly calls an absolute path /usr/bin/python3
. That's not the venv Python executable. The whole point of activating the venv is to set $PATH
so that python
points at the venv interpreter.
In your wrapper script, change it to:
python /home/gsb/socialbot.py
Alternatively you can remove the bash wrapper script entirely, and put a venv shebang into socialbot.py
, then use this Python file in the crontab directly. Make the file executable, and add as the first line:
#!/home/gsb/venv/bin/python
Upvotes: 3