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


Groups > comp.lang.python > #41106

Re: Running external module and accessing the created objects

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"as': 0.07; '-1)': 0.07; '[],': 0.07; 'attributes': 0.07; 'imported': 0.09; 'imports': 0.09; 'script,': 0.09; 'subject:module': 0.09; 'subtle': 0.09; 'stored': 0.10; 'suggest': 0.11; '(like': 0.15; "hasn't": 0.15; '"module"': 0.16; 'globals(),': 0.16; 'hierarchy': 0.16; 'sorts': 0.16; 'subject:accessing': 0.16; 'subject:created': 0.16; 'thread.': 0.16; 'wrote:': 0.17; 'circular': 0.17; 'module,': 0.17; 'script.': 0.17; 'solution.': 0.18; 'load': 0.19; 'module': 0.19; 'define': 0.20; 'import': 0.21; 'earlier': 0.21; 'defined': 0.22; 'example': 0.23; "i've": 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'tree': 0.27; 'run': 0.28; '>>>>': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'code': 0.31; 'url:python': 0.32; 'generally': 0.32; 'defining': 0.33; 'instances': 0.33; 'strict': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; "can't": 0.34; 'ahead': 0.35; 'pm,': 0.35; 'but': 0.36; 'url:org': 0.36; 'loaded': 0.36; 'url:library': 0.36; 'should': 0.36; 'problems': 0.36; 'does': 0.37; 'two': 0.37; 'usual': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'end': 0.40; 'your': 0.60; 'most': 0.61; "you'll": 0.62; 'different': 0.63; 'ever': 0.63; 'more': 0.63; 'dangerous': 0.66; 'hesitate': 0.66; 'received:74.208': 0.71; 'implications': 0.84; 'received:74.208.4.194': 0.84; 'url:functions': 0.84; 'tricky': 0.91; 'angel': 0.93
Date Mon, 11 Mar 2013 22:11:24 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3
MIME-Version 1.0
To python-list@python.org
Subject Re: Running external module and accessing the created objects
References <mailman.3110.1362798411.2939.python-list@python.org> <513aecd3$0$6512$c3e8da3$5496439d@news.astraweb.com> <khlr16$lr0$1@ger.gmane.org> <513E7B6B.5070608@davea.name> <khm025$1hl$1@ger.gmane.org>
In-Reply-To <khm025$1hl$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:fg5F9o7PgwgLmXeV1jGAvo8yIX+SwjpP0alVMa/QXwX LzKLGcvF7sQjwei+sIe171Ry4tUjcIoMnoycC9kVmRP7sbxbUm JLwHm3sdCr7AC6UPK9AeeN54ZbidNUAgruoc+tmVZHliRyvigN ZK3UjrG1P6AW4mM6R58C3X6ECABYV5QX8PxwxMKAqBaGykX4ZD 1JMIMwWNTQ0NQts7INVSm8CAILGGhzLhLiGM9Ja73CRmUvCa50 Fons2XOXDs2wZfdRtBlCRdYH2S7c74dQmtK3nwgj7jgXLvhHIq K7s+xT9QONAk881mMBbXHGP+5KLCfjLcNPKYQztFaenFoQcEA= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3216.1363054307.2939.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363054307 news.xs4all.nl 6857 [2001:888:2000:d::a6]:44656
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41106

Show key headers only | View raw


On 03/11/2013 09:23 PM, Kene Meniru wrote:
> Dave Angel wrote:
>
>> On 03/11/2013 07:57 PM, Kene Meniru wrote:
>
>>
>> I hope you're just kidding.  execfile() and exec() are two of the most
>> dangerous mechanisms around.  import or __import__() would be much
>> better, as long as your user hasn't already run myapp.py as his script.
>>
>
> It does what I want. Security is another issue and I understand but can't
> help it until I find another solution.
>
> import does not do what I want.
>
> How does __import__() work and what do you mean "as long as your user hasn't
> already run myapp.py as his script."?
>

The __import__() function is defined
    http://docs.python.org/2/library/functions.html#__import__


appname = "myapp"
usermodule = __import__(appname, globals(), locals(), [], -1)

And now you can use usermodule as though you had imported it in the 
usual way.

As for my other caveat, I've said it before in this thread.  Make sure 
you don't ever load a module by more than one name, or you'll end up 
with a mess.  And that includes the original script, which is loaded by 
the name '__main__'

You also should avoid any circular import, as it can be very tricky to 
deal with them.

As I said earlier in the thread:

 >>>>
 >>>> For example the __import__() function can import a module that you
 >>>> don't know the name of ahead of time.  It's not often the right
 >>>> answer, though, so I hesitate to suggest it.
 >>>>
 >>>> For another example, if you import a module by two different names,
 >>>> or if you import the "module" that is your starting script, then
 >>>> you can end up with two instances of such a module, with all sorts
 >>>> of negative implications about global data or class attributes
 >>>> stored in that module, or even more subtle problems.
 >>>>
 >>>> For a final example, having a circular import tree can cause
 >>>> problems if any of those imports have any global code (like class
 >>>> initialization, defining constants, etc.).  It's generally much
 >>>> better to define a strict hierarchy of who imports whom.



-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Running external module and accessing the created objects  Kene Meniru <Kene.Meniru@illom.org> - 2013-03-08 22:06 -0500
  Re: Running external module and accessing the created objects Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-09 08:03 +0000
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 06:05 -0500
      Re: Running external module and accessing the created objects Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-09 11:34 +0000
    Re: Running external module and accessing the created objects Chris Angelico <rosuav@gmail.com> - 2013-03-09 22:47 +1100
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-09 07:02 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 10:34 -0500
      Re: Running external module and accessing the created objects Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-09 08:51 -0800
        Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 12:21 -0500
          Re: Running external module and accessing the created objects Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-09 10:33 -0800
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-09 11:12 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 11:56 -0500
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-09 12:20 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 12:39 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-09 13:18 -0500
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-11 19:57 -0400
      Re: Running external module and accessing the created objects Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-11 23:40 -0700
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-11 20:48 -0400
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-11 21:23 -0400
    Re: Running external module and accessing the created objects Kene Meniru <Kene.Meniru@illom.org> - 2013-03-11 21:58 -0400
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-11 22:16 -0400
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-11 22:11 -0400
    Re: Running external module and accessing the created objects Michael Torrie <torriem@gmail.com> - 2013-03-11 22:05 -0600
    Re: Running external module and accessing the created objects Dave Angel <davea@davea.name> - 2013-03-12 07:20 -0400
    Re: Running external module and accessing the created objects Kene Meniru <kemeniru@gmail.com> - 2013-03-12 13:38 +0000
    Re: Running external module and accessing the created objects Kene Meniru <kemeniru@gmail.com> - 2013-03-12 13:28 +0000

csiph-web