f



Invoking Python from Python

Hi all,

I have a need to create a Python script on the fly from another Python
program and then execute the script so created.  Do I need to invoke
Python through os.spawnl or is there a better way?

Thanks,

--
John

0
john106henry (186)
11/8/2005 4:10:25 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

10 Replies
1412 Views

Similar Articles

[PageSpeed] 19

Am Tue, 08 Nov 2005 08:10:25 -0800 schrieb John Henry:

> Hi all,
> 
> I have a need to create a Python script on the fly from another Python
> program and then execute the script so created.  Do I need to invoke
> Python through os.spawnl or is there a better way?

Hi,

creating source code with a script, is no good solution. 

Once I had to maintain lisp code which stored its data in lisp code, too
(incl. conditions and loops). It was a nightmare.

Please explain what you want to do, and we will find a better solution.

 HTH,
  Thomas

-- 
Thomas G�ttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: niemand.leermann@thomas-guettler.de

0
11/8/2005 4:21:12 PM
In article <pan.2005.11.08.16.21.11.472836@thomas-guettler.de>,
Thomas Guettler  <niemand.leermann@thomas-guettler.de> wrote:
			.
			.
			.
>creating source code with a script, is no good solution. 
>
>Once I had to maintain lisp code which stored its data in lisp code, too
>(incl. conditions and loops). It was a nightmare.
>
>Please explain what you want to do, and we will find a better solution.
			.
			.
			.
Yes and no.  There are times when it's justified.  I ENTIRELY
agree, though, that many people who *think* that's what they
want to do simply don't understand how dynamic base Python is,
and therefore don't realize how much easier it can be to write
a single, unified application.

At this point, 'twould be appropriate to describe an instance
or two in which code generation is a good idea.  While I have
some, they're tedious to make clear.  Maybe I'll do so in a
follow-up ...
0
claird (2363)
11/8/2005 6:08:03 PM
John Henry wrote:
> Hi all,
> 
> I have a need to create a Python script on the fly from another Python
> program and then execute the script so created.  Do I need to invoke
> Python through os.spawnl or is there a better way?

Could you import the generated script?  This might be the way to go if, 
e.g., you're generating a "template" configuration file that might 
subsequently be edited by a human being.
0
jeff34 (1597)
11/8/2005 7:20:32 PM
John Henry wrote:
> Hi all,
>
> I have a need to create a Python script on the fly from another Python
> program and then execute the script so created.  Do I need to invoke
> Python through os.spawnl or is there a better way?

When doing something similar to this I used the built-in 'execfile()'
function to execute the code created (and retrieve its results).

Cameron Laird wrote:
>Once I had to maintain lisp code which stored its data in lisp code, too
>(incl. conditions and loops). It was a nightmare.

This is exactly what my Python program does, but I've found it to be a
very powerful and useful technique while remaining relatively easy to
maintain (the generated code doesn't contain any conditionals or loops,
however).  Another nice by-product is that the data stored this way is
portable to different platforms.

-Martin

0
11/8/2005 8:35:04 PM
claird@lairds.us (Cameron Laird) writes:
> In article <pan.2005.11.08.16.21.11.472836@thomas-guettler.de>,
> Thomas Guettler  <niemand.leermann@thomas-guettler.de> wrote:
>>creating source code with a script, is no good solution. 
>>Once I had to maintain lisp code which stored its data in lisp code, too
>>(incl. conditions and loops). It was a nightmare.
> Yes and no.  There are times when it's justified.  I ENTIRELY
> agree, though, that many people who *think* that's what they
> want to do simply don't understand how dynamic base Python is,
> and therefore don't realize how much easier it can be to write
> a single, unified application.

Yup. Python can do a lot of things directly that other languages might
solve with code that writes code. However, that's a *very* powerful
technic, and not everything it does can be done with lesser tools. On
the other hand, it's a *very* powerful technic, and abusing it can
easilyi create unmaintainable code.

> At this point, 'twould be appropriate to describe an instance
> or two in which code generation is a good idea.  While I have
> some, they're tedious to make clear.  Maybe I'll do so in a
> follow-up ...

