beeware / Python-Apple-support

A meta-package for building a version of Python that can be embedded into a macOS, iOS, tvOS or watchOS project.
MIT License
1.11k stars 160 forks source link

ios run xcode 12 ios 14 #111

Closed ghost closed 3 years ago

ghost commented 3 years ago

Initializing Python runtime Fatal Python error: Py_Initialize: unable to load the file system codec ImportError: No module named 'encodings'

NSString tmp_path; NSString python_home; wchar_t wpython_home; 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");

python_home = [NSString stringWithFormat:@"%@/Library/Python.framework/Resources", resourcePath, nil];
NSLog(@"PythonHome is: %@", python_home);
wpython_home = _Py_char2wchar([python_home UTF8String], NULL);
Py_SetPythonHome(wpython_home);

// 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();
freakboy3742 commented 3 years ago

Again - you've given a handful of code and an error message, but you haven't described what you've done to generate this error.

The "no module named encodings" error usually means you've configured the Xcode project incorrectly, and Python can't find the standard library. If you want a sample project that shows the configuration that is required, you can generate one with Briefcase.