Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37396 > unrolled thread

Failed to import a "pyd: File When python intepreter embed in C++ project

Started byliujz39@gmail.com
First post2013-01-22 18:43 -0800
Last post2013-01-23 18:43 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Failed to import a "pyd: File When python intepreter embed in C++ project liujz39@gmail.com - 2013-01-22 18:43 -0800
    Re: Failed to import a "pyd: File When python intepreter embed in C++ project Chris Angelico <rosuav@gmail.com> - 2013-01-23 15:49 +1100
      Re: Failed to import a "pyd: File When python intepreter embed in C++ project liujz39@gmail.com - 2013-01-22 23:05 -0800
    RE: Failed to import a "pyd: File When python intepreter embed in C++ project "Leonard, Arah" <Arah.Leonard@bruker-axs.com> - 2013-01-23 16:28 +0000
      Re: Failed to import a "pyd: File When python intepreter embed in C++ project Junze Liu <liujz39@gmail.com> - 2013-01-23 18:43 -0800
      Re: Failed to import a "pyd: File When python intepreter embed in C++ project Junze Liu <liujz39@gmail.com> - 2013-01-23 18:43 -0800

#37396 — Failed to import a "pyd: File When python intepreter embed in C++ project

Fromliujz39@gmail.com
Date2013-01-22 18:43 -0800
SubjectFailed to import a "pyd: File When python intepreter embed in C++ project
Message-ID<552da0d2-e744-4ce2-8204-14a7274515de@googlegroups.com>
I create a pyd File named "testPyd" with boostPython,and then I import the testPyd module into "test.py", it works perfect!
But when I embeded the python interpreter into my C++ project and run the "test.py", it comes out a "ImportErr: no module named testPyd".
It has confused me for two days and I googled for long time,but I can't find the answer!
Anybody here can help me ?
Thank you!

[toc] | [next] | [standalone]


#37413

FromChris Angelico <rosuav@gmail.com>
Date2013-01-23 15:49 +1100
Message-ID<mailman.863.1358916592.2939.python-list@python.org>
In reply to#37396
On Wed, Jan 23, 2013 at 1:43 PM,  <liujz39@gmail.com> wrote:
> I create a pyd File named "testPyd" with boostPython,and then I import the testPyd module into "test.py", it works perfect!
> But when I embeded the python interpreter into my C++ project and run the "test.py", it comes out a "ImportErr: no module named testPyd".
> It has confused me for two days and I googled for long time,but I can't find the answer!
> Anybody here can help me ?
> Thank you!

First thing you should do is to get a minimal example that
demonstrates the problem, and *copy and paste* it and the exception
traceback. When your own spelling is as sloppy as this, we need to see
Python's accuracies to have any chance of being able to help you.
Plus, the process of shrinking your example to the minimum often
highlights the actual cause of the problem. Even if it doesn't, it'll
certainly be easier for us to help you if it's a small, self-contained
script than if it's huge.

http://sscce.org/

ChrisA

[toc] | [prev] | [next] | [standalone]


#37419

Fromliujz39@gmail.com
Date2013-01-22 23:05 -0800
Message-ID<10549479-c8fa-47a9-9511-9f1454b9c9ca@googlegroups.com>
In reply to#37413
On Wednesday, January 23, 2013 12:49:43 PM UTC+8, Chris Angelico wrote:
>
> 
> > Thank you!
> 
> 
> 
> First thing you should do is to get a minimal example that
> 
> demonstrates the problem, and *copy and paste* it and the exception
> 
> traceback. When your own spelling is as sloppy as this, we need to see
> 
> Python's accuracies to have any chance of being able to help you.
> 
> Plus, the process of shrinking your example to the minimum often
> 
> highlights the actual cause of the problem. Even if it doesn't, it'll
> 
> certainly be easier for us to help you if it's a small, self-contained
> 
> script than if it's huge.
> 
> 
> 
> ChrisA

Thanks for your attention and sorry for the sloppy of my spelling!!!
Here is my test examples:
The C++ codes to build the *pyd File* is:
Excute.h

#ifndef EXCUTE_H_
#define EXCUTE_H_
#include <string>

class Excute
{
public:
	Excute(){}
	int getIoReuslt(std::string ioStr);
	int getSignal();
};
#endif

Excute.cpp:
#include "Excute.h"
#include "Explanation.h"
#include <boost/python.hpp>

int Excute::getIoReuslt(std::string ioStr)
{
	return 1;
}

int Excute::getSignal()
{
	int i = rand()%2;
	return i;
}

BOOST_PYTHON_MODULE(pythonDll)
{
	using namespace boost::python;
	class_<Excute>("Excute", init<>())
		.def("getIoResult", &Excute::getIoReuslt)
		.def("getSignal", &Excute::getSignal)
		;
}
Then a pyd File Named pythonDll is created(pythonDll.pyd),
Here are the codes in my test.py:
test.py:

from pythonDll import*
h=Excurte()
print h.getIoResult("a")
print h.getSignal()

