SAP / PyRFC

Asynchronous, non-blocking SAP NW RFC SDK bindings for Python
http://sap.github.io/PyRFC
Apache License 2.0
511 stars 140 forks source link

Pyrfc- not able to import Connection from pyrfc #178

Closed Piyush068-png closed 4 years ago

Piyush068-png commented 4 years ago

ImportError: cannot import name 'Connection' from 'pyrfc' . Can you help me with this issue i am stuck in this issue from long,i am using python 3 and nwrfcsdk installation s showing fine in cmd when we execute rfcexec in cmd. from pyrfc import * is working fine. But, Connection I am not able to import een using cmd.

bsrdjan commented 4 years ago

Could you please provide some more info:

1) Operating system? 2) Python version (python -V output) 3) pynwrfc version (pip freeze) and how it is installed, like by pip install, build from source ... 4) Complete error log after from pyrfc import Connection

Piyush068-png commented 4 years ago

1.Operating System - Windows 10 2.Python version (python -V output) - Python 3.8.1 3.I installed it using pip install pyrfc in command prompt. pynwrfc==2.0.5 and NWRFCSDK Version - 7.50 64 bit(downloaded from SAP Marketplace) 4.Complete error log - ========================================================================= RESTART: D:/saper.py ======================================================================== Traceback (most recent call last): File "D:/saper.py", line 1, in from pyrfc import Connection ImportError: cannot import name 'Connection' from 'pyrfc' (C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pyrfc__init__.py)

Other Details:

Getting same error using command prompt too. So, please help me as I won't be able to proceed without this. Trying this since yesterday

Path of lib -C:\Users\Admin\AppData\Local\Programs\Python\Python38\nwrfcsdk\lib(added in same path variable where python and pip path is given in user variable for admin)

SAPNWRFC_HOME = C:\Users\Admin\AppData\Local\Programs\Python\Python38\nwrfcsdk(added both in user variable for admin and system variable too).

Piyush068-png commented 4 years ago

Please let me know if you can help me with the issue.

amarvin commented 4 years ago

Try pip install pynwrfc. I think you installed the wrong module.

Piyush068-png commented 4 years ago

https://sap.github.io/PyRFC/install.html As per this documentation we need to do pip install pyrfc for python 3. But, still I uninstalled pyrfc and tried pip install pynwrfc. Now, I am getting following error:

Traceback (most recent call last): File "D:/saper.py", line 1, in from pyrfc import Connection File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pyrfc__init__.py", line 39, in from .pyrfc import ( ImportError: DLL load failed while importing pyrfc: The specified module could not be found.

I think pip install pyrfc is only correct for python 3.8 In code too we use from pyrfc import Connection

Piyush068-png commented 4 years ago

Please let me know if you can help me with the issue.

Piyush068-png commented 4 years ago

nwrfcsdk is properly installed:

C:\Users\Admin\AppData\Local\Programs\Python\Python38\nwrfcsdk\bin>rfcexec Error: Not all mandatory parameters specified Please start the program in the following way: rfcexec -t -a -g -x -f -s The options "-t" (trace), "-f" and "-s" are optional.

Below further optional parameters are listed. You can find their documentation in sapnwrfc.ini: -on_cce <0, 1, 2> (On Character Conversion Error) -cfit (Conversion Fault Indicator Token - the substitute symbol used if on_cce=2) -keepalive (Sets the keepalive option. Default is 0.) -delta <0, 1> (default 1, i.e. use delta-manager) -no_compression (table compression, default is 0, i.e. compression is on)

bsrdjan commented 4 years ago

The documentation refers to old setup method and requires the pyrfc download from GitHub, before the pip install:

wget https://github.com/SAP/PyRFC/releases/download/2.0.0/pyrfc-2.0.0-cp38-cp38-win_amd64.whl

pip install pyrfc-1.9.97-cp37-cp37m-macosx_10_14_x86_64.whl

It will be updated.

The installation works like described in README, just as @amarvin mentioned.

