473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Importing modules in embedded Python

Hello everyone,

I can embed Python interpreter in C code now, but now there's another
problem, importing modules in Python code doesn't work:

Exception exceptions.ImportError:
'/usr/lib/python2.4/lib-dynload/timemodule.so: undefined symbol:
PyModule_AddObject' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Aborted

The (autogenerated) C code:
---cut---
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>

void userbreak(int sig)
{
Py_Finalize();
printf("Interrupted..\n");
exit(sig);
}

void terminaterun(int sig)
{
Py_Finalize();
printf("Received SIGTERM. Terminating.\n");
exit(sig);
}

int main(int argc, char **argv)
{
Py_Initialize();
PyObject *pyCode, *mainmodule, *maindict;
mainmodule = PyImport_AddModule("__main__");
maindict = PyModule_GetDict(mainmodule);
unsigned int size = 272;

unsigned char python_code[] = { 0x63,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x40,0x00,0x00,0x00,0x73,0x16,0x00,0x00,0x00,0x64,
0x00,0x00,0x6B,0x00,0x00,0x5A,0x00,0x00,0x64,0x01,
0x00,0x84,0x00,0x00,0x5A,0x01,0x00,0x64,0x00,0x00,
0x53,0x28,0x02,0x00,0x00,0x00,0x4E,0x63,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
0x43,0x00,0x00,0x00,0x73,0x23,0x00,0x00,0x00,0x74,
0x00,0x00,0x69,0x01,0x00,0x64,0x01,0x00,0x83,0x01,
0x00,0x01,0x64,0x02,0x00,0x47,0x48,0x74,0x00,0x00,
0x69,0x01,0x00,0x64,0x03,0x00,0x83,0x01,0x00,0x01,
0x64,0x00,0x00,0x53,0x28,0x04,0x00,0x00,0x00,0x4E,
0x69,0x01,0x00,0x00,0x00,0x74,0x07,0x00,0x00,0x00,
0x73,0x75,0x63,0x63,0x65,0x73,0x73,0x69,0x5A,0x00,
0x00,0x00,0x28,0x02,0x00,0x00,0x00,0x74,0x04,0x00,
0x00,0x00,0x74,0x69,0x6D,0x65,0x74,0x05,0x00,0x00,
0x00,0x73,0x6C,0x65,0x65,0x70,0x28,0x00,0x00,0x00,
0x00,0x28,0x00,0x00,0x00,0x00,0x28,0x00,0x00,0x00,
0x00,0x74,0x07,0x00,0x00,0x00,0x74,0x65,0x73,0x74,
0x2E,0x70,0x79,0x74,0x04,0x00,0x00,0x00,0x74,0x65,
0x73,0x74,0x04,0x00,0x00,0x00,0x73,0x06,0x00,0x00,
0x00,0x00,0x01,0x0D,0x01,0x05,0x01,0x28,0x02,0x00,
0x00,0x00,0x52,0x01,0x00,0x00,0x00,0x52,0x04,0x00,
0x00,0x00,0x28,0x02,0x00,0x00,0x00,0x52,0x04,0x00,
0x00,0x00,0x52,0x01,0x00,0x00,0x00,0x28,0x00,0x00,
0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x52,0x03,0x00,
0x00,0x00,0x74,0x01,0x00,0x00,0x00,0x3F,0x02,0x00,
0x00,0x00,0x73,0x02,0x00,0x00,0x00,0x09,0x02 };

pyCode = PyMarshal_ReadObjectFromString((PyObject *)
python_code, size);

(void) signal(SIGINT, userbreak);
(void) signal(SIGTERM, terminaterun);
PyEval_EvalCode(pyCode, maindict, maindict);

Py_Finalize();

}
---cut---

The Python code:

import time

def test():
time.sleep(1)
print "success"
time.sleep(90)
How do I manage to get modules imported? Anybody?

Jul 2 '08 #1
0 879

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
3
by: stefan | last post by:
Hi Folks, I currenty extended some of my C++ functionality to python and also embedded python to use python functionality in my C++ system (and use as well these extended functions). While...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
0
by: Brandon Metcalf | last post by:
I come from a Perl and C background and have been given an application written in Python to maintain and I know very little about Python. I'm having trouble at run time with importing modules. ...
6
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
0
by: Jure Vrscaj | last post by:
Hi, as title implies, I wrote a simple module that allows importing modules or even packages via http, conceptually similar to zipimport (of which I learned about during the process of writing...
0
Bulldog
by: Bulldog | last post by:
I have been playing around with this issue for a while and seen some previous posting trying to address the problem but I haven't seen any answers to the problem so I am reposting it in my quest for...
0
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing...
7
by: Hussein B | last post by:
Hey, Suppose I have a Python application consists of many modules (lets say it is a Django application). If all the modules files are importing sys module, how many times the sys module will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.