And this script works perfect in IDLE.

And then I embed python into my C++ project,Here are the test codes:
#include <Python.h>
#include <string>

int main(int argc, char* argv[])
{
	Py_Initialize();
        FILE * fp = fopen("$PATH/test.py", "r");
	if (fp == NULL)
	{
	    return 1;
	}
	PyRun_SimpleString("execfile($PATH/test.py')");
	Py_Finalize();

	system("Pause");

	return 0;
}

The result is:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "$PATH/test.py", line 1, in <module>
    from pythonDll import*
ImportError: No module named pythonDll
[19228 refs]

It bored me for a couple of days!If you get any ways to solve this ,please let me know!And thank you again!

[toc] | [prev] | [next] | [standalone]


#37488

From"Leonard, Arah" <Arah.Leonard@bruker-axs.com>
Date2013-01-23 16:28 +0000
Message-ID<mailman.908.1358958543.2939.python-list@python.org>
In reply to#37396
> I create a pyd File named "testPyd" with boostPython,and then I import the testPyd module into "test.py", it works perfect!
> But when I embeded the python interpreter into my C++ project and run the "test.py", it comes out a "ImportErr: no module named testPyd".
> It has confused me for two days and I googled for long time,but I can't find the answer!
> Anybody here can help me ?
> Thank you!
> 

	Ah, that sounds familiar.  I have a small bit of experience with Boost.  I could be wrong, because once you start mixing it up with Boost all sorts of weird things can happen (especially on a MS compiler, because no one tests for Windows, let alone a pay-for compiler) but my experience has shown that if you get that specific import error, what it actually means is just that the PYD import failed for ANY reason.  It has nothing to do with the name or that the PYD couldn't be found.  Just that somewhere, at some time during import, it failed to fully load.

	In my use a lot of times it was either that a DLL that module depended on wasn't in the path, or my favorite kicker, that some compile/link flags between the PYD and the Python interpreter don't match well enough.  (Usually from mixing debug and release builds together.)

	From what I've seen anyway, the Python interpreter really doesn't like being built in a traditional debug mode, so I always do a release build of it.  It's a little inconvenient, but in the linker flags you can still set your PYDs to generate debug information even in release builds, so you can still run the debugger on them when you attach to the process of the python interpreter EXE.  And especially be sure to use the RELEASE runtime library flag (such as /MD) instead of the debug flag (such as /MDd).

	That's as much as I know anyway.  Though depending, if you add any new templates/libraries into Boost (such as for NumPy ndarray), you also may need to use the /DBOOST_ALL_NO_LIB compiler macro on an MS compiler because MS doesn't adhere to template standards correctly and you often end up with multiply-defined functions if you don't use that macro.  If I remember correctly.  (It's been a while.)

	That and you may be picking up variable length arrays out of your teeth, replacing chunks of code with the use of new and delete operators.  No one tests for Microsoft and the MS compiler is way behind in adhering to C/C++ standards, and VLAs pop up a lot.

	Hopefully something in all of this helped.  Boost can be ... daunting.  I get it, in theory.  But in practice it often hurts my head.  ;)

Sincerely,
Arah Leonard

[toc] | [prev] | [next] | [standalone]


#37531

FromJunze Liu <liujz39@gmail.com>
Date2013-01-23 18:43 -0800
Message-ID<e367f973-0b38-42c8-af82-cb4236f15d91@googlegroups.com>
In reply to#37488
On Thursday, January 24, 2013 12:28:58 AM UTC+8, Leonard, Arah wrote:
> > I create a pyd File named "testPyd" with boostPython,and then I import the testPyd module into "test.py", it works perfect!
> 
> > But when I embeded the python interpreter into my C++ project and run the "test.py", it comes out a "ImportErr: no module named testPyd".
> 
> > It has confused me for two days and I googled for long time,but I can't find the answer!
> 
> > Anybody here can help me ?
> 
> > Thank you!
> 
> > 
> 
> 
> 
> 	Ah, that sounds familiar.  I have a small bit of experience with Boost.  I could be wrong, because once you start mixing it up with Boost all sorts of weird things can happen (especially on a MS compiler, because no one tests for Windows, let alone a pay-for compiler) but my experience has shown that if you get that specific import error, what it actually means is just that the PYD import failed for ANY reason.  It has nothing to do with the name or that the PYD couldn't be found.  Just that somewhere, at some time during import, it failed to fully load.
> 
> 
> 
> 	In my use a lot of times it was either that a DLL that module depended on wasn't in the path, or my favorite kicker, that some compile/link flags between the PYD and the Python interpreter don't match well enough.  (Usually from mixing debug and release builds together.)
> 
> 
> 
> 	From what I've seen anyway, the Python interpreter really doesn't like being built in a traditional debug mode, so I always do a release build of it.  It's a little inconvenient, but in the linker flags you can still set your PYDs to generate debug information even in release builds, so you can still run the debugger on them when you attach to the process of the python interpreter EXE.  And especially be sure to use the RELEASE runtime library flag (such as /MD) instead of the debug flag (such as /MDd).
> 
> 
> 
> 	That's as much as I know anyway.  Though depending, if you add any new templates/libraries into Boost (such as for NumPy ndarray), you also may need to use the /DBOOST_ALL_NO_LIB compiler macro on an MS compiler because MS doesn't adhere to template standards correctly and you often end up with multiply-defined functions if you don't use that macro.  If I remember correctly.  (It's been a while.)
> 
> 
> 
> 	That and you may be picking up variable length arrays out of your teeth, replacing chunks of code with the use of new and delete operators.  No one tests for Microsoft and the MS compiler is way behind in adhering to C/C++ standards, and VLAs pop up a lot.
> 
> 
> 
> 	Hopefully something in all of this helped.  Boost can be ... daunting.  I get it, in theory.  But in practice it often hurts my head.  ;)
> 
> 
> 
> Sincerely,
> 
> Arah Leonard