Since Cameron didn't provide examples, let me grab a simple one. The
cheetah templating system works by creating Python programs from the
template. The programs, when run, output the "filled in" template. The
templates are generally more maintainable than the raw python - even
if you cleaned up all the things Cheetah does to make writing
templates easier. This model makes it possible for Cheetah templates
use inheritance - they can inherit from each other, from python
classes, and python classes can inherit from them.

        <mike
-- 
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
0
mwm (1296)
11/9/2005 1:14:38 AM
In article <86acgeaagx.fsf@bhuda.mired.org>, Mike Meyer  <mwm@mired.org> wrote:
			.
			.
			.
>Since Cameron didn't provide examples, let me grab a simple one. The
>cheetah templating system works by creating Python programs from the
>template. The programs, when run, output the "filled in" template. The
>templates are generally more maintainable than the raw python - even
>if you cleaned up all the things Cheetah does to make writing
>templates easier. This model makes it possible for Cheetah templates
>use inheritance - they can inherit from each other, from python
>classes, and python classes can inherit from them.
			.
			.
			.
Good example.  Excellent one, even, for emphasizing the place
of inheritance in the design.

Functionalism, space-time, security, persistence, duality, ...
I have trouble talking in this area without starting to froth.
An unsatisfying treatment of some of these issues appears in
<URL: http://www.unixreview.com/documents/s=9884/ur0509m/ >.

I'll rein myself in and suggest an even easier introduction
to this subject:  configuration files.  RARELY is the correct
answer to create a new syntax, although many development
organizations give the impression that's their first choice.
".ini"-speak is a safe-enough choice.  Most interesting,
though, is to interpret Python or some subset as a configu-
ration specification, so that one has immediately not just
    HOME = "/some/folder"
    STEP_LIMIT = 16
but 
    pool_size = cpu_count * 30
and even
    if today == "Sunday":
        total_process_maximum = 8
available in the configuration language.  Neat, eh?  

But if configuration is *that* powerful, then it can also
do great damage.  How does one make Python interpretation safe?

That's a subject for another day.
0
claird (2363)
11/9/2005 4:08:02 PM
I also think something along the lines of execfile() may serve the
original poster. There was a  thread last month about compile()
and exec() with a concise example from  Fredrik Lundh.
Google "Changing an AST" in this group.

With dynamically generated code I prefer the separate compile()
step so that I can catch the compile exceptions separately.

0
mrbmahoney (13)
11/9/2005 5:36:10 PM
claird@lairds.us (Cameron Laird) writes:
> I'll rein myself in and suggest an even easier introduction
> to this subject:  configuration files.  RARELY is the correct
> answer to create a new syntax, although many development
> organizations give the impression that's their first choice.
> ".ini"-speak is a safe-enough choice.  Most interesting,
> though, is to interpret Python or some subset as a configu-
> ration specification, so that one has immediately not just
>     HOME = "/some/folder"
>     STEP_LIMIT = 16
> but 
>     pool_size = cpu_count * 30
> and even
>     if today == "Sunday":
>         total_process_maximum = 8
> available in the configuration language.  Neat, eh?  

I once carried this a step further, and used methodless classes as a
configuration mechanism:

class PlainFoo:
      # Values for a plain foo

class FancyFoo(PlainFoo):
      # Overrides for just the things that are different

The program that used this created needed lots of objects, in a
variety of different flavers that were literally specified as "Just
like PlainFoo, but with ...". Doing it this way made configuring
things trivial.

At the time, I attached "configuration variables" to instances. If I
were going to do it today, I'd look into making the parent classes of
the class that implements Foo dynanmic.

plwm (an X11 window manager - sort of - built in top of python-xlib)
carries this another step further. You configure your window manager
by creating a subclass of the WindowManager (or other) class that
mixes in the features you want, and sets the attributes to control
specific features.

It's very flexible - but at this point, the "configuration file" is a
Python program, and not really suitable to use by non-programmers.

> But if configuration is *that* powerful, then it can also
> do great damage.  How does one make Python interpretation safe?
> That's a subject for another day.

We're all waiting for this, somewhat impatiently :-).

      <mike
