Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'importing': 0.04; 'importerror:': 0.05; 'try:': 0.07; "'.'": 0.09; "'default'": 0.09; 'cmd': 0.09; 'does,': 0.09; 'exists,': 0.09; 'imported': 0.09; 'subject:module': 0.09; 'cc:addr:python- list': 0.10; 'stored': 0.10; 'properly': 0.15; '"import': 0.16; 'helps.': 0.16; 'michael,': 0.16; "module's": 0.16; 'subject:exists': 0.16; 'sys.path': 0.16; 'wrote:': 0.17; 'exists': 0.17; 'tries': 0.17; 'code,': 0.18; 'module': 0.19; 'variable': 0.20; 'question.': 0.20; 'suggested': 0.20; 'import': 0.21; 'context.': 0.22; 'exists.': 0.22; 'produces': 0.22; "i'd": 0.22; 'received:74.125.82.174': 0.23; 'cc:2**0': 0.23; 'errors': 0.23; 'raise': 0.24; 'somewhere': 0.24; 'thus': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'question': 0.27; 'necessary.': 0.27; "doesn't": 0.28; 'correct': 0.28; 'existence': 0.29; 'url:mailman': 0.29; 'located': 0.31; '(and': 0.32; 'url:python': 0.32; "skip:' 20": 0.32; 'running': 0.32; 'could': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'received:74.125.82': 0.33; 'much.': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'exist': 0.35; 'fail': 0.35; 'doing': 0.35; 'received:192.168.0': 0.35; 'sometimes': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'michael': 0.36; 'but': 0.36; 'message- id:@gmail.com': 0.36; 'received:74.125': 0.36; 'url:org': 0.36; 'modules': 0.36; "didn't": 0.36; 'anything': 0.36; 'too': 0.36; 'subject: (': 0.36; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'why': 0.37; 'moment': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'some': 0.38; 'possible.': 0.38; 'received:192': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'containing': 0.61; 'side': 0.61; 'capable': 0.63; 'more': 0.63; 'want,': 0.65; 'distinguish': 0.84; 'gelonida': 0.84; 'it"': 0.84; 'subject:find': 0.84; 'received:192.168.0.5': 0.91; 'to:none': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:cc:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=z7n22RQwplnhhf2pK9ZerNlrAn9Wo6Djm6T9MIO//rk=; b=YSLlUZQVBaYcCYp6vUN9eHxJamMAvt+uv87FZbXY/92IJqNmiq4TdzGcYfpajfKg32 xvk/bXarw7ZIratDOhvAwRs4cyUWuPTPU+e1GIaJOIA9nrEPMLjgza0B4jqNmO3QExmf feKN+JtWkkaMqYjYsrk96FYR1oBQvIsyDb/6Mi1gUOGQAn64rya3MwpLsrWyeA3BhSGy GeKQrYC6SfFjwKoUUOmJKHHbWPxsITs7HOZpYwD/PCJacgcyTenV/sRPv9WI7azSWO+J jOX8Lz2ZdCtX+Or/rwgT7o3OWmoMV2o865oue7dQ16iQqr0cao9Yfc8Mz5p0FpLVFYh7 Hflg== Date: Tue, 07 Aug 2012 10:00:16 +0200 From: Gelonida N User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 CC: python-list@python.org Subject: Re: find out whether a module exists (without importing it) References: <20120807064331.GA31774@terra.cms.at> In-Reply-To: <20120807064331.GA31774@terra.cms.at> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344326425 news.xs4all.nl 6871 [2001:888:2000:d::a6]:52506 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26691 Hi Michael, On 08/07/2012 08:43 AM, Michael Poeltl wrote: > in my opinion, "without importing it" makes it unnecessarily complicated. It does, but I think this is what I want, thus my question. I tried to keep my question simple without explaining too much. Well now here's a little more context. There's two reasons why I sepcified the without importing it. Some modules may have side effects when being imported,and sometimes I just want to check for a module's existence Second: Sometimes I only want to know, whether a module exists. I do not want to know whether a module is syntactically correct or whether a module if imported is capable of importing all it's submodules What I'd like to achieve at the moment is to distinguish three situations: - a module with a given name does not exist - a module with a given name exists and produces errors (might be ImportErors) - a module with a given name exists and can be imported In fact what I really want to achieve is: import a module if it exists (and fail if it is broken) if it doesn't exist import a 'default' module and go on. The name of the module is stored in a variable and is not known prior to running the script so the code, that you suggested would be something like. modulename = 'my.module' cmd = 'import %s as amodule' try: exec(cmd) print "imported successfully" except ImportError: print "module doesn't exist or the module tries to " \ "import another module that doesn't exist" # if the module doesn't exist I'd like to import a 'fallback' module # otherwise I'd like to abort. except Exception as exc: print "module exists, but is broken" raise exc amodule.do_something() > You just want to know it module xyz exists, or better said can be found > (sys.path). > > why not try - except[ - else ] > > try: > import mymodule > except ImportError: > # NOW YOU KNOW it does not exist > #+ and you may react properly > ?? > * Gelonida N [2012-08-06 22:49]: >> Is this possible. >> >> let's say I'd like to know whether I could import the module >> 'mypackage.mymodule', meaning, >> whther this module is located somewhere in sys.path >> >> i tried to use >> >> imp.find_module(), but >> it didn't find any module name containing a '.' >> >> Am I doing anything wrong? >> >> Is there another existing implementation, that helps. >> >> I could do this manually, but this is something I'd just like to do >> if necessary. >> >> >> -- >> http://mail.python.org/mailman/listinfo/python-list