Dear Lenoard,
    Thanks for your hearty assistance, and it finally works.I just change my RELEASE Run Time Library flag to MD, then the results come out.
    Before I receive your mail, I googled for kinds of solutions and tried to solve this problem, but none of them works.Yesterday, someones said that the python module in *pyd Files* should be initialized before they can be embed into C++, and a function named init<ModuleName> should be called before importing the module, I was confused with this for a couple of hours until I saw your mail.
   I am new to boost.python.If I propose any questions that seems naive to you the other days, any suggestion from you would be more than welcome!  
   Thanks again for your help to make me out of my wrong way!

Sincerely,
George Liu

[toc] | [prev] | [next] | [standalone]


#37532

FromJunze Liu <liujz39@gmail.com>
Date2013-01-23 18:43 -0800
Message-ID<mailman.940.1358995439.2939.python-list@python.org>
In reply to#37488
On Thursday, January 24, 2013 12:28:58 AM UTC+8, Leonard, Arah wrote:
> > I create a pyd File named "testPyd" with boostPython,and then I import the testPyd module into "test.py", it works perfect!
> 
> > But when I embeded the python interpreter into my C++ project and run the "test.py", it comes out a "ImportErr: no module named testPyd".
> 
> > It has confused me for two days and I googled for long time,but I can't find the answer!
> 
> > Anybody here can help me ?
> 
> > Thank you!
> 
> > 
> 
> 
> 
> 	Ah, that sounds familiar.  I have a small bit of experience with Boost.  I could be wrong, because once you start mixing it up with Boost all sorts of weird things can happen (especially on a MS compiler, because no one tests for Windows, let alone a pay-for compiler) but my experience has shown that if you get that specific import error, what it actually means is just that the PYD import failed for ANY reason.  It has nothing to do with the name or that the PYD couldn't be found.  Just that somewhere, at some time during import, it failed to fully load.
> 
> 
> 
> 	In my use a lot of times it was either that a DLL that module depended on wasn't in the path, or my favorite kicker, that some compile/link flags between the PYD and the Python interpreter don't match well enough.  (Usually from mixing debug and release builds together.)
> 
> 
> 
> 	From what I've seen anyway, the Python interpreter really doesn't like being built in a traditional debug mode, so I always do a release build of it.  It's a little inconvenient, but in the linker flags you can still set your PYDs to generate debug information even in release builds, so you can still run the debugger on them when you attach to the process of the python interpreter EXE.  And especially be sure to use the RELEASE runtime library flag (such as /MD) instead of the debug flag (such as /MDd).
> 
> 
> 
> 	That's as much as I know anyway.  Though depending, if you add any new templates/libraries into Boost (such as for NumPy ndarray), you also may need to use the /DBOOST_ALL_NO_LIB compiler macro on an MS compiler because MS doesn't adhere to template standards correctly and you often end up with multiply-defined functions if you don't use that macro.  If I remember correctly.  (It's been a while.)
> 
> 
> 
> 	That and you may be picking up variable length arrays out of your teeth, replacing chunks of code with the use of new and delete operators.  No one tests for Microsoft and the MS compiler is way behind in adhering to C/C++ standards, and VLAs pop up a lot.
> 
> 
> 
> 	Hopefully something in all of this helped.  Boost can be ... daunting.  I get it, in theory.  But in practice it often hurts my head.  ;)
> 
> 
> 
> Sincerely,
> 
> Arah Leonard

Dear Lenoard,
    Thanks for your hearty assistance, and it finally works.I just change my RELEASE Run Time Library flag to MD, then the results come out.
    Before I receive your mail, I googled for kinds of solutions and tried to solve this problem, but none of them works.Yesterday, someones said that the python module in *pyd Files* should be initialized before they can be embed into C++, and a function named init<ModuleName> should be called before importing the module, I was confused with this for a couple of hours until I saw your mail.
   I am new to boost.python.If I propose any questions that seems naive to you the other days, any suggestion from you would be more than welcome!  
   Thanks again for your help to make me out of my wrong way!

Sincerely,
George Liu

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web