2

I tried to import a file var.py to openfile.py. var is passed as an argument to openfile. I am unable to figure out how to import it by reading the name from command line.

var = sys.argv[1]
importlib.import_module(var, package=None)

I tried the above code but it gave the below error

Traceback (most recent call last):
  File "true.py", line 7, in <module>
    importlib.import_module(var, package=None)
NameError: name 'importlib' is not defined
1
  • Shuldn't you import importlib itself? Commented Jul 2, 2014 at 8:38

2 Answers 2

1

you are missing importlib module.

Thats why you are getting NameError: name 'importlib' is not defined

do import importlib

Note:

If imporlib is not installed, you can install it using easy_install/pip on *nix:

easy_install importlib

Sign up to request clarification or add additional context in comments.

1 Comment

This version of the answer is much better than the original one that simply recommended installing with easy_install importlib ;-)
0

You have to import the importlib module first:

import importlib
....
importlib.import_module(...)

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.