Announcement

Collapse
No announcement yet.

Embed python

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embed python

    Simple example using pbcc shown below. Make sure your exe can see the python25.dll. I'm having a problem with raw_input that doesn't allow input. Any suggestions?

    Code:
    #DIM ALL
    DECLARE SUB   pyIni LIB "python25.dll" ALIAS "Py_Initialize"
    DECLARE SUB   pyFin LIB "python25.dll" ALIAS "Py_Finalize"
    declare function pyRun lib "python25.dll" ALIAS "PyRun_SimpleString" (s as asciiz) as integer
    FUNCTION PBMAIN()
     
     STDOUT "Initiate python"
     pyIni
     pyRun("print '>>> hello from python <<<'")
     pyRun("import sys")
     pyRun("print '>>> modules: <<<'")
     pyRun("print sys.builtin_module_names")
     pyRun("print '>>> module keys: <<<'")
     pyRun("print sys.modules.keys()") 
    ' PyRun("execfile('foo.py')")
     STDOUT "Finalize python"
     pyFin
    END FUNCTION
    #if 0
    'Contents of foo.py
    name = raw_input('What is your name?\n') # \n is a newline 
    print 'Hi', name
    #endif

  • #2
    Hi Andre,

    I do not have PBCC, could try only alternative of your code in thinBASIC ... where it worked. Do you have latest Python 2.5.1 ?
    Maybe you should alocate console using API and not PB default ( maybe the same, not sure ).

    Do you get any error message or the console hangs waiting for input?

    Listing I got:
    Code:
    Initiate python>>> hello from python <<<
    >>> modules: <<<
    ('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs
    _hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_csv', '_fun
    ctools', '_heapq', '_hotshot', '_locale', '_lsprof', '_md5', '_multibytecodec',
    '_random', '_sha', '_sha256', '_sha512', '_sre', '_struct', '_subprocess', '_sym
    table', '_types', '_weakref', '_winreg', 'array', 'audioop', 'binascii', 'cPickl
    e', 'cStringIO', 'cmath', 'collections', 'datetime', 'errno', 'exceptions', 'gc'
    , 'imageop', 'imp', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'ope
    rator', 'parser', 'rgbimg', 'signal', 'strop', 'sys', 'thread', 'time', 'xxsubty
    pe', 'zipimport', 'zlib')
    >>> module keys: <<<
    ['copy_reg', 'sre_compile', 'locale', '_sre', '__main__', 'site', '__builtin__',
     'encodings.cp1250', 'operator', 'encodings', 'os.path', 'encodings.encodings',
    'encodings.codecs', 'sre_constants', 're', 'ntpath', 'UserDict', 'nt', 'stat', '
    zipimport', 'warnings', 'encodings.types', '_codecs', 'sys', 'codecs', 'types',
    '_types', '_locale', 'signal', 'linecache', 'encodings.aliases', 'exceptions', '
    sre_parse', 'os']
    What is your name?
    [I]Petr or maybe not?[/I]
    Hi Petr or maybe not?
    Finalize python
    My input is in italics.

    In case it does not work maybe you could use something else that Python, like PBLua or Bint32.
    Or wait for Python 3000

    Bye,
    Petr
    Last edited by Petr Schreiber jr; 11 Mar 2008, 02:49 PM.
    [email protected]

    Comment


    • #3
      Thanks Petr

      Console under pbwin 8 using win32api didn't work either. It does work under c I've included the translated code below for those wanting to test.

      Code:
      //gcc -o cpy.exe cpy.c -lpython25
      #include "c:\python25\include\python.h"
      int main() {
       printf("Initiate python");
       Py_Initialize();
       PyRun_SimpleString("print '>>> hello from python <<<'");
       PyRun_SimpleString("import sys");
       PyRun_SimpleString("print '>>> modules: <<<'");
       PyRun_SimpleString("print sys.builtin_module_names");
       PyRun_SimpleString("print '>>> module keys: <<<'");
       PyRun_SimpleString("print sys.modules.keys()");
       PyRun_SimpleString("execfile('foo.py')");
       printf("Finalize python");
       Py_Finalize();
       return 0;
      }

      Comment

      Working...
      X