ImportError in embedded Python Interpreter

  • Thread starter googler.1.webmaster
  • Start date
G

googler.1.webmaster

Hi,

i have a problem. I compiled Python and the socket module so I got
this structure. (all on windows)

C:\test\dll_files\python25.dll
C:\test\my_app
C:\test\dll_files\DLLs\
C:\test\dll_files\python.exe

If I run python.exe I get the console and I can call "import socket"
which
succeeds. I wrote a small console app which is stored in C:\test\ and
embeddeds the python interpreter. I set the environment variable PATH
so the python25.dll can be load.

But when I want to import the socket module in a script, it fails.
(The interpreter works fine, just importing files in the DLLs folder
fail). Why? When I print out the sys.path in my app I get the same
like in the
real python.exe.

What do you think, whats missing?

Thanks a lot. bye :)
 
G

googler.1.webmaster

Hi!

Okay, I checkede Py_Main(...) and called some python code there. There
it works too. So I know whats missing.

sys.environ.. returns nothing.
How can I set the paths with the Python C API?

Thanks.
 
G

Gabriel Genellina

i have a problem. I compiled Python and the socket module so I got
this structure. (all on windows)

C:\test\dll_files\python25.dll
C:\test\my_app
C:\test\dll_files\DLLs\
C:\test\dll_files\python.exe

If I run python.exe I get the console and I can call "import socket"
which
succeeds. I wrote a small console app which is stored in C:\test\ and
embeddeds the python interpreter. I set the environment variable PATH
so the python25.dll can be load.

The easiest way is to mirror the directory structure of a standard Python
install. Python looks for Lib/os.py and, once found, derives all the other
paths from here. See PC/getpathp.c in the source distribution.

You have these alternatives:

- put a lib\* containing at least os.py below the directory containing
your main executable.

- set a PYTHONHOME variable

- call Py_SetProgramName

- completely replace getpathp.c with your own functions
 
G

googler.1.webmaster

Hi!


Thanks. Well, os.py is found and all the others which don't need a
library.
I tested this:

I execute Py_Main(...) in my app which executes the console
interpreter and
i tried to execute "import socket" which works.

So Py_Main has something what my created PyRun_SimpleString doesn't
have.
Maybe the environment variables? Something has to be called in Py_Main
which I should call, too.


bye..
 
G

Gabriel Genellina

Thanks. Well, os.py is found and all the others which don't need a
library.
I tested this:

I execute Py_Main(...) in my app which executes the console
interpreter and
i tried to execute "import socket" which works.

So Py_Main has something what my created PyRun_SimpleString doesn't
have.
Maybe the environment variables? Something has to be called in Py_Main
which I should call, too.

(I assume you moved the executable to another directory - in your first
post, your app was in some place, and python.exe/python25.dll in another)

Py_Main, apart from processing arguments and some stuff related to the
interactive mode, doesn't do much; a stripped down version would be:

Py_SetProgramName(argv[0]);
Py_Initialize();
PySys_SetArgv(...);
PyRun_AnyFileExFlags(fd, filename, ...)
WaitForThreadShutdown();
Py_Finalize();
 
G

googler.1.webmaster

Hi!

Okay, thats just the question. I did that what you wrote but it
doesn't really works.
What is, if Py_SetProgramName() gets a NULL Pointer, if argv[0] is
empty?

Well, the problem is, in my opinion that os.environ returns some paths
in python.exe
and in my embedded interpreter if I call os.environ in Py_Main(...)

When I call it in my PyRun_SimpleString(...) it returns nothing.
Hm.. thats really strange.
 
G

Gabriel Genellina

Okay, thats just the question. I did that what you wrote but it
doesn't really works.
What is, if Py_SetProgramName() gets a NULL Pointer, if argv[0] is
empty?

Why so? Are you in an embedded environment or something that doesn't have
a filesystem? Calling Py_SetProgramName with an absolute path would be the
easiest way to make it work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top