Topic: [SOLVED] _ffi ImportError: No module named _ffi

Hi, I'm working in a Python project on Raspberry Pi .
I followed this tutorial: "https://wolfssl.github.io/wolfcrypt-py/" that I give on another thread to can build WolfSSL. But after it  I'm getting some troubles.

When I import a module like 'ciphers.py' or 'hashes.py' in my python project, I get the error: "ffi import from wolfcrypt._ffi as _ffi ImportError: No module named _ffi".

Where is this module? How can I import it into my python project correctly? Thank you so much in any case for all the help received so far, you're saving me.

Share

Re: [SOLVED] _ffi ImportError: No module named _ffi

Hi jesussotofan,


I checked with our python expert and he said this is a very common issue.


if you run python inside wolfssl/wrapper/python
it will try to use the local code instead of the installed one.
the local code doesn’t have the _ffi module, it is built during the building process.

    - Moisés Guimarães

His suggestion is to run python from a different location and if you have similar issue then take a screenshot or echo the error output to a file and send to us for review.


- Kaleb

Re: [SOLVED] _ffi ImportError: No module named _ffi

Many thanks!! It works!
I'm going crazy trying everything, but finally it works, I have just to make the execution in a different location.

Really thank you for everything Kaleb.

Share

Re: [SOLVED] _ffi ImportError: No module named _ffi

Hi jesussotofan,

Not a problem. Happy we could assist you with this project!


Regards,

Kaleb and the wolfSSL Team

Re: [SOLVED] _ffi ImportError: No module named _ffi

Hi Kaleb and Jesussotofan

I have met the problem 'no module _ffi' also.
I am using python virtualvenv. I tried to run python in upper folder, but id does not work.

So I think I do not under what do you mean of running python in different location.

Can you tell me more? Thanks a lot!

regards
Anders

Share

Re: [SOLVED] _ffi ImportError: No module named _ffi

Hi Anders,

Are you building wolfCrypt Pi?

Thanks,
Eric - wolfSSL Support

Re: [SOLVED] _ffi ImportError: No module named _ffi

I have the same problem as qt4004 on Android 14 termux:

(.venv) ~ $ pkg info libwolfssl
Package: libwolfssl
Version: 5.7.2
Maintainer: @termux
Installed-Size: 3891 kB
Homepage: https://www.wolfssl.com/
Download-Size: 630 kB
APT-Manual-Installed: yes
APT-Sources: https://mirrors.bfsu.edu.cn/termux/apt/termux-main stable/main aarch64 Packages
Description: A small, fast, portable implementation of TLS/SSL for embedded devices to the cloud

(.venv) ~ $ pip list
Package   Version
--------- -----------
cffi      1.17.1
pip       24.2
pycparser 2.22
wolfssl   5.7.2.post0
(.venv) ~ $

Please enlight me what's wrong and how to solve it?

Share

Re: [SOLVED] _ffi ImportError: No module named _ffi

The error log

(.venv) ~ $ cd d-net/
(.venv) ~/d-net $ python main.py
Traceback (most recent call last):
  File "/data/data/com.termux/files/home/d-net/main.py", line 6, in <module>
    secure_socket = wolfssl.wrap_socket(sock)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/d-net/.venv/lib/python3.12/site-packages/wolfssl/__init__.py", line 976, in wrap_socket
    return SSLSocket(sock=sock, keyfile=keyfile, certfile=certfile,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/d-net/.venv/lib/python3.12/site-packages/wolfssl/__init__.py", line 407, in __init__
    self._context = SSLContext(ssl_version, server_side)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/home/d-net/.venv/lib/python3.12/site-packages/wolfssl/__init__.py", line 144, in __init__
    _lib.wolfSSL_Init()
    ^^^^
NameError: name '_lib' is not defined
Exception ignored in: <function SSLContext.__del__ at 0x742a00c9a0>
Traceback (most recent call last):
  File "/data/data/com.termux/files/home/d-net/.venv/lib/python3.12/site-packages/wolfssl/__init__.py", line 167, in __del__
    if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
                                      ^^^^
NameError: name '_ffi' is not defined
Exception ignored in: <function SSLSocket.__del__ at 0x742a00d300>
Traceback (most recent call last):
  File "/data/data/com.termux/files/home/d-net/.venv/lib/python3.12/site-packages/wolfssl/__init__.py", line 469, in __del__
    self._release_native_object()
  File "/data/data/com.termux/files/home/d-net/.venv/lib/python3.12/site-packages/wolfssl/__init__.py", line 472, in _release_native_object
    if getattr(self, 'native_object', _ffi.NULL) != _ffi.NULL:
                                      ^^^^
NameError: name '_ffi' is not defined
(.venv) ~/d-net $

Share

Re: [SOLVED] _ffi ImportError: No module named _ffi

The code

(.venv) ~/d-net $ cat main.py
import socket
import wolfssl

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)

secure_socket = wolfssl.wrap_socket(sock)
secure_socket.connect(("dns.google.com", 443))
secure_socket.write(b"GET / HTTP/1.1\r\n\r\n")
print(secure_socket.read())
secure_socket.close()

(.venv) ~/d-net $

Share