-- 
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
0
mwm (1296)
11/9/2005 7:54:24 PM
In article <86slu58umn.fsf@bhuda.mired.org>, Mike Meyer  <mwm@mired.org> wrote:
			.
			.
			.
>It's very flexible - but at this point, the "configuration file" is a
>Python program, and not really suitable to use by non-programmers.
			.
			.
			.
Or both.  I have lived through a lot of experiences where
customers think a configuration file has a very simple
syntax--but in case of some puzzle/emergency/challenge/...,
I can tell 'em, "put 'complicated_script_that_I_can_recite_
on_the_telephone' right at the bottom, and tell me what
happens."  That's saved weeks of thrashing around.

I should make that explicit:  application developers, you
don't have to tell customers everything your programs do.
Your obligation is to make 'em meet requirements.  If it
helps *you* that they do more, so be it.
0
claird (2363)
11/9/2005 11:08:03 PM
Cameron Laird wrote:
....
> I should make that explicit:  application developers, you
> don't have to tell customers everything your programs do.
> Your obligation is to make 'em meet requirements.  If it
> helps *you* that they do more, so be it.
I'd agree with the proviso that you at least inform your
customer if you are creating a security hole.

-- 
-Scott David Daniels
scott.daniels@acm.org
0
scott.daniels (1683)
11/11/2005 5:21:52 PM
Reply:

Similar Artilces:

Python mascot? How about a Pythoneer?
I've been thinking that a good Python mascot might help in raising public awareness of Python. The problem with a snake as a mascot is that it is not anthropomorphic enough. An effective mascot has to have something approximating two arms, two legs, and a head, so that he can be put into various interesting situations and adventures. (At the very least, he has to be able to work a keyboard and a mouse!) Good examples can be found in some of the imaginative things that have been done with Linux's Tux. So a snake is out. So are Terry Gilliam's wonderful cartoons for the Flyin...

Embedding Python in Python
Hi, I've been programming in Python for a couple of weeks now and am quite impressed with the language - it was very easy to learn (it actually reminds me a lot of programming in BASIC many many years ago - it's very fast to hack together a couple lines of code in interactive mode). I'm throwing together a simple little game (sort of a MUD-type thing) as my first 'big' project, and I thought it would be cool to be able to script the game in Python. Basically, I want to be able to type scripts in from within the game and attach them to objects, monsters, rooms, whatever. N...

Python script to install python
I would like to have a python script that would download the most recent svn of python, configure, make, install and cleanup after itself. I am not replacing the python version I would be using to run the script. I was struggling to get this to work and I assume someone else has done it better. Any pointers? Thanks Vincent ...

python/python output buffers
Hi, I'm having a little problem while using python mode. I'm used to hit C-c C-c and have a Python Output buffer opened with the output. Now, on Ubuntu 7.10 and 8.04, C-c C-c sends to the Python buffer that is not raised in a window. I need to hit C-c C-z to see it. First of all I'm puzzled and would like to understand what makes the difference. I guess is some defaults that changed in the system configuration. What makes the difference between the two behaviors? Thanks in advance sandro *:-) sandro dentella a �crit : > Hi, > > I'm having a little proble...

python QT or python-GTK
** All the posts found in google are old. I'm assuming new improvements have been made to both IDEs. ** Please correct me if I'm wrong, I'm a newbie. 1. Which one of them requires fewer lines to accomplish the same thing? from what I understand QT it's just like Borland J-Builder. Meaning, you want a button, you draw it, then you double-click on it, a window opens up and you type events and behavior. And with GTK, you just type everything. 2. Which one is cross platform? (Linux, MacOS, Windows,etc). 3. Which one has more widgets? 4. Which one is the easiest to pick up? I...

python-daemon for Python v3
Hi Community Is there ported to Python v3 python-daemon package? https://pypi.python.org/pypi/python-daemon/ i am afraid it is not as simple as correction of relative path input feature and except clauses in mentioned package. Thanks Asaf Asaf Las <roegltd@gmail.com> writes: > Is there ported to Python v3 python-daemon package? > > https://pypi.python.org/pypi/python-daemon/ Have a read through the archives for the ‘python-daemon-devel’ discussion forum <URL:http://lists.alioth.debian.org/mailman/listinfo/python-daemon-devel>, where we have ...

Python from Smalltalk / Smalltalk from Python?
I am currently using a graphical package which is scripted with (integrated with) python. But I'd like to do my programming in smalltalk, thank you very much. How much of a hassle is it to connect python and smalltalk for this sort of thing? Anyone else done it? Is there a recommended approach? Tips? Suggestions? Pitfalls? (I have interfaced with C in the past, relatively painlessly, so I'm not a total newbie with this sort of thing) Thanks. pineapple.link@yahoo.com escreveu: > I am currently using a graphical package which is scripted with > (integrated with...

python 2 to python 3
I am using Wing101 v.5 and it is using Python2, but I want to make it = use Python3 instead because need Python3 for a uni lab. How do I = change it?= Audrey McFarlane wrote: > I am using Wing101 v.5 and it is using Python2, but I want to make it use > Python3 instead because need Python3 for a uni lab. How do I change it? I'm afraid I don't use Wing so I can't give a good answer, but I googled and found this: http://stackoverflow.com/questions/25318792/how-do-i-configure-wing101-to-run-python-3-3 It's not very helpful, but it might point you in th...

Subsets of Python implemented in Python
I don't understand why some parts of the Python language (or the Python standard library too) are implemented in C while some other parts are implemented in the Python language itself. For instance, lists and dictionnaries are implemented in C but sets are not. Wouldn't be better to implement all in C (for efficiency reasons for example) ? On Jul 17, 12:34=A0am, candide <cand...@free.invalid> wrote: > I don't understand why some parts of the Python language (or the Python > standard library too) are implemented in C while some other parts are > implemented in the Python language itself. For instance, lists and > dictionnaries are implemented in C but sets are not. > > Wouldn't be better to implement all in C (for efficiency reasons for > example) ? CPython's sets are implemented in C. Old versions of Python implemented sets in the 'set' module which was written in Python but that has been deprecated and removed. A lot of the standard library is implemented in Python because it makes it more easily portable to non- CPython implementations. Chard. On Sat, 17 Jul 2010 01:34:48 +0200, candide wrote: > I don't understand why some parts of the Python language (or the Python > standard library too) are implemented in C while some other parts are > implemented in the Python language itself. For instance, lists and > dictionnaries are implemented in C but sets are not. > ...

python-list@python.org
Has anyone got a simple python script that will parse a linux mbox and create a large file to view . I need to write a python program the grabs mail from a standard linux mail folder and processes it to file to eventually be transfered to a database. Im having problems at the moment can someone post a exmaple or no if a similar script exists to hack. Not to good with Python and I have'nt got alot of time to get to grips with writing my own. Cheers Chuck ...

Python daemonisation with python-daemon
Hello there, I currently have a little project which involve the writing of a server that launch tasks when requested by the user (through a web interface for instance). I would like to turn my server script into a Linux/Unix daemon (launched at boot time by init, dunno if that matter) using the nice python-daemon package by Ben Finley et al (http://pypi.python.org/pypi/ python-daemon/). This package comes with a class DaemonRunner that seems to fit almost exactly my need but I still have some interrogation. My main class looks like the following: class SIGTERM_Received(Except...

ActivePython or Python from Python.org
Hi everyone. I noticed that ActiveState has their own variation of Python and was curious if there's a benefit to choosing their version over the version offered via Python.org. Could someone lend some insight? Thanks, Rey... Rey Bango wrote: > Hi everyone. I noticed that ActiveState has their own variation of > Python and was curious if there's a benefit to choosing their version > over the version offered via Python.org. > > Could someone lend some insight? Here is some reasoning I wrote a while back: http://mail.python.org/pipermail/python-list/2007-July/447987...

Embedding Python in Python #2
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to give them access to modify pieces of my classes. I'd like to disallow access to pretty much the rest of the modules. Any ideas/examples? -Robey You probably want something like this: globalDict = {} exec(stringOfPythonCodeFromUser, globalDict) globalDict is now the global namespace of whatever was in stringOfPythonCodeFromUser, so you can grab values from that and selectivly im...

Using Python To Launch Python
Hello All, I have a situation where I can count on a Python installation being available on a system, but I can't count on it being a version of Python needed by my application. Since my application has it's own version of Python installed with it how should I use the system Python to launch the version of Python that launches my Application. Yes, this is a convoluted process, but not all Pythons are built the same :) Right now I am leaning towards using exec to start a new process, but I thought I would check to see if anyone else has had the need to perform a task similar to thi...

How to run python without python
Is there a way to developing a script on linux and give it to someone on microsoft, so that they could run it on microsoft without installing python? Wayne ...

embedding python in python #3
Hi, anyone had any experiences in embedding python in python? I've tried to do this but it doesn't work. eval("from Tkinter import *") Thanks maurice Use exec. On Wed, Sep 29, 2004 at 09:23:28AM +0000, Maurice LING wrote: > Hi, > > anyone had any experiences in embedding python in python? > > I've tried to do this but it doesn't work. > > eval("from Tkinter import *") Maurice LING <mauriceling@acm.org> wrote in message news:<415a7f0b$1@news.unimelb.edu.au>... > Hi, > > anyone had any experiences in embeddi...

python
hi, is there any possibility to include python in spss versions prior to 14???. flo On Jun 25, 11:13 am, "stat.flo" <stat....@googlemail.com> wrote: > hi, > > is there any possibility to include python in spss versions prior to > 14???. > > flo Sorry, this is not possible. The Python integration work goes deep into the innards of SPSS and took quite a lot of engineering work to build. Regards, Jon Peck On 25 Jun., 23:52, JKPeck <JKP...@gmail.com> wrote: > On Jun 25, 11:13 am, "stat.flo" <stat....@googlemail.com> wrote: > > > hi, > > > is there any possibility to include python in spss versions prior to > > 14???. > > > flo > > Sorry, this is not possible. The Python integration work goes deep > into the innards of SPSS and took quite a lot of engineering work to > build. > > Regards, > Jon Peck What a shame! But Thanks a lot. Flo ...

Python
Is anyone working with the Risc OS version of Python, and particularly with the interface to the WIMP? I am having some difficulty, and would like to hear from someone who has made it work. -- __ __ __ __ __ ___ _____________________________________________ |__||__)/ __/ \|\ ||_ | / Acorn StrongArm Risc_PC | || \\__/\__/| \||__ | /...Internet access for all Acorn RISC machines ___________________________/ dhwild@argonet.co.uk In message <4c85aed9a7dhwild@argonet.co.uk> David H Wild <dhwild@argonet.co.uk> wrote: > Is anyone working with the Risc ...

