Git Product home page Git Product logo

Comments (8)

Xingsandesu avatar Xingsandesu commented on September 25, 2024

I think I should catch this exception. Please wait a moment while I check the output information of this exception.

from nuitka.

Xingsandesu avatar Xingsandesu commented on September 25, 2024

The Unicode code is Chinese, and the translated meaning is "Enterprise WeChat configuration error, please check the relevant configuration of Enterprise WeChat." Because the relevant configurations of Enterprise WeChat are stored in config.yaml, the error reported ultimately is the code for loading config above. 'ascii' codec can't decode byte 0xe6 in position 146: ordinal not in range(128)

root@erp:~/test# ./app.bin 
'ascii' codec can't decode byte 0xe6 in position 146: ordinal not in range(128)
\u4f01\u4e1a\u5fae\u4fe1\u914d\u7f6e\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5\u4f01\u4e1a\u5fae\u4fe1\u76f8\u5173\u914d\u7f6e
Traceback (most recent call last):
  File "/root/test/app.py", line 18, in <module>
ImportError: cannot import name 'wechat_push' from 'ext.ext' (/root/test/ext/ext.py)

error code:
'ascii' codec can't decode byte 0xe6 in position 146: ordinal not in range(128)

from nuitka.

Xingsandesu avatar Xingsandesu commented on September 25, 2024

In my testing, this is a possible fix: specify UTF-8 encoding, but I still don't understand why this problem will not occur without turning on LTO optimization.

def load_config():
    try:
        with open('config.yaml', 'r', encoding='utf-8') as file:
            config = yaml.safe_load(file)
    except Exception as e:
        # Generate a new key pair
        print(e)
        key = jwk.JWK.generate(kty='RSA', size=4096)

        # Export the private key and public key
        DEFAULT_CONFIG['PRIVATE_KEY_JSON'] = key.export_private()
        DEFAULT_CONFIG['PUBLIC_KEY_JSON'] = key.export_public()

        config = DEFAULT_CONFIG
        with open('config.yaml', 'w', encoding='utf-8') as file:
            yaml.dump(DEFAULT_CONFIG, file)

    return config

from nuitka.

kayhayen avatar kayhayen commented on September 25, 2024

The default encoding is sys.getfilesystemencoding(). Maybe print it in compiled variants of your system and see if there is a difference. I doubt there would be a difference.

from nuitka.

Xingsandesu avatar Xingsandesu commented on September 25, 2024

The default encoding is sys.getfilesystemencoding(). Maybe print it in compiled variants of your system and see if there is a difference. I doubt there would be a difference.

Thank you very much for your suggestion, I added sys.getfilesystemencoding() to test run. The following is the modified code.
As you can see from the program output below, using LTO optimization, the default will become ascii instead of UTF-8.
This may be the reason why Chinese characters become Unicode, and it may also be the cause of the error 'ascii' codec can't decode byte 0xe6 in position 146: ordinal not in range(128) reason
In addition, I will attach the log of Github Action. This example is compiled with Github Action. After my testing, whether using local Debian12 using nuitka installed with pip or using Github Action directly, this problem always exists.
job-logs.txt

def load_config():
    try:
        systemencoding = sys.getfilesystemencoding()
        print(f"Load_Config_Nomal:{systemencoding}")
        with open('config.yaml', 'r') as file:
            config = yaml.safe_load(file)
    except Exception as e:
        # Generate a new key pair
        systemencoding = sys.getfilesystemencoding()
        print(f"Load_Config_Exception:{systemencoding}")
        print(e)
        key = jwk.JWK.generate(kty='RSA', size=4096)

        # Export the private key and public key
        DEFAULT_CONFIG['PRIVATE_KEY_JSON'] = key.export_private()
        DEFAULT_CONFIG['PUBLIC_KEY_JSON'] = key.export_public()

        config = DEFAULT_CONFIG
        with open('config.yaml', 'w') as file:
            yaml.dump(DEFAULT_CONFIG, file)

    return config

lto=yes

root@erp:~/ltoyes-18# ./app.bin 
Load_Config_Nomal:ascii
Load_Config_Exception:ascii
'ascii' codec can't decode byte 0xe6 in position 146: ordinal not in range(128)
\u4f01\u4e1a\u5fae\u4fe1\u914d\u7f6e\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5\u4f01\u4e1a\u5fae\u4fe1\u76f8\u5173\u914d\u7f6e
Traceback (most recent call last):
  File "/root/ltoyes-18/app.py", line 18, in <module>