The pyrfc is another PyPI package, has nothing to do with RFC communication: https://pypi.org/project/pyrfc/

The correct module is pynwrfc: https://pypi.org/project/pynwrfc/

Please try to completely remove pyrfc and install pynwrfc, something like:

$ pip uninstall pyrfc
$ pip install pynwrfc

After de-installation, please ensure no pyrfc folder exists on site packages location. The location of site packages folder you may find by:

$ python -m site
# ends with something like: lib/python3.8/site-packages
Piyush068-png commented 4 years ago

Hi, thanks for your quick response and providing updated information. I have uninstalled pyrfc and also made sure that no pyrfc folder exists on site packages location.

Now, I installed pyrfc using pip install pynwrfc.

I have tried on two system one with Microsoft visual C++ 2010 redistributable(64) and Microsoft visual C++ 2013 redistributable(64) respectively.

But, still I am getting issue:

Traceback (most recent call last): File "D:/saper.py", line 1, in from pyrfc import Connection File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pyrfc__init__.py", line 39, in from .pyrfc import ( ImportError: DLL load failed while importing pyrfc: The specified module could not be found.

I have googled the issue but all my path are set properly. I have given the path of lib directory in Path variable where Python and PIP path is given.

And C:\Users\Admin\AppData\Local\Programs\Python\Python38\nwrfcsdk in SAPNWRFC_HOME variable. Also my all Windows, Python and SAP NWRFC SDK 7.50 PL6 are of 64 bit. Also, visual studio code is installed in my system.

But, still I am facing issue and I guess earlier also few people faced this issue of failing of DLL load. So, please let me know if any possible solution for this issue. I am getting same error in both systems. I have checked the documentation for 1 possible listed issues but it didn't helped me lib directory of the C connector is added in Path as I mentioned above and also on executing rfcexec in command prompt gives me same message as shown in documentation

Piyush068-png commented 4 years ago

I think i need to install older version of pynwrfc? As, some users has commented in other issues, that they installed older versions of pynwrfc and their issue was solved. I saw @amarvin also commenting in some issues bout installing some older version of pynwrfc? I too tried the same but I was getting error when I tried to install older version or command other than pip install pynwrfc.

Actually I want to fix as I mentioned in previous comment: ImportError: DLL load failed while importing pyrfc: The specified module could not be found.

bsrdjan commented 4 years ago

DLL load fails when NWRFCSDK libs or their runtime dependencies are not found.

VS 2013 C++ redistributable must be installed, following README Windows prerequisites and SAP OSS note mentioned there.

The nwrfcsdk/lib folder must be on user or system PATH, for Python < 3.8.

Could you please check if both these prerequisites met ?

Piyush068-png commented 4 years ago

VC 2013 C++ is installed in my system.I have checked in control panel in all programs. Also when I execute rfcexec in cmd, I get same message as documentation and I have shown you in second comment. I am using Python 3.8.1 Also nwrfcsdk/lib folder is added in system path.

So, these prequisites are met. And I am using Windows 10, so, I guess SAP OSS note doesn't applies to me. Also I don't have SAP ID to view SAP OSS note.

File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\pyrfc__init__.py", line 39, in from .pyrfc import ( ImportError: DLL load failed while importing pyrfc: The specified module could not be found.

Also this error seems to be arises from init.py in pyrfc.

bsrdjan commented 4 years ago

Please find here the details about working configuration and let see if any differences on your system.

Command propmt

CMD or PowerShell, not WSL

Redistributable

Only 2013 required, others are for development:

Redistributable

SAP NWRFCSDK

Check if looks like this:

PS > echo $env:SAPNWRFC_HOME 
C:\Tools\nwrfcsdk

