Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError : No module named '_ctypes' #932

Closed
MaximG1234 opened this issue Aug 4, 2019 · 3 comments
Closed

ImportError : No module named '_ctypes' #932

MaximG1234 opened this issue Aug 4, 2019 · 3 comments

Comments

@MaximG1234
Copy link

MaximG1234 commented Aug 4, 2019

Environment

  • Pythonnet version: 2.3.0.0
  • Python version: 3.5.4
  • Operating System: Windows 10 Pro Version 1803

I am trying to use Python.NET to perform interop between C# and Python on a Windows machine.

Specifically I have the following code.

PythonEngine.PythonHome = @"C:\Python\3_5_4";
PythonEngine.PythonPath = @"C:\Python\3_5_4\Lib;C:\Python\3_5_4\Lib\site-packages";

using (Python.Runtime.Py.GIL())
{
    dynamic np = Py.Import("numpy");
    dynamic sin = np.sin;
    Console.WriteLine(sin);
}

While I can successfully execute a general python statement as such:

var res = PythonEngine.Eval("1 + 1");
Console.WriteLine(res);
//res = 2

Which indicates that the python engine itself is working successfully, I can also invoke something like this:

var html = Py.Import("html");
Console.WriteLine(html);
//html = <module 'html' from 'C:\\Python\\3_5_4\\Lib\\html\\__init__.py'>

Which further indicates that the module loading functionality is also working correctly.

However whenever I attempt to invoke the line:

dynamic np = Py.Import("numpy"); 

I receive the following error:

{"ImportError : No module named '_ctypes'"}.

['  File "C:\\Python\\3_5_4\\Lib\\site-packages\\numpy\\__init__.py", line 140, in <module>\n    from . import _distributor_init\n', '  File "C:\\Python\\3_5_4\\Lib\\site-packages\\numpy\\_distributor_init.py", line 9, in <module>\n    from ctypes import WinDLL\n', '  File "C:\\Python\\3_5_4\\Lib\\ctypes\\__init__.py", line 8, in <module>\n    from _ctypes import Union, Structure, Array\n']

The file referenced is located @ C:\Python\3_5_4\Lib\ctypes\__init__.py.

I have verified that all the expected paths are set correctly and that the Ctypes folder exists in my python path.

I have tried everything from uninstalling and reinstalling Python to modifying the ctypes/__init__.py file to try and import Ctypes directly to no effect.

Having researched this topic online throughly I have found a number of suggestions which point to running yum install libffi-devel as detailed here. However this does not appear to be something that I can perform on windows given that yum appears to be a Linux only application.

Can anyone provide any guidance?

@filmor
Copy link
Member

filmor commented Aug 26, 2019

Ctypes is included in the standard library, the referenced module are the compiled parts of it, try to reinstall Python itself.

@filmor filmor closed this as completed Aug 26, 2019
@nguyenvuduc
Copy link

@MaximG1234 : _ctypes.pyd is in PYTHONHOME/DLLs. You should include the directory in your PYTHONPATH environment variable. I can confirm that it will work as I already tried it by myself.

@StackChan
Copy link

@nguyenvuduc You save my day.Sincere thanks for u.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants