Importing numpy fails when CPython is compiled to a DLL
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- operating-systems
Hướng nghiên cứu
Không có tệp CPython hay bài kiểm thử nào được nêu tên. Hãy bắt đầu bằng cách tái hiện lỗi với các điểm vào PyImport_Import, PyRun_SimpleString và PyObject_CallObject được hiển thị trong các cấu hình DLL và console, đồng thời so sánh hành vi với django. Công việc được xem là hoàn tất khi xác định được nguyên nhân là CPython hay thiết lập embedding và ghi lại một chẩn đoán có thể tái hiện.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I am trying to use numpy 1.22.3 with embedded python 3.9.12 or 3.10.2 compiled to a DLL on Windows 10.0.19044.
I've added numpy to embedded python by first installing it to the system python of the same version, then copying the new folders from Lib/site-packages to python3x.zip/site-packages. I have also installed numpy by first installing pip with get-pip from PyPa, but get the same results.
Importing numpy fails in three ways:
When compiling to a UWP DLL or standard DLL without unzipping python3x.zip, I get the following error:
ImportError('
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed.
We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.10 from "C:\\Users\\jtemplin\\git\\PythonInterpreterDLL\\x64\\Debug\\PythonInterpreterTests\\AppX\\PythonInterpreterTests.exe"
* The NumPy version is: "1.22.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named \'numpy.core._multiarray_umath\'
')
This happens in debug and release builds. Other recommendations on the linked troubleshooting page either didn't apply or made no difference.
However, if I unzip the python3x.zip I get different errors:
When compiling to a UWP DLL and and importing numpy in python using PyObject_CallObject I get the following error:
Traceback (most recent call last):
File "C:\path-to-build\tests.py", line 37, in import_numpy_exception
import numpy
File "C:\path-to-build\python310.zip\site-packages\numpy\__init__.py", line 128, in <module>
from numpy.__config__ import show as show_config
File "C:\path-to-build\python310.zip\site-packages\numpy\__config__.py", line 13, in <module>
os.add_dll_directory(extra_dll_dir)
File "os.py", line 1117, in add_dll_directory
OSError: [WinError 87] The parameter is incorrect: 'C:\\path-to-build\\python310.zip\\site-packages\\numpy\\.libs'
The path that os.add_dll_directory is trying to add exists, and the function works if I call it manually in a python shell.
When compiling to a standard DLL I can call os.add_dll_directory using PyRun_SimpleString, but still can't import numpy. I've tried PyImport_Import("numpy"), PyImport_ImportModule("numpy"), PyImport_AddModule("numpy") and they all return nullptr. I've also tried importing numpy in python using PyObject_CallObject and it fails with the following error:
Assertion failed: !PyErr_Occurred(), file D:\a\1\s\Objects\typeobject.c, line 3264
All of the above functions are able to import django successfully, so I don't believe it is a simple path issue. They also import numpy successfully when running in a console app.
So why does importing numpy fail in a DLL?
Here are some code samples that work with django, but not numpy:
bool PythonInterpreter::importModule(const char* moduleName) {
// moduleName = "numpy" or "django"
auto pyModuleName = PyUnicode_FromString(moduleName);
auto module = PyImport_Import(pyModuleName);
auto result1 = !(nullptr == module);
auto module2 = PyImport_ImportModule(moduleName);
auto result2 = !(nullptr == module2);
auto module3 = PyImport_AddModule(moduleName);
auto result3 = !(nullptr == module3);
return result1 || result2 || result3;
}
bool PythonInterpreter::importSimpleString(const char* importString) {
// importString = "import numpy" or "import django"
auto result = 0 == PyRun_SimpleString(importString);
return result;
}
bool PythonInterpreter::importWithException(const char* functionName) {
// functionName = "import_numpy" or "import_django"
auto importNumpyEx = PyObject_GetAttrString(this->testModule, functionName);
auto result = _PyUnicode_AsString(PyObject_CallObject(importNumpyEx, NULL));
return 0 == strcmp(result, "PASS");
}
def import_django():
try:
import django
return "PASS"
except OSError as e:
return full_stack() # lifted from stackexchange
def import_numpy():
try:
import numpy
return "PASS"
except OSError as e:
return full_stack() # lifted from stackexchange
def full_stack():
import traceback, sys
exc = sys.exc_info()[0]
stack = traceback.extract_stack()[:-1] # last one would be full_stack()
if exc is not None: # i.e. an exception is present
del stack[-1] # remove call of full_stack, the printed exception
# will contain the caught exception caller instead
trc = 'Traceback (most recent call last):\n'
stackstr = trc + ''.join(traceback.format_list(stack))
if exc is not None:
stackstr += ' ' + traceback.format_exc().lstrip(trc)
return
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 558
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/cpython
-
docs pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
build type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
stdlib topic-email type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của python/cpython
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
bancolombia/sentinel#23 ·
-
test md Đang mởCI
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100