-1

I have a python script which is executed every 5 minutes by the OS.

The problem is it needs to first activate the jdate virtual I have created for it in /tmp to be able to use the jdatetime module installed there.

I have tried both #1 and #2 but no success. Executing the script in the terminal gives me errors.

#!/usr/bin/env python

# 1:
import os
os.system(f"cd /tmp ; source jdate/bin/activate")
# returns error: ModuleNotFoundError: No module named 'jdatetime'

# 2:
import subprocess
subprocess.check_output(f"cd /tmp ; source jdate/bin/activate", shell=True)
# returns error: ModuleNotFoundError: No module named 'jdatetime'
nino
  • 554
  • 2
  • 7
  • 20

1 Answers1

0

AFAIK, what the venv activation does is just prepend the venv/bin folder path to the PATH environment variable. If you are using CRON to shedule the execution, have you tried this?:

*/5 * * * * (cd /tmp/jdate/bin && python path/to/project/myapp.py)
asmartin
  • 459
  • 1
  • 3
  • 12
  • I'm not sure if it's a wise decision, but I decided to replace ```#!/usr/bin/env python``` with ```#!/bin/python3``` at the beginning of the script. – nino Jan 22 '21 at 11:04