PS > dir $env:SAPNWRFC_HOME\lib                                                                         

    Directory: C:\Tools\nwrfcsdk\lib

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2/5/2020   8:31 PM       20786176 icudt50.dll
-a----         2/5/2020   8:31 PM        1573888 icuin50.dll
-a----         2/5/2020   8:31 PM        1278976 icuuc50.dll
-a----         2/5/2020   8:39 PM          65024 libicudecnumber.dll
-a----         2/5/2020   8:39 PM         159744 libsapucum.dll
-a----         2/5/2020   8:42 PM          18764 libsapucum.lib
-a----         2/5/2020   8:52 PM        7157248 sapnwrfc.dll
-a----         2/5/2020   8:52 PM          81290 sapnwrfc.lib
-a----         2/5/2020   8:50 PM       17813504 sapnwrfc.pdb

Check installed NWRFCSDK version:

PS> findstr Patch sapnwrfc.dll
750 Patch Level 6

pynwrfc

Check pyrfc files:

PS > cd 'C:\\Tools\\Pythons\\3.8.2\\lib\\site-packages'
PS > dir
pyrfc
PS > dir pyrfc

    Directory: C:\Tools\Pythons\3.8.2\lib\site-packages\pyrfc

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        5/24/2020   7:25 AM                __pycache__
-a----        5/24/2020   7:25 AM        1282048 pyrfc.cp38-win_amd64.pdb
-a----        5/24/2020   7:25 AM         276480 pyrfc.cp38-win_amd64.pyd
-a----        5/24/2020   7:25 AM           6016 _exception.py
-a----        5/24/2020   7:25 AM           1417 __init__.py

If any differences, please send the console output or screenshot.

You could also check if user authorisation perhaps missing for NWRFCSDK DLLs? Try to move them out of Python directories, to some standard user directory, like c\Tools\nwrfcsdk for example.

Just for any case, please check if nt returned as platform name:

PS > python
>>> import os
>>> os.name
'nt'
Piyush068-png commented 4 years ago

Thanks for your help. Everything was OK. Issue was in authorisation to that particular directory as you mentioned above. So, I changed directory to c\Tools\nwrfcsdk. 1 more guy was facing this isssue. I checked it in 1 of the issues in github. Now, it is working fine in both system. It works well with both redistributable 2010 and 2013.

Suncatcher commented 3 years ago

The pyrfc is another PyPI package, has nothing to do with RFC communication: https://pypi.org/project/pyrfc/

what a pitfall! I also got to the same hole and installed the wrong package :)

bsrdjan commented 3 years ago

pyrfc looks inactive and I requested a name transfer a long time ago but still waiting :( https://github.com/pypa/pypi-support/issues/258

kevinxbrown commented 2 months ago

Looks like the transfer has completed on 14 Apr 2021... https://github.com/pypi/support/issues/258 and so pynwrfc has disappeared.

I'm still having this issue, having installed pyrfc==3.3.1 into my docker image with python 3.12:

COPY nwrfcsdk/ /usr/local/sap/nwrfcsdk/
ENV SAPNWRFC_HOME /usr/local/sap/nwrfcsdk
RUN printf "export SAPNWRFC_HOME=/usr/local/sap/nwrfcsdk \n" >> ~/.bashrc
RUN ldconfig && ldconfig -p | grep sap
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

2024-09-06 16:36:55 Traceback (most recent call last): 2024-09-06 16:36:55 File "/usr/src/app/scripts/server.py", line 3, in 2024-09-06 16:36:55 import get_sap_data as getsap 2024-09-06 16:36:55 File "/usr/src/app/scripts/get_sap_data.py", line 1, in 2024-09-06 16:36:55 from pyrfc import ABAPApplicationError, ABAPRuntimeError, LogonError, Connection 2024-09-06 16:36:55 ImportError: cannot import name 'Connection' from 'pyrfc' (/usr/local/lib/python3.12/site-packages/pyrfc/init.py) 2024-09-06 16:36:55 libsapnwrfc.so: cannot open shared object file: No such file or directory

When I run docker, I'm giving the environment variable - but I'm sure there's something obvious I'm missing somewhere...

docker run -d -p 5000:5000 -e SAPNWRFC_HOME=/usr/local/sap/nwrfcsdk rapidloadpython