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

Discussion in 'Python' started by Chris Angelico, Aug 7, 2012.

  1. On Tue, Aug 7, 2012 at 6:00 PM, Gelonida N <> 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
     
    Chris Angelico, Aug 7, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plb
    Replies:
    2
    Views:
    354
  2. Gelonida N
    Replies:
    3
    Views:
    154
    Gelonida N
    Aug 6, 2012
  3. Michael Poeltl
    Replies:
    0
    Views:
    141
    Michael Poeltl
    Aug 7, 2012
  4. Gelonida N
    Replies:
    0
    Views:
    146
    Gelonida N
    Aug 7, 2012
  5. Peter Otten
    Replies:
    0
    Views:
    140
    Peter Otten
    Aug 7, 2012
Loading...

Share This Page