top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

python script is not running

0 votes
280 views

I want to run python script which generating data into json fromat, I am using crontab, but it's not executing...

my python code--

try.py --

import json
import simplejson as json
import sys

def tryJson():
 saved = sys.stdout
 correctFile = file('data.json', 'a+')
 sys.stdout = correctFile
 result = []
 i = 1
 for i in range(5):
 info = {
 'a': i+1,
 'b': i+2,
 'c': i+3,
 }
 result.append(info)

 if result:
 print json.dumps(result, indent=6)
 sys.stdout = saved
 correctFile.close()
tryJson()

now i m doing ion terminal-
avin@hp:~$ crontab -e
then type -
*/2 * * * * python /home/avin/data/try.py

and save

but it's not executing.

posted May 18, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You may need to put an explicit path to your Python interpreter. Type:

$ which python

and put that into your crontab.

answer May 18, 2013 by anonymous
True. Somewhat more generally, jobs run under cron have a far more barren environment than most people realize. Or, looking at it a different way, most people don't even realize all the ways they depend on their environment being set up properly by the login process.

If you've set things like PYTHONPATH, you won't have them set right for cron jobs unless you explicitly reset them in your crontab.

It's often instructive to run something like "env > /tmp/xxx" under cron, and compare that to what you get when you run "env" at a command-line prompt.
Similar Questions
0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

+2 votes

I have started looking into distutils because I need to write an extension module in C (for performance reasons) and distutils seems to be the most straight-forward way.

I have had success building a C file into a Python extension module using "python setup.py build" but I am wondering what the recommended way for using that module during development is. While writing Python code I am used to just run the code from the source directory. But the built extension modules .so of course does not just end up on sys.path magically.

So how do I run my code so it will find the built extension module? Do I pass the output directory on the command line manually or is there some other solution? I would like to still be able to run the code from the source directory as I am using PyCharm to edit and debug the code.

+1 vote

I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work.

import subprocess

p = subprocess.Popen("D:PythonPython27Scriptspip.exe list -o",
 stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT,
 bufsize=1,
 universal_newlines=True,
 shell=False)
for line in p.stdout:
 print line

When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command
line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking?

0 votes

I'm new to Python and trying to run a already written code. Can someone please explain the error below? And if possible, how do I resolve this?

Traceback (most recent call last):
 File "c:Project_1regression_1.py", line 7, in  from sklearn import metrics, cross_validation, linear_model
 File "c:Python27libsite-packagessklearnmetrics__init__.py", line 31, in from . import cluster
 File "c:Python27libsite-packagessklearnmetricscluster__init__.py", line 8, in 
 from .supervised import adjusted_mutual_info_score
 File "c:Python27libsite-packagessklearnmetricsclustersupervised.py", line 19, in 
 from .expected_mutual_info_fast import expected_mutual_information
 File "expected_mutual_info_fast.pyx", line 10, in init sklearn.metrics.cluster
.expected_mutual_info_fast (sklearnmetricsclusterexpected_mutual_info_fast.c: 4886)
 File "c:Python27libsite-packagesscipyspecial__init__.py", line 529, in <module>
 from ._ufuncs import *
ImportError: DLL load failed: The specified module could not be found.
...