ImportError: cannot import name 'wechat_push' from 'ext.ext' (/root/ltoyes-18/ext/ext.py)

Use Cpython directly and lto=no

Load_Config_Nomal:utf-8
[2024-05-22 08:50:33 +0800] [2708] [INFO] Starting gunicorn 22.0.0
[2024-05-22 08:50:33 +0800] [2708] [INFO] Listening at: http://127.0.0.1:8000 (2708)
[2024-05-22 08:50:33 +0800] [2708] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2024-05-22 08:50:33 +0800] [2725] [INFO] Booting worker with pid: 2725
[2024-05-22 08:50:33 +0800] [2725] [INFO] Started server process [2725]
[2024-05-22 08:50:33 +0800] [2725] [INFO] Waiting for application startup.
[2024-05-22 08:50:33 +0800] [2725] [INFO] Application startup complete.
[2024-05-22 08:50:33 +0800] [2727] [INFO] Booting worker with pid: 2727
[2024-05-22 08:50:33 +0800] [2727] [INFO] Started server process [2727]
Load_Config_Nomal:utf-8
Load_Config_Exception:utf-8
[Errno 2] No such file or directory: 'config.yaml'
企业微信配置错误,请检查企业微信相关配置
Traceback (most recent call last):
  File "/root/Zero_ERP/app.py", line 18, in <module>
    from ext.ext import wechat_push
ImportError: cannot import name 'wechat_push' from 'ext.ext' (/root/Zero_ERP/ext/ext.py)

Github Action

jobs:
  build:
    strategy:
      matrix:
        os: [ ubuntu-20.04 ]

    runs-on: ${{ matrix.os }}

    steps:
      - name: Check-out repository
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          architecture: 'x64'
          cache: 'pip'
          cache-dependency-path: |
            **/requirements*.txt

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Build Executable
        uses: Nuitka/Nuitka-Action@main
        with:
          nuitka-version: main
          script-name: app.py
          company-name: KooKoo.top
          product-name: ZERO_ERP
          file-version: ${{ github.ref_name }}
          product-version: ${{ github.ref_name }}
          standalone: true
          show-scons: true
          onefile: false
          static-libpython: yes
          lto: yes
          include-package: | 
                    uvicorn
                    gunicorn
                    pydantic
                    logging
          

from nuitka.

kayhayen avatar kayhayen commented on September 25, 2024

This must be GitHub Python being strange. I think it might be unable to load extension modules when you use static libpython, and some codeds, specifically asian ones, might be affected. I see them being exxtension modules normally. This may or may not happen with LTO. You best mess with the static-libpython setting if you know what you are doing.

LTO will inline things from there and be able to drop unused things, but can be wrong about that.

What I think is kind of missing from Nuitka is a sort of auto-detection by defined experiments that allows to decide if the static libpython is good or not. And if LTO works or not. This is hard to make though.

from nuitka.

Xingsandesu avatar Xingsandesu commented on September 25, 2024

This must be GitHub Python being strange. I think it might be unable to load extension modules when you use static libpython, and some codeds, specifically asian ones, might be affected. I see them being exxtension modules normally. This may or may not happen with LTO. You best mess with the static-libpython setting if you know what you are doing.

LTO will inline things from there and be able to drop unused things, but can be wrong about that.

What I think is kind of missing from Nuitka is a sort of auto-detection by defined experiments that allows to decide if the static libpython is good or not. And if LTO works or not. This is hard to make though.

Thank you for your reply. However, I don't think the issue is related to the static-libpython setting, because this encoding problem still occurs even when I don't specify the static-libpython setting (i.e., when it is set to NO). I believe the ultimate solution to this problem is to add encoding='utf-8' to with open, which prevents my app from throwing errors. If possible, could you add this point to the "Solutions to the Common Issues" section on the official website? It might help others as well. Thank you very much for your help。

from nuitka.

kayhayen avatar kayhayen commented on September 25, 2024

Nah, it's a broken configuration of Python if that's needed. I don't know how to deal with people who enable modes that don't work yet.

from nuitka.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.