1

Im trying to import a set of modules that are defined by an array. The reason I would like to store them in an array is because I would like to change this list dynamically.

moduleNames = ['sys', 'os']
import moduleNames

However this doesnt work.

I have searched around and found this:

moduleNames = ['sys', 'os']
modules = map(__inport__, moduleNames)

This imports the modules however I would like to be able to access it by doing sys.version for example. Rather than by using modules[0].version

Is this currently possible I can't seem to find anywhere that shows you how to do it.

2
  • Your rationale make no sense. How does the rest of your code know that a module named sys was imported (so it knows to use sys.version)? Commented Aug 5, 2015 at 21:41
  • I don't know that's why I asked the question. Its now resolved so don't worry about my rationale. Commented Aug 5, 2015 at 21:44

1 Answer 1

4

How about

for m in modules:
    globals()[m] = __import__(m)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.