Hi, To excute a "Java" command, using os.system, you need to either give absolute path, or you need to append the path in system variable. For example,
1. Either os.system('/usr/bin/java') or 2. import sys sys.path.append('/usr/bin') os.system('java') Hope this helps. Regards, Priyesh On Tue, May 31, 2011 at 3:51 AM, <tutor-requ...@python.org> wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > > You can reach the person managing the list at > tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Finding error from os.system(cmd) (Kann Vearasilp) > 2. Re: Finding error from os.system(cmd) (Alexandre Conrad) > 3. Re: Finding error from os.system(cmd) (Marilyn Davis) > 4. Re: Importing classes when needed (Alan Gauld) > 5. Re: Importing classes when needed (Marilyn Davis) > 6. Re: Importing classes when needed (Steven D'Aprano) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 30 May 2011 14:28:49 +0200 > From: Kann Vearasilp <vearas...@gmail.com> > To: Python Tutor <tutor@python.org> > Subject: [Tutor] Finding error from os.system(cmd) > Message-ID: <BANLkTim8C+cTraAUu+F0n1hy=-gl5i+...@mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Dear all, > > I tried using python to execute some external java program in my code. > My problem is the os.system(cmd) was not working properly while > executing 'java' from terminal worked just fine. I am not sure what is > wrong here. Is there a way to print out/detect error in my code for > this case? > > >>>> > > 1 import os > 2 > 3 def create_image(): > 4 path = os.path.abspath('tmp/medusa') > 5 medusa = os.path.abspath('mirnaworkbench/Medusa/Medusa.jar') > 6 cmd = str('java -cp ' + medusa + ' > medusa.batchoperations.BatchOperations ' + path) > 7 os.system(cmd) > > >>>> > > Thanks, > > Kann > > > ------------------------------ > > Message: 2 > Date: Mon, 30 May 2011 09:46:04 -0700 > From: Alexandre Conrad <alexandre.con...@gmail.com> > To: Kann Vearasilp <vearas...@gmail.com> > Cc: Python Tutor <tutor@python.org> > Subject: Re: [Tutor] Finding error from os.system(cmd) > Message-ID: <BANLkTikratSp1gey-DvzAc-S0xrma=c...@mail.gmail.com> > Content-Type: text/plain; charset=UTF-8 > > Hi Kann, > > I haven't looked at your problem closely but you might need to > explicitly tell Python where to output stderr/stdout. I just ran > os.system('ls -al') on my Linux box and it seems to print both > stderr/stdout to my terminal. The return value is the return code of > the process (might be different on another OS though). > > As I hint you might want to know that the "subprocess" module is meant > to replace many Python functions that spawn processes, such as > os.system(). The subprocess module might be better documented with > examples: > > http://docs.python.org/library/subprocess.html > > Only if you are running Python 2.7 (or 3.x I believe), the > subprocess.check_output() function might be useful to you. > > HTH, > > 2011/5/30 Kann Vearasilp <vearas...@gmail.com>: > > Dear all, > > > > I tried using python to execute some external java program in my code. > > My problem is the os.system(cmd) was not working properly while > > executing 'java' from terminal worked just fine. I am not sure what is > > wrong here. Is there a way to print out/detect error in my code for > > this case? > > > >>>>> > > > > ?1 import os > > ?2 > > ?3 def create_image(): > > ?4 ? ? path = os.path.abspath('tmp/medusa') > > ?5 ? ? medusa = os.path.abspath('mirnaworkbench/Medusa/Medusa.jar') > > ?6 ? ? cmd = str('java -cp ' + medusa + ' > > medusa.batchoperations.BatchOperations ' + path) > > ?7 ? ? os.system(cmd) > > > >>>>> > > > > Thanks, > > > > Kann > > _______________________________________________ > > Tutor maillist ?- ?Tutor@python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Alex | twitter.com/alexconrad > > > ------------------------------ > > Message: 3 > Date: Mon, 30 May 2011 09:48:04 -0700 (PDT) > From: "Marilyn Davis" <mari...@pythontrainer.com> > To: tutor@python.org > Subject: Re: [Tutor] Finding error from os.system(cmd) > Message-ID: > <47524.67.169.189.143.1306774084.squir...@mail.tigertech.net> > Content-Type: text/plain;charset=utf-8 > > Hi Kann, > > So you are saying that you printed your command, then ran it at the > terminal prompt, and it ran ok? > > You might want to look at the subprocess library so that you can collect > stderr from the process. The online documentation is great, with lots of > examples. > > BTW, on your line 6, you str(some-stuff-that-is-already-a-str), which is a > waste, and a little confusion for your reader. > > I hope you find some help in these suggestions. > > Marilyn Davis > > > On Mon, May 30, 2011 5:28 am, Kann Vearasilp wrote: > > > Dear all, > > > > > > I tried using python to execute some external java program in my code. > > My problem is the os.system(cmd) was not working properly while > > executing 'java' from terminal worked just fine. I am not sure what is > > wrong here. Is there a way to print out/detect error in my code for this > > case? > > > >>>>> > > > > 1 import os > > 2 > > 3 def create_image(): > > 4 path = os.path.abspath('tmp/medusa') > > 5 medusa = os.path.abspath('mirnaworkbench/Medusa/Medusa.jar') > > 6 cmd = str('java -cp ' + medusa + ' > > medusa.batchoperations.BatchOperations ' + path) 7 os.system(cmd) > > > > > >>>>> > > > > Thanks, > > > > > > Kann > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > > > ------------------------------ > > Message: 4 > Date: Mon, 30 May 2011 21:57:27 +0100 > From: "Alan Gauld" <alan.ga...@btinternet.com> > To: tutor@python.org > Subject: Re: [Tutor] Importing classes when needed > Message-ID: <is10bo$a38$1...@dough.gmane.org> > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > > "Alexandre Conrad" <alexandre.con...@gmail.com> wrote > > >> Why not use the os functions to read the file names > >> dynamically and build the list that way? Provided > >> the files use a standard naming scheme you don't > >> need to change the init code. > > > > I wouldn't do that. If Timo adds non-parser modules in that > > directory > > (say some utils.py file for all parsers to use), then you have to > > maintain a list of excluded files > > I did say he would have to use a naming convention and > that in turn implies the use of glob to filter the names returned. > But I agree it has risks but then, so does relying on > always editing init.py > > I just prefer to avoid introducing hard to remember > maintenance tasks when I can do it automatically, > but of course you need to remember the naming convention. > But hopefully a quick 'ls' will do that for you... > > Alan G. > > > > > ------------------------------ > > Message: 5 > Date: Mon, 30 May 2011 14:57:24 -0700 (PDT) > From: "Marilyn Davis" <mari...@pythontrainer.com> > To: "Alan Gauld" <alan.ga...@btinternet.com> > Cc: tutor@python.org > Subject: Re: [Tutor] Importing classes when needed > Message-ID: > <55862.67.169.189.143.1306792644.squir...@mail.tigertech.net> > Content-Type: text/plain;charset=utf-8 > > If we are coding via a vote, I'd be with Alan. > > If Timo adds non-parser modules, and they get through his glob filter, > then surely his code will break with a nice error statement and that would > remind him of his convention. > > Or maybe it would just give a verbose report and go on to the next file. > > Marilyn > > On Mon, May 30, 2011 1:57 pm, Alan Gauld wrote: > > > "Alexandre Conrad" <alexandre.con...@gmail.com> wrote > > > > > >>> Why not use the os functions to read the file names > >>> dynamically and build the list that way? Provided the files use a > >>> standard naming scheme you don't need to change the init code. > >> > >> I wouldn't do that. If Timo adds non-parser modules in that > >> directory (say some utils.py file for all parsers to use), then you have > >> to maintain a list of excluded files > > > > I did say he would have to use a naming convention and > > that in turn implies the use of glob to filter the names returned. But I > > agree it has risks but then, so does relying on always editing init.py > > > > I just prefer to avoid introducing hard to remember > > maintenance tasks when I can do it automatically, but of course you need > to > > remember the naming convention. But hopefully a quick 'ls' will do that > > for you... > > > > Alan G. > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > ------------------------------ > > Message: 6 > Date: Tue, 31 May 2011 08:21:36 +1000 > From: Steven D'Aprano <st...@pearwood.info> > To: "tutor@python.org" <Tutor@python.org> > Subject: Re: [Tutor] Importing classes when needed > Message-ID: <4de41870.8020...@pearwood.info> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Timo wrote: > > Hello all, > > > > I have a question about how this is done the best way. > > > > In my project I have a folder with multiple file parsers, like this: > > - src > > -- main.py > > -- parsers > > --- __init__.py > > --- parser1.py > > --- parser2.py > > This gives you a stand-alone module called "main.py", and a separate > package called "parsers". Is that intended? > > I might think about putting this in a single package, perhaps like this: > > > - src > -- my_application > -- __init__.py > -- __main__.py > -- parsers > --- __init__py > --- parser1.py > --- parser2.py > > > Now my_application is treated as a single package, containing a > sub-package my_application.parsers. > > The purpose of the __main__.py is that when you call the package from > the command line, like this: > > python -m my_application > > the code inside __main__.py is executed. > > > > The parsers just contain a class which do the work. > > > Keep in mind that this is Python, not Java, and it's not compulsory to > have one class per file. If the classes are small enough you are > encouraged to put them in the one file. > > Particularly if you use inheritance to delegate most of the work to a > single parent class, something like this made up example: > > from parsers import Parser1 > > class Parser2(Parser1): > def parse(self, text): > words = super(Parser2, self).parse(text) > words = [s.lower() for s in words] > return words > > > > > When the user clicks a button, I want to show all available parsers and > > use the choosen one when the user clicks "ok". > > Is it ok to place the following code in the __init__.py? > > from parser1 import Parser1 > > from parser2 import Parser2 > > def get_parsers(): > > return [Parser1(), Parser2()] > > Certainly. You can have any code you like in __init__.py. > > > > If so, is it ok to do a get_parsers() everytime the user clicks a > > button? Because that will initialize the parsers over and over again, > > right? > > Is that a problem? > > If you prefer to always use the same two parsers, do this instead: > > import parser1 > import parser2 > > PARSERS = tuple(p() for p in (parser1, parser2)) > > > Then instead of calling get_parsers(), just use PARSERS. > > > > -- > Steven > > > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 87, Issue 120 > ************************************** >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor