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


Groups > comp.lang.python > #26694

Re: find out whether a module exists (without importing it)

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'exec': 0.07; 'try:': 0.07; 'cmd': 0.09; 'subject:module': 0.09; 'wrong,': 0.09; 'aug': 0.13; '6:00': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:exists': 0.16; 'wrote:': 0.17; 'received:209.85.214.174': 0.21; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; "i'm": 0.29; 'print': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'subject: (': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'skip:a 30': 0.60; 'here': 0.65; 'gelonida': 0.84; 'subject:find': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=fIxmd9LK9nGXBMRKd7DVlj+L+tz5K3ukH051OWe2zCA=; b=e4GFL2hzbO0bxw+GOuoQN11lnAOuhz2xgesW9l7eKHOSGlqRl9yUn5+qshJuIMgpVD Kvz4Lv3GgI9zObk8cHn1tcrm4pW+fhEgBjGirGlTlOks1IootdeNwJC3vAl8O4jJVuS+ mnou80mEJsVdhQh//BrnJypl/kATbmdG8vncSWckBi431z38gumbeAvPJQ8pns/ZVBPa YHzYEdVr7SW01ways+u1y04ha5r6vNSOhB81N1CfkPAiKi41PwD/VRbiAFxsesixbl8H Ct+QISK7uY6weRbDCC5TqTCfy6lHdx9KB7qduo0lUxBfyqoM2WorUMkQmG6/amxLXMmE H2oQ==
MIME-Version 1.0
In-Reply-To <5020CB10.2020608@gmail.com>
References <jvpaid$9ia$1@dough.gmane.org> <20120807064331.GA31774@terra.cms.at> <5020CB10.2020608@gmail.com>
Date Tue, 7 Aug 2012 19:08:08 +1000
Subject Re: find out whether a module exists (without importing it)
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.3047.1344330491.4697.python-list@python.org> (permalink)
Lines 13
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1344330491 news.xs4all.nl 6984 [2001:888:2000:d::a6]:44403
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26694

Show key headers only | View raw


On Tue, Aug 7, 2012 at 6:00 PM, Gelonida N <gelonida@gmail.com> wrote:
> modulename = 'my.module'
> cmd = 'import %s as amodule'
> try:
>     exec(cmd)
>     print "imported successfully"

Someone will doubtless correct me if I'm wrong, but I think you can
avoid exec here with:

amodule=__import__(modulename)

ChrisA

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


Thread

Re: find out whether a module exists (without importing it) Chris Angelico <rosuav@gmail.com> - 2012-08-07 19:08 +1000

csiph-web