SAP Connection failed: name 'Connection' is not defined, PYRFC in django
I'm experiencing an issue with the pyrfc library in my Django project running on Pythonanywhere server. Specifically, I am trying to use the Connection class from pyrfc to establish a connection to an SAP system, but I am encountering an ImportError when I try to import Connection in my views.py file. The error message says:
SAP Connection failed: name 'Connection' is not defined
However, when I test the same code in the Django shell, everything works fine, and the Connection class is correctly imported. I have verified that the environment variables SAP_NWRFC_HOME and LD_LIBRARY_PATH are set correctly, and the libsapnwrfc.so library loads successfully.
If i do make import like
from pyrfc import Connection then it gives the error
2025-01-22 07:28:52,598: Error running WSGI application
2025-01-22 07:28:52,599: ImportError: cannot import name 'Connection' from 'pyrfc' (/home/moeez007/.local/lib/python3.10/site-packages/pyrfc/__init__.py)
2025-01-22 07:28:52,599: File "/var/www/moeez007_pythonanywhere_com_wsgi.py", line 80, in <module>
2025-01-22 07:28:52,600: from pyrfc import Connection
i also have tried to set the environment variables in the wsgi file
import os
os.environ["SAP_NWRFC_HOME"] = "/home/moeez007/nwrfcsdk"
os.environ["LD_LIBRARY_PATH"] = "/home/moeez007/nwrfcsdk/lib"
but still the same, i also have tried to run this as a separate file but still the same
why is it working in the django shell and python but not in the view??
this is the code for my connection view
import pyrfc
from pyrfc import *
logger = logging.getLogger(__name__)
def sap_conn_test(request):
if request.method == "GET":
try:
print(os.environ.get("SAP_NWRFC_HOME"))
print(os.environ.get("LD_LIBRARY_PATH"))
sap_conn = XPL_SAP_CON.objects.first()
conn = Connection(
user=sap_conn.user_name,
passwd=sap_conn.password,
ashost=sap_conn.server_address,
sysnr=sap_conn.system_number,
client=sap_conn.client_number,
)
response = {
"message": "SAP Connection successful!",
"status": "success"
}
except Exception as e:
response = {
"message": f"SAP Connection failed: {e}",
"status": "error"
}
return JsonResponse(response)
return JsonResponse({"message": "Invalid request method.", "status": "error"})