Python
Stupid me! I was updating to the latest version and noticed there were quite a few older versions on the system so I deleted them, only to discover that OSX expects a particular version. I have a huge variety to choose from, so what do I need for 10.8? Further, since I am going to be moving to 10.9, what do I need for that. I updated one computer to 10.9 and found there were still several versions installed. I would like to know what I can delete without blowing up the system. Robert Peirce <bob@peirce-family.com> wrote: > Stupid me! I was updating to the latest...

Python
I am shuffling my way through 'Python in easy steps' I have reached the section on 'Classes' and cannot make the tutorials work. I have tried several variations on the attached code without success. The first program Bird.py which sets up the class does not give any errors but running instance.py always errors. 'Bird has no attribute _doc_' is an example of the first error. Can anyone through any light/knowledge my way since I am getting frustrated. Code below. Malcolm Smith #!/C:\python3 #Bird.py # Declare a new class with descriptive string ...

python
What computer science concepts can I learn from Java and not from Python? you got us. programming is programming. for one thing - there are more newsgroups for java. but Python is easier - and with Jython you can compile it to bytecode - and use servlets like java. its a good language. but go ahead and learn both - "Demo" <demobudda@yahoo.com> wrote in message news:6712b7e.0409102200.7df6f821@posting.google.com... > What computer science concepts can I learn from Java and not from Python? --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus ...

Python
You can run Python from the Windows Command Prompt and Matlab can send commands to the Windows Command Prompt. So far so good... But when I try and run Python from Matlab behaving as the Windows command prompt, nothing happens - where am I going wrong? Many thanks Leo Leo, if you have trouble implementing something that you got as an answer to your posting, it's definitely better to continue the thread instead of staring a new one. That way, other people can see what's been done to solve the problem. Also, the guy who spend some time to help you does not feel ignored. The reply ...

Python
To what extent are APLers aware of Python's array oriented capability? My group is in the process of comparing various languages in their ability to represent a generic filter estimation problem in missile defense. I was surprised to see my APL2 version translated to Python in almost line-for-line fashion, with very similar execution efficiency. What are Python's pros and cons generally? Thanks. On Aug 25, 7:15=A0am, Jack <jgr...@comcast.net> wrote: > To what extent are APLers aware of Python's array oriented > capability? I can't talk about...

python
I am handled a Python script to re-use a part of it. I have to options. Modify Python script and and catch its STDOUT or write the whole code as a Perl module. Is there any other way to include Python code on a Perl script; On Friday, September 20, 2013 11:31:32 AM UTC-4, George Mpouras wrote: > I am handled a Python script to re-use a part of it. Have you tried writing a Perl script that calls the Python script? Does the Python script return data values? Perhaps it writes to an output f= ile that you can open and munge in Perl. You can also try writing a shell script that (1) runs the Python script, (2= ) saves the output in a particular place, and (3) runs the Perl script that= accepts the output as input. I find that Perl and Python are good at different things. Ideally, you shou= ld use the same language for the same task, just as a matter of sanity, rew= riting as necessary in the language you choose. However, I don't object to = having a process that uses different languages to handle different parts, a= nd I have some processes that use Perl, ColdFusion, Visual Basic, and even = Transact/SQL (Microsoft DTS) for different parts. CC. Στις 20/9/2013 22:04, ο/η ccc31807 έγραψε: > On Friday, September 20, 2013 11:31:32 AM UTC-4, George Mpouras wrote: >> I am handled a Python script to re-use a part of it. > > Have you tried writing a Perl script that calls the Python script? > > Does the Python script retur...

Web resources about - Invoking Python from Python - comp.lang.python

Invoking the power... - Flickr - Photo Sharing!
Strobist: gridspot on face, one flash behind model on a low stand, hairlight on top of the rack camera right (snooted but still visible, removed ...

Senate Republican Leaders Push a Yes Vote On Invoking Cloture on House Bill - YouTube
Cruz: A vote for cloture is a vote for Obamacare http://www.politico.com/blogs/politico-live/2013/09/cruz-a-vote-for-cloture-is-a-vote-for-o ...

Prosecutor: French teacher lied about being attacked by masked assailant invoking Islamic State - CTV ...
A preschool teacher who claimed to have been attacked by a masked assailant invoking Islamic State made up the story, French prosecutors said ...

Cop To Cameraman: ‘If You’re Invoking Your Rights, You Must Be Doing Something Wrong’
America in the 21st Century is not as up on the Bill of Rights as you'd hope.

Alabama judges are invoking Jim Crow laws to avoid issuing marriage licenses
In case there was any confusion about the fact that social conservatives’ massive resistance to same-sex marriage is a direct descendant of their ...

Invoking The 14th
Hertzberg hopes Obama will go there: In the end, Obama could have no honorable choice but to invoke the Fourteenth. There is little doubt that ...

Autodesk sued for $10 million after invoking DMCA to stop eBay resales
A Seattle man has filed suit against Autodesk, the creators of AutoCAD, for …

Invoking the Coase Theorem for Fracking Noise
From Kevin Williamson's excellent article in National Review " The Truth About Fracking ": "There are other workaday environmental problems ...

How I solved my problem with pushup avoidance by invoking Peter Drucker
Long story short: a couple months ago my son-in-law — listening to me lament how my upper body isn't nearly as strong as it used to be and is ...

Hillary Clinton Defends Ties to Wall Street by Invoking 9/11
Democratic presidential candidate Hillary Clinton defended her ties to Wall Street by invoking the September 11, 2001 terror attack.

Resources last updated: 3/10/2016 2:16:27 AM