[Solved] ModuleNotFounderror: No Module named _ctypes in Python

Errors are raised when python cannot understand a given code because the code differs from the syntax and its rules. “modulenotfounderror: no module named _ctypes ” is one such error on which we will be discussing in this article.

As always, we will be breaking down the entire error into parts and learn what each term signifies.

What is modulenotfounderror?

Modules are lines of code containing functions that can be imported into another program. Modules are designed to make the code in python more manageable by improving readability and implementing reusability. The syntax for importing a module is: import module_name

Python raises a modulenotfounderror when it is unable to import a module. Python will try to find a module in either sys.modules or sys.path. When it cannot load the module, it will raise modulenotfounderror. It raises the error when python is unable to find either or one of the dependencies.

Module ‘_ctypes’

The ctypes module in python is used for providing C-compatible data types. Basically, ctypes is used to create and manipulate C data types in Python.

We use ctypes to call functions that are present in shared libraries. It is a way to implement C functions in python. With ctypes, we can use C data types such as arrays, double, structures, pointers, etc.

Ctypes is also used to wrap shared libraries in pure Python. To import all the functionalities from ctypes, we use: from ctypes import *

We use CDLL() function to represent a loaded DLL or a shared library.

Solving ModuleNotFoundError: no module named ‘_ctypes’

There are a couple of reasons why this error might be reflected on your computer. First, you need to ensure that while importing the ctypes module, you are typing the module name correctly because python is a case-sensitive language and will throw a modulenotfounderror in that case too.

import _Ctypes

The above import statement will throw an error because the ‘c’ in _ctypes should not be an uppercase character.

ModuleNotFoundError: No module named '_Ctypes'

Modulenotfounderror: No Module Named _ctypes for Linux System

If you use the Ubuntu Linux system, there are more chances that python will throw the modulenotfounderror while importing the _ctypes module. You may try to install and re-install python, but it might not work in most cases.

The main reason is that the _ctypes module in the ubuntu system requires the libffi-dev package to be installed. The _ctypes module depends on libffi-dev, so that has to be installed first. Because the system is missing the installation of libffi-dev, it cannot use to _ctypes module.

To install libffi-dev in your system, you can try either of the below code:

sudo apt install libffi-dev


OR

sudo apt-get install libffi-dev

When installing python version 3.7.0 or above under Linux, you need to ensure that libffi-dev is installed. Because in ubuntu, an error message of the missing dependencies(which is _ctype in this case) will appear during python installation.

Also Check Out | [Solved] No Module Named Numpy in Python

Modulenotfounderror: No Module Named _ctypes for CentOS

Modulenotfounderror is encountered while using CentOS in Linux. For example, while installing python in CentOS with version 3.7 or greater than 3.7, the above error occurs.

To use the _ctypes modules, we need to install a new package: libffi -devel. To install the libffi -devel package, we have to use the ‘yum’ command in centos. The ‘yum’ command is the default package manager. We use this command to install and update packages in CentOS.

The code for installing the package is:

yum install libffi-devel -y
make install

After installing the libffi-devel package, again install and compile. This should resolve the error.

Also, Read | [Solved] No Module Named Tensorflow Error

Modulenotfounderror: No Module Named _ctypes for MacOS

macOS faces the same problem while dealing with the _ctypes module. You will have to install libffi for using the _ctypes module. Note that for Python versions less than 2.6, libffi does not work on 64-bit macOS.

To install libffi, use the below code in your MacOS terminal. Here, we are installing libffi for version 3.2.1. You can change your version accordingly.

wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
tar xvfz libffi-3.2.1.tar.gz
cd libffi-3.2.1
./configure --prefix=/usr/local/libffi/3_2_1
make
make install

FAQ’s

What is the difference between ImportError and ModuleNotFoundError?

ImportError is actually a parent class of ModuleNotFoundError, where ModuleNotFoundError is its subclass. ModuleNotFoundError is raised when python was not successful in importing a particular module. When an error occurs in importing a module while using: ‘import module_name,’ a ModuleNotFoundError is raised. But, when there is an error while using
‘from … import module_name’ command, python raises an ImportError.

How to solve Modulenotfounderror: No Module Named ‘_ctypes’ for matplotlib/numpy in Linux System

While performing sudo make install’ during python installation, you may get modulenotfounderror for _ctypes modules. The error occurs because python is missing some dependencies. Try to install the dependencies given in the code below:

1. sudo apt-get update

2
. sudo apt-get upgrade

3
. sudo apt-get dist-upgrade

4
. sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus

5
. sudo apt-get install libncursesw5-dev libgdbm-dev libc6-dev

6
. sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev

7
. sudo apt-get install libssl-dev openssl

8
. sudo apt-get install libffi-dev


This sums up the article about Modulenotfounderror: No Module Named _ctypes in Python. If you have any questions, let us know in the comments below.

Until then, Happy Learning!

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Phill
Phill
2 months ago

I’m using Linux /// I’ve installed libfidev. I hope it works