Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45500 > unrolled thread

python script is not running

Started byAvnesh Shakya <avnesh.nitk@gmail.com>
First post2013-05-18 03:12 -0700
Last post2013-05-19 09:33 +1000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  python script is not running Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-05-18 03:12 -0700
    Re: python script is not running Chris Angelico <rosuav@gmail.com> - 2013-05-18 23:28 +1000
      Re: python script is not running Roy Smith <roy@panix.com> - 2013-05-18 09:44 -0400
    Re: python script is not running Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-18 12:35 -0400
    Re: python script is not running woooee <woooee@gmail.com> - 2013-05-18 10:35 -0700
      Re: python script is not running Chris Angelico <rosuav@gmail.com> - 2013-05-19 03:59 +1000
      Re: python script is not running Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2013-05-18 20:43 +0200
      Re: python script is not running Chris Angelico <rosuav@gmail.com> - 2013-05-19 09:33 +1000

#45500 — python script is not running

FromAvnesh Shakya <avnesh.nitk@gmail.com>
Date2013-05-18 03:12 -0700
Subjectpython script is not running
Message-ID<bb111639-6a94-4151-a153-9bbebe75a4ea@googlegroups.com>
hi,
    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.

[toc] | [next] | [standalone]


#45508

FromChris Angelico <rosuav@gmail.com>
Date2013-05-18 23:28 +1000
Message-ID<mailman.1801.1368883685.3114.python-list@python.org>
In reply to#45500
On Sat, May 18, 2013 at 8:12 PM, Avnesh Shakya <avnesh.nitk@gmail.com> wrote:
> avin@hp:~$ crontab -e
> then type -
> */2 * * * * python /home/avin/data/try.py
>

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

$ which python

and put that into your crontab.

ChrisA

[toc] | [prev] | [next] | [standalone]


#45509

FromRoy Smith <roy@panix.com>
Date2013-05-18 09:44 -0400
Message-ID<roy-FB5354.09442518052013@news.panix.com>
In reply to#45508
In article <mailman.1801.1368883685.3114.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> On Sat, May 18, 2013 at 8:12 PM, Avnesh Shakya <avnesh.nitk@gmail.com> wrote:
> > avin@hp:~$ crontab -e
> > then type -
> > */2 * * * * python /home/avin/data/try.py
> >
> 
> You may need to put an explicit path to your Python interpreter. Type:
> 
> $ which python
> 
> and put that into your crontab.

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.

[toc] | [prev] | [next] | [standalone]


#45515

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-05-18 12:35 -0400
Message-ID<mailman.1806.1368894928.3114.python-list@python.org>
In reply to#45500
On 5/18/2013 6:12 AM, Avnesh Shakya wrote:
> hi,
>      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

Don't need to change stdout.

>      result = []
>      i = 1
>      for i in range(5):
>          info = {
>                  'a': i+1,
>                  'b': i+2,
>                  'c': i+3,
>                 }
>          result.append(info)

What is you want to add print result for debugging?

>      if result:
>          print json.dumps(result, indent=6)

Either use print >> correctFile, correctFile.write (do you really want 
the extra '\n' that print adds?), or, do the future import of print as 
function and pass correctFile as the file argument

>          sys.stdout = saved
>          correctFile.close()
> tryJson()

[toc] | [prev] | [next] | [standalone]


#45518

Fromwoooee <woooee@gmail.com>
Date2013-05-18 10:35 -0700
Message-ID<185557b8-5cd0-4e9c-8c6d-855a8635492c@pd6g2000pbc.googlegroups.com>
In reply to#45500
The obvious question, do you have the shebang on the first line so the
OS knows it's to be run as a Python program?  Also I would change
tryJson() to
if __name__ == "__main__':
    tryJson()
This probably won't make any difference but you will have the bases
covered.

[toc] | [prev] | [next] | [standalone]


#45519

FromChris Angelico <rosuav@gmail.com>
Date2013-05-19 03:59 +1000
Message-ID<mailman.1809.1368899960.3114.python-list@python.org>
In reply to#45518
On Sun, May 19, 2013 at 3:35 AM, woooee <woooee@gmail.com> wrote:
> The obvious question, do you have the shebang on the first line so the
> OS knows it's to be run as a Python program?

That won't make any difference; the cron job specifically stipulates
the interpreter. It just needs to be done with a full path.

ChrisA

[toc] | [prev] | [next] | [standalone]


#45521

FromVincent Vande Vyvre <vincent.vandevyvre@swing.be>
Date2013-05-18 20:43 +0200
Message-ID<mailman.1811.1368903041.3114.python-list@python.org>
In reply to#45518
Le 18/05/2013 19:59, Chris Angelico a écrit :
> On Sun, May 19, 2013 at 3:35 AM, woooee <woooee@gmail.com> wrote:
>> The obvious question, do you have the shebang on the first line so the
>> OS knows it's to be run as a Python program?
> That won't make any difference; the cron job specifically stipulates
> the interpreter. It just needs to be done with a full path.
>
> ChrisA
Not necessary, I use crontab without problem with this line:

25 16 18 5 * export DISPLAY=:0 & LC_CTYPE="fr_FR.utf-8" 
Lang="fr_FR.utf-8" python /usr/bin/qarte -a 1

... on Ubuntu.

-- 
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte 
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>

[toc] | [prev] | [next] | [standalone]


#45535

FromChris Angelico <rosuav@gmail.com>
Date2013-05-19 09:33 +1000
Message-ID<mailman.1822.1368920031.3114.python-list@python.org>
In reply to#45518
On Sun, May 19, 2013 at 4:43 AM, Vincent Vande Vyvre
<vincent.vandevyvre@swing.be> wrote:
> Le 18/05/2013 19:59, Chris Angelico a écrit :
>
>> On Sun, May 19, 2013 at 3:35 AM, woooee <woooee@gmail.com> wrote:
>>>
>>> The obvious question, do you have the shebang on the first line so the
>>> OS knows it's to be run as a Python program?
>>
>> That won't make any difference; the cron job specifically stipulates
>> the interpreter. It just needs to be done with a full path.
>>
>> ChrisA
>
> Not necessary, I use crontab without problem with this line:
>
> 25 16 18 5 * export DISPLAY=:0 & LC_CTYPE="fr_FR.utf-8" Lang="fr_FR.utf-8"
> python /usr/bin/qarte -a 1
>
> ... on Ubuntu.

Everything's configurable. I'd like to hear back from the OP though;
as Roy said, checking 'env' from a cron job will reveal much.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web