Skip to content

ModuleNotFoundError: No module named 'encodings' #142

@CarlosNano

Description

@CarlosNano

Describe the bug
When I run Py_Initialize(); the app crashes and I get:

Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000000103083880 (most recent call first):
  <no Python frame>

I've found this other ticket: #21 which advised how to set up python.

My code is a copy of the main file:

    NSString * resourcePath = [[NSBundle mainBundle] resourcePath];

    // Special environment to prefer .pyo; also, don't write bytecode
    // because the process will not have write permissions on the device.
    putenv("PYTHONOPTIMIZE=1");
    putenv("PYTHONDONTWRITEBYTECODE=1");
    putenv("PYTHONUNBUFFERED=1");

    // Set the home for the Python interpreter
    python_home = [NSString stringWithFormat:@"%@/Library/Python", resourcePath, nil];
    NSLog(@"PythonHome is: %@", python_home);
    wpython_home = Py_DecodeLocale([python_home UTF8String], NULL);
    Py_SetPythonHome(wpython_home);

    // Set the PYTHONPATH
    python_path = [NSString stringWithFormat:@"PYTHONPATH=%@/Library/Application Support/com.mycompany.PythonHelloWorld/app:%@/Library/Application Support/com.mycompany.PythonHelloWorld/app_packages", resourcePath, resourcePath, nil];
    NSLog(@"PYTHONPATH is: %@", python_path);
    putenv((char *)[python_path UTF8String]);

    // iOS provides a specific directory for temp files.
    tmp_path = [NSString stringWithFormat:@"TMP=%@/tmp", resourcePath, nil];
    putenv((char *)[tmp_path UTF8String]);

    NSLog(@"Initializing Python runtime...");
    Py_Initialize();

    // Set the name of the python NSLog bootstrap script
    nslog_script = [
        [[NSBundle mainBundle] pathForResource:@"Library/Application Support/com.mycompany.PythonHelloWorld/app_packages/nslog"
                                        ofType:@"py"] cStringUsingEncoding:NSUTF8StringEncoding];

    if (nslog_script == NULL) {
        NSLog(@"Unable to locate NSLog bootstrap script.");
        exit(-2);
    }
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(wpython_home);
    if (python_argv) {
        PyMem_RawFree(python_argv);
    }
    NSLog(@"Leaving...");
    return 0;

I've replaced the execution of the python code with PyRun_SimpleString, but the problem is happening in Py_Initialize

Any advise how to find where the problem is? For what I could find in searching it could be to do with the PYTHONHOME or PYTHONPATH

Thanks

Expected behavior
No crash neither errors in the console

Environment:

Operating System: 12.2.1
Python version: 3.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions