A crontab script accessing Django models fails with ImportError - python

I'm trying to use a crontab to execute a python script periodically. I followed the solution given here to execute the command using virtualenv. Following is my crontab
SHELL=/bin/bash
HOME=/
MAILTO="myid#example.com"
* * * * * cd /home/jaskaran/edmhunters && /home/jaskaran/edmhunters/env/bin/python /home/jaskaran/edmhunters/scripts/db/songlist.py
I keep getting this ImportError
Traceback (most recent call last):
File "/home/jaskaran/edmhunters/scripts/db/songlist.py", line 4, in <module>
from hunt.models import DJ, Song
ImportError: No module named hunt.models
The script works fine when run from the shell. What am I missing?

Django import errors are often misleading due to how Django loads its INSTALLED_APPLICATIONS and all implicit code what comes when Django tries to access its settings.
To work around, create a Django management command out of your script.
Then try this syntax if you need to run virtualenv'ed Django script or management command from shell script or cron:
export DJANGO_SETTINGS_MODULE=mymodule.settings ; /srv/django/myproject/venv/bin/python /srv/django/myproject/manage.py MyManagementCommand

Related

Issue with Python 3 Imports when using a Shell script

I have the following scenario:
A Python3 Package (meross_iot) installed through pip and located in
~/.local/lib/python3.7/site-packages
A Python script (meross_electricity.py) that imports from this package: from meross_iot.controller.mixins.electricity import ElectricityMixin
A shell script (launcher.sh) that is meant to be a wrapper so that the .py script is run at startup:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi/Documents
sudo python meross_electricity.py
cd /
If I simply execute the .py file everything works as expected, imports done, etc. If I try to run the .sh script I get the following error:
pi#home:~/Documents $ ./launcher.sh
Traceback (most recent call last):
File "meross_electricity.py", line 4, in <module>
from meross_iot.controller.mixins.electricity import ElectricityMixin
ModuleNotFoundError: No module named 'meross_iot'
Can someone please help me solve this issue?
Thanks!
Installed package with sudo worked .
Answer based on comment by #KlausD

Import Error in python when non root user

I am having an issue which has been driving me crazy. I am trying to run a python script as a non root user but when I try to execute the script I get the following error.
Traceback (most recent call last):
File "/usr/local/lib/EdgarRenderer/src/EdgarRenderer.py", line 13, in <module>
from arelle import PythonUtil # define 2.x or 3.x string types
ImportError: cannot import name PythonUtil
Now if I execute it as the root user it runs with out a hitch. I have triple checked all permissions and all the scripts and folders access by the desired user are in fact owned by that user (with the exception of the /usr/bin/python3.3 file). This is the command I am trying to execute
su - tomcat -c '/usr/bin/python3.3 /usr/local/lib/EdgarRenderer/src/EdgarRenderer.py -c /usr/local/lib/EdgarRenderer/conf/RunEdgar.xml --xdgConfigHome=/usr/local/lib/re3/arelle'
I run the exact same script the exact same way on another server with out any errors. I am using CentOS 6.5
Thanks!

importError using a virtualenv from cmd on Windows

trying to run a flask dev server on localhost:5000 using a virtualenv on Windows 7
In my command line in the project directory, i activated the virtualenv with the command "env\scripts\activate". It seemed to work, as my next line was preceded with an (env) tag. When I attempted to run the app file (bank_app), however, I got an import error.
Here's the console log
C:\Users\TJ\Documents\Python Projects\TestingPython> env\scripts\activate
(env) C:\Users\TJ\Documents\Python Projects\TestingPython> bank_app
Traceback (most recent call last):
File "C:\Users\TJ\Documents\Python Projects\TestingPython\bank_app.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
and here's a gist of the bank_app file (just in case it's relevant)
I'm used to running the code directly from PyCharm, which handles the virtualenv for me. it works fine running directly from PyCharm
This is probably related to how Windows maps extensions to executables.
You started the script with bank_app, which is really not the name of your script (your script has a .py extension I assume?). Windows must be doing a search in your directory, and then starting the script with the interpreter that is registered for the .py extension of the script, which is the globally installed interpreter, not the interpreter that is currently in the PATH.
I'm pretty sure if you run the command as python bank_app.py everything will work just fine.

Python script elicits error when run in cron job but at no other time

I have a shell script that includes execution of a python script. When I run it manually in terminal, it works fine. However when I execute the shell script in a cron job, the python script fails.
The error is apparently triggered while functions are being imported from module1 into module2. The function referenced by the error is not among the functions being imported, nor does the function where the syntax error is supposed to be elicit an error when it is executed by itself.
Here's the error that gets logged when I run the cron job:
File "/Users/me/module2.py", line 5, in <module>
from module1 import consolidate_rankings, build_all
File "/Users/me/module1.py", line 159
things = {row["thing"]: row for row in rows}
^
SyntaxError: invalid syntax
The module2 script is pretty straightforward:
#!/usr/bin/env python
from module1 import consolidate_rankings, build_all
consolidate_rankings()
build_all()
Here's the line that calls this in the shell script:
python /Users/me/module2.py
Anyone have any idea what's going on here?
You are probably running your script from cron with a different version of Python that doesn't support the dictionary comprehension syntax.
To fix this, either explicitly add your desired Python version in the shebang line:
#!/usr/bin/env python2.7
or launch your script from cron through the correct command:
* * * * /usr/bin/python2.7 /path/to/script.py
I solved this by declaring my local env in the crontab itself (since the above didn't help):
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/sifi/bin:/home/username/.local/bin:/home/username/bin:/home/username

python run redis error when crontab shell

i install redis-2.7.1 in my folder,and run my python code,it worked.
COMMON_MODEL_DIR="/data/aa/redis-2.7.1"
sys.path.append(COMMON_MODEL_DIR)
import redis
And when i set up crontab ,
*/10 1-23 * * * (cd /data3/aa/; python step.py 2 >> /data3/aa/2.log 2>&1)
But it won't worked
it will report
Traceback (most recent call last):
File "step.py", line 11, in ?
import redis
File "/data/aa/redis-2.7.1/redis/__init__.py", line 1, in ?
from redis.client import Redis, StrictRedis
File "/data/aa/redis-2.7.1/redis/client.py", line 157
response = [nativestr(i) if i is not None else None for i in response]
^
SyntaxError: invalid syntax
How will i fix it?
Obviously the user account under which your cron job is running is using another (and quite old) Python version that doesn't support ternary expressions. Find out which Python you are using and pass the full path in your crontab.

Categories

Resources