ceorron
ceorron

Reputation: 1268

Embedding python error Import by filename is not supported

I'm trying to embed python in to my application and have got stuck pretty early on.

I am embedding python into my C++ application and using the code found at this tutorial: http://docs.python.org/2/extending/embedding.html#pure-embedding

My application matches entirely and compiles successfully no errors. However on running the application pModule = PyImport_Import(pName); line fails returning 0 meaning I get error output from PyErr_Print() of

Failed to load "C:\Users\workspace\dpllib\pyscript.py"
ImportError: Import by filename is not supported.

The application is being called with the commands C:\Users\workspace\ndnlib\pyscript.py multiply 50 150

Upvotes: 7

Views: 8884

Answers (2)

Alexander.Iljushkin
Alexander.Iljushkin

Reputation: 4576

It is simplier if you create your file as a module.

For instance,

create this:

<project>/MyModule/__init__.py

Then run your file <project>/script.py

dyn_module_name = (... get module name 'MyModule' from console arguments ...)
my_dynamic_module = __import__(dyn_module_name)

Since it would be a module, it will load while your scripy.py execution

Upvotes: 0

Sepand
Sepand

Reputation: 166

I can't be sure, but I'm thinking that since pName is set to argv[1] and you're using the full path to call the script, then argv[1] is the full path. This means the code would try to import "C:\Users\workspace\dpllib\pyscript.py", which python can't (it can only import "pyscript").

Try running the script by just typing "pyscript.py" from within the directory and see if the error changes to 'Failed to load "pyscript.py"'. If it does, then you have to fix it so it doesn't just import argv[1] and modifies the string to get a module name instead of a file name.

Upvotes: 7

Related Questions