SAP / PyRFC

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

ImportError DLL load failed while importing _cyrfc #319

Closed jduncan8142 closed 1 year ago

jduncan8142 commented 1 year ago

Describe the bug Import from .cyrfc is failing in pyrfc__init__.py

To Reproduce Use pipenv --python 3.11 to create a new virtual environment. Use pipenv install cython, pipenv install wheel, & pipenv install pyrfc Use pipenv shell to activate the new environment. Run python .\rfc_demo.py

Content of rfc_demo.py: Of course, ashost, user, and passed have been removed for this post.

from pyrfc import Connection

conn = Connection(ashost='************', sysnr='00', client='100', user='************', passwd='************')

result = conn.call('STFC_CONNECTION', REQUTEXT=u'Hello SAP!')
print (result)

The SAP NW RFC SDK is located at C:\nwrfcsdk and is included in PATH and also in the root of the project directory I have file nwrfcsdk.conf with content: C:\nwrfcsdk

Environment

Additional context Error output:

PS C:\Users\duncan\github\murfc> python .\rfc_demo.py
Traceback (most recent call last):
  File "C:\Users\duncan\github\murfc\rfc_demo.py", line 1, in <module>
    from pyrfc import Connection
  File "C:\Users\duncan\.virtualenvs\murfc-9LOxSbvs\Lib\site-packages\pyrfc\__init__.py", line 32, in <module>
    from ._cyrfc import (
ImportError: DLL load failed while importing _cyrfc: The specified module could not be found.
PS C:\Users\duncan\github\murfc> 
bsrdjan commented 1 year ago

I could not reproduce as follows:

bsrdjan@bshost ~ % pip3 install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages (21.2.4)
Collecting pip
  Downloading pip-23.1.2-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.6 MB/s
Installing collected packages: pip
  WARNING: The scripts pip, pip3, pip3.10 and pip3.9 are installed in '/Users/bsrdjan/Library/Python/3.9/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-23.1.2
WARNING: You are using pip version 21.2.4; however, version 23.1.2 is available.
You should consider upgrading via the '/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip' command.

bsrdjan@bshost ~ % python3 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./Library/Python/3.9/lib/python/site-packages (23.1.2)

bsrdjan@bshost ~ % export PATH=/Users/bsrdjan/Library/Python/3.9/bin:$PATH

bsrdjan@bshost ~ % pip -V
pip 23.1.2 from /Users/bsrdjan/Library/Python/3.9/lib/python/site-packages/pip (python 3.9)

bsrdjan@bshost ~ % pip install pipenv
Defaulting to user installation because normal site-packages is not writeable
Collecting pipenv
  Downloading pipenv-2023.4.29-py3-none-any.whl (2.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.8/2.8 MB 15.7 MB/s eta 0:00:00
Collecting certifi (from pipenv)
  Downloading certifi-2022.12.7-py3-none-any.whl (155 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 155.3/155.3 kB 10.9 MB/s eta 0:00:00
Collecting setuptools>=67.0.0 (from pipenv)
  Downloading setuptools-67.7.2-py3-none-any.whl (1.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 24.4 MB/s eta 0:00:00
Collecting virtualenv-clone>=0.2.5 (from pipenv)
  Downloading virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Collecting virtualenv>=20.17.1 (from pipenv)
  Downloading virtualenv-20.23.0-py3-none-any.whl (3.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 28.1 MB/s eta 0:00:00
Collecting distlib<1,>=0.3.6 (from virtualenv>=20.17.1->pipenv)
  Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 468.5/468.5 kB 18.3 MB/s eta 0:00:00
Collecting filelock<4,>=3.11 (from virtualenv>=20.17.1->pipenv)
  Downloading filelock-3.12.0-py3-none-any.whl (10 kB)
Collecting platformdirs<4,>=3.2 (from virtualenv>=20.17.1->pipenv)
  Downloading platformdirs-3.5.0-py3-none-any.whl (15 kB)
Installing collected packages: distlib, virtualenv-clone, setuptools, platformdirs, filelock, certifi, virtualenv, pipenv
Successfully installed certifi-2022.12.7 distlib-0.3.6 filelock-3.12.0 pipenv-2023.4.29 platformdirs-3.5.0 setuptools-67.7.2 virtualenv-20.23.0 virtualenv-clone-0.5.7

bsrdjan@bshost ~ % pipenv shell
Creating a virtualenv for this project...
Pipfile: /Users/bsrdjan/Pipfile
Using /usr/bin/python3 (3.9.6) to create virtualenv...
⠇ Creating virtual environment...created virtual environment CPython3.9.6.final.0-64 in 2161ms
  creator CPython3macOsFramework(dest=/Users/bsrdjan/.local/share/virtualenvs/bsrdjan-2OFqX0do, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/bsrdjan/Library/Application Support/virtualenv)
    added seed packages: pip==23.1.2, setuptools==67.7.2, wheel==0.40.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /Users/bsrdjan/.local/share/virtualenvs/bsrdjan-2OFqX0do
Creating a Pipfile for this project...
Launching subshell in virtual environment...
 . /Users/bsrdjan/.local/share/virtualenvs/bsrdjan-2OFqX0do/bin/activate
bsrdjan@bshost ~ %  . /Users/bsrdjan/.local/share/virtualenvs/bsrdjan-2OFqX0do/bin/activate
(bsrdjan) bsrdjan@bshost ~ % pip install pyrfc
Collecting pyrfc
  Downloading pyrfc-2.8.3-cp39-cp39-macosx_13_0_arm64.whl (231 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 231.4/231.4 kB 4.8 MB/s eta 0:00:00
Installing collected packages: pyrfc
Successfully installed pyrfc-2.8.3

(bsrdjan) bsrdjan@bshost ~ % python -c "import pyrfc; print(pyrfc.__version__)"
2.8.3
(bsrdjan) bsrdjan@bshost ~ %

Could you please try the same and post the full console log, with all steps from creating the environment, to error you are getting?

Did you check the SAP NWRFC SDK installation on your system, using rfcexec test: http://sap.github.io/PyRFC/install.html#sap-nwrfc-sdk-installation ?

Which SAP NWRFC SDK version are you using?

bsrdjan commented 1 year ago

For Windows you don't need pip install cython, wheel etc.

pip install pyrfc is enough because it fetches the prebuilt wheel from pypi and no local build needed on your Windows system

mpf82 commented 1 year ago

The SAP NW RFC SDK is located at C:\nwrfcsdk and is included in PATH

Just to be sure ...

You should add c:\nwrfcsdk\lib to PATH

Additionally, set the SAPNWRFC_HOME env variable: SAPNWRFC_HOME=c:\nwrfcsdk

jduncan8142 commented 1 year ago

Thanks all for the replies. I've updated PATH to include C:\nwrfcsdk\lib and also added the SAPNWRFC_HOME env variable as SAPNWRFC_HOME=c:\nwrfcsdk but still the same error occurs.

I also checked the response when running rfcexec.

PS C:\Users\duncan\github\murfc> C:\nwrfcsdk\bin\rfcexec.exe
Error: Not all mandatory parameters specified
        Please start the program in the following way:
        rfcexec -t -a <program ID> -g <gateway host> -x <gateway service>
                -f <file with list of allowed commands> -s <allowed Sys ID> RFC_TRACE=<level> CPIC_TRACE=<level>
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)
PS C:\Users\duncan\github\murfc>

SAP NWRFC SDK version: 7.50.11-70002755

Full console log:

PS C:\Users\duncan\github\murfc> pipenv --python 3.11
Creating a virtualenv for this project...
Pipfile: C:\Users\duncan\github\murfc\Pipfile
[    ] Creating virtual environment...created virtual environment CPython3.11.0.final.0-64 in 6929ms
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\duncan\AppData\Local\pypa\virtualenv)
    added seed packages: pip==23.0.1, setuptools==67.6.1, wheel==0.40.0
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

Successfully created virtual environment!
Virtualenv location: C:\Users\duncan\.virtualenvs\murfc-9LOxSbvs
Creating a Pipfile for this project...
PS C:\Users\duncan\github\murfc> pipenv shell
Launching subshell in virtual environment...
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\duncan\github\murfc> pipenv install pyrfc
Installing pyrfc...
Resolving pyrfc...
Installing...
Adding pyrfc to Pipfile's [packages] ...
Installation Succeeded
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
Success!
Locking [dev-packages] dependencies...
Updated Pipfile.lock (0edb0f692f2f0db8a553ca19259ab522e4ef7edcb20977822e6eec58ebe07a8b)!
Installing dependencies from Pipfile.lock (e07a8b)...
PS C:\Users\duncan\github\murfc> python -c "import pyrfc; print(pyrfc.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\duncan\.virtualenvs\murfc-9LOxSbvs\Lib\site-packages\pyrfc\__init__.py", line 32, in <module>
    from ._cyrfc import (
ImportError: DLL load failed while importing _cyrfc: The specified module could not be found.
PS C:\Users\duncan\github\murfc> pipenv graph
pyrfc==2.8.3
PS C:\Users\duncan\github\murfc> python .\rfc_demo.py
Traceback (most recent call last):
  File "C:\Users\duncan\github\murfc\rfc_demo.py", line 1, in <module>
    from pyrfc import Connection
  File "C:\Users\duncan\.virtualenvs\murfc-9LOxSbvs\Lib\site-packages\pyrfc\__init__.py", line 32, in <module>
    from ._cyrfc import (
ImportError: DLL load failed while importing _cyrfc: The specified module could not be found.
PS C:\Users\duncan\github\murfc> 
mpf82 commented 1 year ago

Sorry for being pedantic, but what output do you see when you run this?

python -c "import os; print(os.environ.get('SAPNWRFC_HOME'))"

Respectively, does this work?

python -c "import os; os.environ['SAPNWRFC_HOME'] = 'C:\\nwrfcsdk'; import pyrfc; print(pyrfc.__version__)"

(I'm asking, because I can reproduce the same error you are seeing if I do NOT set the environment variable on my Windows system, at least with pyrfc 2.7.0).

jduncan8142 commented 1 year ago
PS C:\Users\duncan\github\murfc> python -c "import os; print(os.environ.get('SAPNWRFC_HOME'))"
C:\nwrfcsdk;
PS C:\Users\duncan\github\murfc> python -c "import os; os.environ['SAPNWRFC_HOME'] = 'C:\\nwrfcsdk'; import pyrfc; print(pyrfc.__version__)"
2.8.3
bsrdjan commented 1 year ago

I can check Windows system tomorrow but why not setting env variable in Windows system settings, instead of using os.environ in Python?

The env variable is used by pyrfc to add SAP NWRFC SDK DLLs to Python DLLs path:

https://github.com/SAP/PyRFC/blob/main/src/pyrfc/__init__.py#L16

You can also try to add the RFC SDK lib directory by using os.add_dll_directory(), instead of setting SAPNWRFC_HOME variable but setting env variable in Windows settings should be easier.

mpf82 commented 1 year ago

C:\nwrfcsdk;

Seems your env varaible has an additional ; - that could be the cause of the problem. As you could see, setting the env varaible in python did work, so it is indeed a poblem with your system env variable.

jduncan8142 commented 1 year ago

Issue was with Window environment variables. The global variables had SAPNWRFC_HOME defined also and had an extra ; included.

unitythemaker commented 2 months ago

I was also getting the same error. After trying to run one of the binaries in nwrfcsdk directory, I got the MSVCP120.dll missing error. After installing Visual C++ Redistributable Packages for Visual Studio 2013 I resolved the error.