Search code examples
numpypyside6python-3.10nuitkadeploying

Nuitka Build Warning and ImportError with NumPy in PySide6 Project


I'm currently working on a PySide6 project and using Nuitka to compile it (pyside6-deploy command) During the build process, I encounter a warning, and at runtime, I face an ImportError related to NumPy. Here are the details:

Build Warning

When building the project, Nuitka outputs the following warning:

Nuitka-Plugins:WARNING: dll-files: DLL configuration by filename code for 'numpy' did not give a result. Either conditions are missing, or this version of the module needs treatment added.

Despite this warning, the build completes successfully.

Runtime Error

However, when I execute the built project, I encounter the following ImportError:

ImportError: Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there.

The error suggests that the project is incorrectly trying to import NumPy from its source directory, but I've verified that I'm not running the project from within the NumPy source directory.

Minimal Reproducible example

Hereunder, you can find a minimal reproducible example. Numpy is embedded in NumPy here but I'm also using it directly in some other modules of the package. To then build my environment I use the following command:

pyside6-deploy --name DICON  -v -f .\main.py

For the involved libraries and python I have the following versions:

Python 3.10.13
Numpy  1.26.4

Here is the minimal example:

import sys
import os

sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), "..")))
import numpy as np

if __name__ == "__main__":
    print(np.linspace(0,100,100))

After executing the build command this is the output that I receive:

INFO:root:[DEPLOY] Start
INFO:root:[DEPLOY] Using python at C:\Users\damie\anaconda3\envs\qutee\python.exe
INFO:root:[DEPLOY] Creating config file C:\Users\damie\OneDrive\Desktop\Damien\AeroDelft\repositories\DCDC-MP-CTRL-PANEL\pysidedeploy.spec
INFO:root:[DEPLOY] No .pyproject file found. Project file not set
INFO:root:[DEPLOY] Installing dependencies
INFO:root:[DEPLOY] package: nuitka==1.5.4 already installed
INFO:root:[DEPLOY] package: ordered_set already installed
INFO:root:[DEPLOY] package: zstandard already installed
INFO:root:[DEPLOY] Creating C:\Users\damie\OneDrive\Desktop\Damien\AeroDelft\repositories\DCDC-MP-CTRL-PANEL\pysidedeploy.spec
INFO:root:[DEPLOY] Deploying application
INFO:root:[DEPLOY] Running Nuitka
Nuitka-Plugins:WARNING: pyside6: Unwanted import of 'tkinter' that is redundant with 'PySide6' encountered. Use '--nofollow-import-to=tkinter' or uninstall it for best compatibility with pure Python execution.
Nuitka-Plugins:WARNING: dll-files: DLL configuration by filename code for 'numpy' did not give a result. Either conditions are missing, or this version of the module needs treatment added.
[DEPLOY] Executed file created in C:\Users\damie\OneDrive\Desktop\Damien\AeroDelft\repositories\DCDC-MP-CTRL-PANEL\main.exe
INFO:root:[DEPLOY] Deployment directory purged
INFO:root:[DEPLOY] End

Attempts to resolve

  1. I have confirmed that my current working directory is not the NumPy source directory
  2. Specified numpy in my pysidedeploy.spec and then instead of building from main.py use the the .spec file instead. You can see the configuration below:
[app]
# title of your application
title = DICON
# project directory. the general assumption is that project_dir is the parent directory
# of input_file
project_dir = .
# source file path
input_file = C:\Users\damie\OneDrive\Desktop\Damien\AeroDelft\repositories\DCDC-MP-CTRL-PANEL\main.py
# directory where exec is stored
exec_directory = .
# path to .pyproject project file
project_file = 

[python]
# python path
python_path = C:\Users\damie\anaconda3\envs\qutee\python.exe
# python packages to install
# ordered-set = increase compile time performance of nuitka packaging
# zstandard = provides final executable size optimization
packages = nuitka==1.5.4,ordered_set,zstandard, numpy==1.26.4
# buildozer = for deploying Android application
android_packages = buildozer==1.5.0,cython==0.29.33

[qt]
# comma separated path to qml files required
# normally all the qml files are added automatically
qml_files = 
# excluded qml plugin binaries
excluded_qml_plugins = 
# path to pyside wheel
wheel_pyside = 
# path to shiboken wheel
wheel_shiboken = 
# plugins to be copied to libs folder of the packaged application. comma separated
plugins = platforms_qtforandroid

[nuitka]
# (str) specify any extra nuitka arguments
# eg = extra_args = --show-modules --follow-stdlib
extra_args = --quiet --noinclude-qt-translations=True

[buildozer]
# build mode
# possible options = [release, debug]
# release creates an aab, while debug creates an apk
mode = debug
# contrains path to pyside6 and shiboken6 recipe dir
recipe_dir = 
# path to extra qt android jars to be loaded by the application
jars_dir = 
# if empty uses default ndk path downloaded by buildozer
ndk_path = 
# if empty uses default sdk path downloaded by buildozer
sdk_path = 
# modules used. comma separated
modules = 
# other libraries to be loaded. comma separated.
# loaded at app startup
local_libs = plugins_platforms_qtforandroid
# architecture of deployed platform
# possible values = ["aarch64", "armv7a", "i686", "x86_64"]
arch = 

Questions:

  1. How can I resolve the DLL warning from Nuitka about NumPy to ensure it's handling the dependencies correctly?
  2. What could be causing the ImportError and how can I resolve it to correctly import NumPy without errors?

Solution

  • Based on Nuitka issue #2113, I figured that my issue was being caused by the fact that I used pip to install Numpy. So I uninstalled using pip uninstall numpy and then installed it again using conda install numpy. This solved my issue with the DLL's.