pyrfc - SAP NetWeaver RFC Bindings

raw JSON →
3.3.1 verified Fri May 01 auth: no python

pyrfc provides Python bindings for SAP NetWeaver RFC SDK, enabling remote function calls to SAP systems. Version 3.3.1 supports Python >=3.8. Active maintenance with periodic releases.

pip install pyrfc
error pyrfc._pyrfc.ConnectionError: Module sapnwrfc.so not found
cause SAP NetWeaver RFC SDK (librfccm.so / sapnwrfc.dll) is not installed or not on the library path.
fix
Install SAP NW RFC SDK (available from SAP) and set LD_LIBRARY_PATH (Linux) or PATH (Windows) to include the SDK's lib folder.
error ImportError: cannot import name 'Connection' from 'pyrfc'
cause User attempts to import from old package pattern (e.g., import pyrfc and then pyrfc.Connection) but module structure changed.
fix
Use 'from pyrfc import Connection' (note the capital C).
error RuntimeError: GC caused segfault in pyrfc. Connection was garbage collected while still being used by another thread.
cause As explained in warnings: Python garbage collector releases the native SDK handle while still in use.
fix
Ensure conn.close() is called before discarding the reference, and don't share connections across threads without proper synchronization.
error pyrfc._pyrfc.ConnectionError: RFC_INVALID_PARAMETER (rc=4)
cause Invalid or missing connection parameters (host, sysid, client, user, passwd).
fix
Double-check all connection parameters. Ensure ASHOST is correct and the SAP gateway is accessible.
breaking pyrfc 2.0 introduced Connection class replacing direct module functions. Old code using pyrfc.connect() will break.
fix Use from pyrfc import Connection and instantiate Connection object.
gotcha GC side-effect: assigning a Connection instance to a local variable and letting it go out of scope may cause segfault (the C extension destructor triggers GC of the native SDK).
fix Explicitly call conn.close() or use a context manager if available (not yet built-in in 3.x). Keep a reference to the connection until done.
gotcha Password with special characters (especially single quotes) can cause syntax errors or connection failures.
fix Use raw strings or escape quotes properly. Pass password via environment variable to avoid shell interpolation.
deprecated pyrfc 2.x deprecated ssl parameters (e.g., ssl_sni, ssl_sni_disable) in favor of a unified saprfc ini file approach.
fix Use saprfc.ini configuration file for SSL settings. See pyrfc documentation for details.

Connect to SAP, call RFC_READ_TABLE, and print results.

from pyrfc import Connection

conn = Connection(
    ashost='<host>',
    sysid='<sysid>',
    client='<client>',
    user=os.environ.get('SAP_USER', '<user>'),
    passwd=os.environ.get('SAP_PASS', '<password>'),
    lang='EN'
)

result = conn.call('RFC_READ_TABLE', QUERY_TABLE='T001', ROWCOUNT=10)
for row in result['DATA']:
    print(row['WA'])

conn.close()