python run redis error when crontab shell - python

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.

Related

After cloning my Flask app to my new computer I can not get it to run

I am building a Flask app to create a peer to peer market place. I am having trouble getting the code running on my new computer. The code can be found here: https://github.com/ripemelon/flaskbyge.
flaskbyge$ python run.py
/home/byge/.local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
File "run.py", line 1, in <module>
from flaskbyge import app
File "/home/byge/Desktop/flaskbyge/flaskbyge/__init__.py", line 15, in <module>
from flaskbyge import routes
File "/home/byge/Desktop/flaskbyge/flaskbyge/routes.py", line 33
flash(f'Account created for {form.username.data}! You can now log in','success' )***
It gives me this error when I try to run the Flask module using the SQLAlchemy database.
You are using Python2.7 as you can see in the first line of your error message. It seems that this app is developed for at least Python3.6, because formatted strings are used as you can see in the last line (flash(f'Account ...)).
This works for me:
$ cd $ROOT_FOLDER_OF_YOUR_APP
$ python3.8 -m venv flaskbyge
$ source flaskbyge/bin/activate
$ pip install -r requirements.txt
$ python run.py
For me python3.8 is clearly running the Python3.8 interpreter. The command can be different for you, maybe python3 or similar. Maybe Python3.6 is enough. Note, that after activating the virtual environment, the python and pip command refers to the right Python version (here Python3.8).
All after all, it seems that you only have to ensure that you don't start the app via Python2.7.

thread vs threading module in Python

I am developing web application through Django/Python framework on Raspberry Pi platform running Debian Linux and Python 2.7.9 .
I have to use multiple threads in Python script to handle multiple peripherals such as camera and microphone. These peripherals must be handled in real time.
I wonder why I can issue "import thread" but not "import threading" in my Python script (does not matter whether it is Python version 2.x or 3.x) ?
pi#raspberrypi:~ $ python --version
Python 2.7.9
pi#raspberrypi:~ $ python ./mythread.py
Traceback (most recent call last):
File "./mythread.py", line 4, in <module>
from threading import Thread
File "/home/pi/threading.py", line 8, in <module>
del _sys.modules[__name__]
AttributeError: 'module' object has no attribute 'Thread'
pi#raspberrypi:~ $ python3 ./mythread.py
Traceback (most recent call last):
File "./mythread.py", line 4, in <module>
from threading import Thread
ImportError: bad magic number in 'threading': b'\x03\xf3\r\n'
A couple of issues could exist, one of which, as #Alberto mentioned, is the fact that Python may be trying to used a pre-compiled, byte-code .pyc file. To avoid this, you can remove any pycache files from the related directory, which will then be recompiled when the interpreter tries to run them. The Bash code below will recursively remove .pyc file recursively from the current directory.
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

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!

A crontab script accessing Django models fails with ImportError

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

How to set a python path when using runit

I am am using runit to manage a process on Ubuntu 12.04. I get the below error in the logs when I run:
sv up test/
I assume it is a python path issue.
ImportError: No module named htcommon.ht_redis
Traceback (most recent call last):
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py", line 17, in <module>
from htpData import HTPItemBase, HTPUserBase
File "/home/ubuntu/workspace/htFrontEnd/htanalytics/htpData.py", line 9, in <module>
from htcommon.ht_redis import HTRedisConnection
ImportError: No module named htcommon.ht_redis]
I have also set the path in /etc/environment and also set in .bashrc.
Below is my runit script.
#!/bin/sh
exec 2>&1
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat
exec export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1
root#aws-rpc-server-east-staging-20130203070552:/etc/sv#
When I run the process from the command line I get no issues and it works.
/usr/bin/python /home/ubuntu/workspace/htFrontEnd/heythat/htanalytics/ht_rpc_server.py
Why will runit not work? Why can it not find the path?
Look like a few potential problems with this script. I don't think an 'export' can be 'exec'd. At least, it fails in my version of bourne. The exec command replaces the current process (your script) with the called command and doesn't return unless the called command fails. So, it doesn't make sense to 'exec' the exports anyway. Also they can be collapsed into one line. So, you're script should look more like this:
#!/bin/sh
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/workspace/htFrontEnd/heythat:/home/ubuntu/workspace/htFrontEnd/heythat/htanalytics
exec /usr/bin/python /home/ubuntu/workspace/htFrontEnd/htanalytics/ht_rpc_server.py >> /tmp/ht_rpc_server.log 2>&1

Categories

Resources