Skip to content

Instantly share code, notes, and snippets.

@pavan538
Last active February 15, 2018 10:16
Show Gist options
  • Save pavan538/7d86b92fd20be8e76506e9203e987952 to your computer and use it in GitHub Desktop.
Save pavan538/7d86b92fd20be8e76506e9203e987952 to your computer and use it in GitHub Desktop.
import python modules dynamically
"""
The built-in __import__ function accomplishes the same goal as using the import statement,
but it's an actual function, and it takes a string as an argument.
"""
sys = __import__('sys')
os = __import__('os')
re = __import__('re')
unittest = __import__('unittest')
moduleNames = ['sys', 'os', 're', 'unittest']
modules = map(__import__, moduleNames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment