4
PyRun_String("random.randint(1,10)", Py_eval_input, globals, globals);

returns error with:

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

earlier in the code, I did:

PyImport_ImportModule("random");

I guess this is not the way to get it work. What is the correct way? Thank you!

1 Answer 1

3

PyImport_ImportModule returns the imported value. You need to save it in globals under the name random. In summary:

PyMapping_SetItemString(globals, "random", PyImport_ImportModule("random"));

but don't forget to also check the result of the import in case it throws an exception.

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.