1

I know we can import modules and just embed Python code in C++ and evaluate it. But how can I use built-in functions like print or open? These functions off course aren't module. Evaluating embedded open statement just gives me the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'open' is not defined

Stuck. Please help me.

1
  • 1
    you can import builtins, no idea if this help though... Commented Nov 2, 2018 at 11:21

1 Answer 1

1

Try importing the builtins and io module and if you want any other function just call the __module__ attribute to find about which module to import

>>> print.__module__
'builtins'
>>> open.__module__
'io'
Sign up to request clarification or add additional context in comments.

5 Comments

Are you sure? how will open.__module__ work if it is showing a NameError.
@scipsycho i dont get it where it is showing a name error? and i used python3 for this
You are correct @AlbinPaul but all I was saying is that the guy who asked this question is having a NameError, so how will he be able to run open.__module__
@scipsycho ok the function open is defined in the module io in python3 and builtin in python2 which are imported by default in a program so to avoide the name error in c++ the OP must import the builtin module or io module to get it running
Thanks @AlbinPaul! In my environment, open seems to live in __builtin__, though. Anyway, by importing __builtin__ module, my problem has been resolved.

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.