473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

howto check does module 'asdf' exist? (is available for import)

howto check does module 'asdf' exist (is available for import) or no?
(without try/cache of course)
Thx in advance, D.

May 21 '07 #1
8 3736
dmitrey wrote:
howto check does module 'asdf' exist (is available for import) or no?
Walk through sys.path and find it yourself?

(without try/cache of course)
Why is the obvious (and most common) try/import/catch solution "of course" out?

Stefan
May 21 '07 #2
js
Why not just use try?
Trying to import a module is a python idiom.
http://www.diveintopython.org/file_handling/index.html

On 21 May 2007 06:17:16 -0700, dmitrey <op*****@ukr.netwrote:
howto check does module 'asdf' exist (is available for import) or no?
(without try/cache of course)
Thx in advance, D.

--
http://mail.python.org/mailman/listinfo/python-list
May 21 '07 #3
On May 21, 11:17 pm, dmitrey <open...@ukr.netwrote:
howto check does module 'asdf' exist (is available for import) or no?
try :
import asdf
del asdf
except ImportError :
#do stuff ...
(without try/cache of course)
Oops sorry, you didn't want it the obvious way ... but why ever not?

May 22 '07 #4
On May 21, 11:17 pm, dmitrey <open...@ukr.netwrote:
howto check does module 'asdf' exist (is available for import) or no?
try :
import asdf
del asdf
except ImportError :
print "module asdf not available"
else :
print "module asdf available for loading"

You can generalise this, but at the expense of a couple of exec
statements:
def is_module_available (module) :
try :
exec('import %s' % module)
exec('del %s' % module)
except ImportError :
return False
else :
return True
(without try/cache of course)
Oops sorry, you wanted it done in some non-obvious way! Why?!

May 22 '07 #5
On Tue, 2007-05-22 at 00:27 -0700, Asun Friere wrote:
You can generalise this, but at the expense of a couple of exec
statements:
def is_module_available (module) :
try :
exec('import %s' % module)
exec('del %s' % module)
except ImportError :
return False
else :
return True
You can save the exec statement by using the built-in __import__
function:

def is_module_available(modulename):
try: mod = __import__(modulename)
except ImportError: return False
else: return True

--
Carsten Haese
http://informixdb.sourceforge.net
May 22 '07 #6
Asun Friere <af*****@yahoo.co.ukwrites:
howto check does module 'asdf' exist (is available for import) or no?
try :
import asdf
del asdf
except ImportError :
print "module asdf not available"
else :
print "module asdf available for loading"
But this has a side effect: if asdf is already loaded, it deletes it.
May 22 '07 #7
I think that's there because you just wanted to check if it was
available for import, implying that you didn't actually want to import
it right then.

On 22 May 2007 09:09:02 -0700, Paul Rubin
<"http://phr.cx"@nospam.invalidwrote:
Asun Friere <af*****@yahoo.co.ukwrites:
howto check does module 'asdf' exist (is available for import) or no?
try :
import asdf
del asdf
except ImportError :
print "module asdf not available"
else :
print "module asdf available for loading"

But this has a side effect: if asdf is already loaded, it deletes it.
--
http://mail.python.org/mailman/listinfo/python-list
May 23 '07 #8
kaens <ap***************@gmail.comwrites:
I think that's there because you just wanted to check if it was
available for import, implying that you didn't actually want to import
it right then.
Whether it's available for import and whether you've already imported
it are two separate questions. Maybe you said "import fghj as asdf"
and now you want to find out if there's another module actually named
asdf. It would be cleaner if there's a method with no side effects.
May 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adam | last post by:
hello What query shoul I send to SQL serwer ( in transact SQL language ) to check does some database exist on serwer ? It similar to problem "does some table exist in database" - resolve to it...
2
by: msnews.microsoft.com | last post by:
Hi, My C# application used a COM. When I deploy my application, how to detect if the target machine has installed the COM? Thanks, Max
4
by: Digital Fart | last post by:
howto make a connection to database available in my classes. What is the best practice when i want to write classes that need a connection to the database? Do i make a conn variable in my...
3
by: dmitrey | last post by:
hi all, howto check is object Arg1 - a func, lambda-func - something else? I tried callable(Arg1), but callable(lambda-func) returnes False Thx, D.
2
by: joemo2003 | last post by:
How to use VBA to check does a reference library exist? In my case, some computer have visio installed, but some don't. So i want use VBA to check does that visio reference exist on that computer. ...
2
by: dmitrey | last post by:
my Python module was changed in HDD (hardware disk drive), moreover, changed its location (but still present in sys.path). how can I reload a func "myfunc" from the module? (or howto reload whole...
2
ADezii
by: ADezii | last post by:
Many times when writing VBA code, we find the need to work with Files in one form or another. Whatever the process may be, we need to be sure that a File is present in a specified location. One...
1
by: dmitrey | last post by:
hi all, howto check is function capable of obtaining **kwargs? i.e. I have some funcs like def myfunc(a,b,c,...):... some like def myfunc(a,b,c,...,*args):... some like
2
by: Kless | last post by:
How to check is a library/module is installed on the system? I use the next code but it's possivle that there is a best way. ------------------- try: import foo foo_loaded = True except...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.