You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: This issue is due to I was trying to use pickle(Python 3) to load a pkl file that generated by Python 2.7.x pickle. I don't think this issue needs pay much attention, as Python 2's lifecycle has been over for a while now. I have resolved this issue, steps to reproduce the issue and the solution are as follows.
Nuitka version, full Python version, flavor, OS, etc. as output by this exact command.
python -m nuitka --version
2.4.11
Commercial: None
Python: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:17:27) [MSC v.1929 64 bit (AMD64)]
Flavor: Anaconda Python
Executable: D:\Software\Miniconda3\envs\Test\python.exe
OS: Windows
Arch: x86_64
WindowsRelease: 11
Version C compiler: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.41.34120\bin\Hostx64\x64\cl.exe (cl 14.3).
The original pkl file is too large, so I wrote this script to generate small test files.
Please make sure run generate_pkl_file.py in both Python 2 (2.7.18) and Python 3 (3.12.7)environments.
This script is use to generate two test files(demo_py2.pkl and demo_py3.pkl).
Adjust the filename as needed before run this script.
Solution 1:
add implicit-import to path\to\myenv\Lib\site-packages\nuitka\plugins\standard stdlib3.nuitka-package.config.yml
- module-name: 'pickle'# checksum: 167cb032implicit-imports:
- depends:
- 'numpy.core.multiarray'anti-bloat:
- description: 'remove module ability to run as a binary'change_function:
'_test': "'(lambda: None)'"
Solution 2:
Simply adding the import statement import numpy.core.multiarray before pickle.loads function(uncomment this line in test.py) should resolve the issue.
This solution meets my requirements, as in my entire project, this function is only used in specific location, so the impact of this solution is minimal.
The text was updated successfully, but these errors were encountered:
So, pickle will only be able to load modules that are included, not all modules are includes, unless some dependency pulls it in, typically in a program, these will align, but as observed, they don't have to, and if pickles are used for IPC, then of course absolutely not has to be the case.
So, what can be done here, is to give a more informative error in case of ImportError with explanations as a non-deployment mode hook, but I don't see why every pickle using program would have to include numpy at all, and that's just a specific case.
Need to figure out, how we could make this happen, for pickle functions there, we can probably catch errors for them by wrapping them with module append code and that should be easy. For deployment mode, we might still give a better error message, but maybe without pointing out what to compile with.
The wrapper basically should catch ImportError and turn it into RuntimeError with a usage hint.
We have used things like this:
sys.exit('''Nuitka: Need to use this as an option to compile with '--include-distribution-metadata=%s'.''' % pkg_name)
The module name not found will be in the exception object you caught, and then you can inform the developer. In other cases, we did RuntimeError like this: raise (RuntimeError('Nuitka: Needs to be elevated already, use --windows-uac-admin')) and it would be better maybe. For deployment mode, maybe sys.exit could be used, for non-deployment raising, to make sure it goes through to the developer, but that's something I can add myself later once I harmonize these things.
Note: This issue is due to I was trying to use pickle(Python 3) to load a pkl file that generated by Python 2.7.x pickle. I don't think this issue needs pay much attention, as Python 2's lifecycle has been over for a while now. I have resolved this issue, steps to reproduce the issue and the solution are as follows.
Nuitka version, full Python version, flavor, OS, etc. as output by this exact command.
How did you install Nuitka and Python
conda virtualenv, pip install nuitka
The specific PyPI names and versions
generate_pkl_file.py
The original pkl file is too large, so I wrote this script to generate small test files.
Please make sure run
generate_pkl_file.py
in both Python 2 (2.7.18) and Python 3 (3.12.7)environments.This script is use to generate two test files(
demo_py2.pkl
anddemo_py3.pkl
).Adjust the filename as needed before run this script.
test.py
Compile test.py using Nuitka . Please keep
import numpy.core.multiarray
line is commented out(see solution 2).After compiling, run the generated
.\test.dist\test.exe
file to reproduce the issue. The output:The file generated by Python 3 is load correctly, while the file generated by Python 2 is not.
python -m nuitka --msvc=latest --standalone .\test.py
numpy < 2.0 works
add
implicit-import
topath\to\myenv\Lib\site-packages\nuitka\plugins\standard stdlib3.nuitka-package.config.yml
Simply adding the import statement
import numpy.core.multiarray
beforepickle.loads
function(uncomment this line in test.py) should resolve the issue.This solution meets my requirements, as in my entire project, this function is only used in specific location, so the impact of this solution is minimal.
The text was updated successfully, but these errors were encountered: