Home > Software engineering >  Shipping Python interpreter with C project
Shipping Python interpreter with C project

Time:11-28

Problem description: I have a Visual Studio 2022 C project that involves live python script interpretation. Naturally, I need a valid Python installation to do this. However, I intend to ship this as an application, so I'd like to have a localized Python installation, to avoid consumer-side installation, but that doesn't interfere with Windows' Environmental Variables.

What I've done: I included "Python.h" from my Python installation's "include" folder, I've added its "libs" folder to "Additional Library Directories", I've added "python311.lib" to "Additional Dependencies", and I remembered to copy Python311.dll to my project's Solution Directory. Everything is linked properly. However, when I run compile and execute my program, I receive a long list of errors, which are as follows:

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = 'C:\Coding Projects\MaSGE\Lib'
  sys._base_executable = 'C:\\Coding Projects\\MaSGE\\x64\\Release\\MaSGE.exe'
  sys.base_prefix = 'C:\\Coding Projects\\MaSGE'
  sys.base_exec_prefix = 'C:\\Coding Projects\\MaSGE'
  sys.platlibdir = 'DLLs'
  sys.executable = 'C:\\Coding Projects\\MaSGE\\x64\\Release\\MaSGE.exe'
  sys.prefix = 'C:\\Coding Projects\\MaSGE'
  sys.exec_prefix = 'C:\\Coding Projects\\MaSGE'
  sys.path = [
    'C:\\Coding Projects\\MaSGE\\python311.zip',
    'C:\\Coding Projects\\MaSGE\\Lib',
    'C:\\Coding Projects\\MaSGE\\DLLs',
  ]
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 0x0000399c (most recent call first):
  <no Python frame>

Of particular interest to me are the first two lines, plus the "PYTHONHOME = (not set)" and "PYTHONPATH = (not set)" on lines 4 and 5 which, to my knowledge, are Environmental Variables.

This brings me to the crux of the problem: Is there some way in which I can install a portable Python interpreter to a specific folder to circumvent the issue with Environmental Variables?

CodePudding user response:

To embed python into your application, you need two things:

Initialize isolated python

This will not let user's system interfere with your app.

https://docs.python.org/3/c-api/init_config.html#init-isolated-conf

Deploy python stuff with your application

On windows, you need:

  1. Python DLL (python311.dll).
  2. Python standard library. Either copy Lib folder from python installation, or zip its contents and name it after python dll (e.g. python311.zip), or use the same zip from Windows embeddable package)
  3. Python modules (DLLs directory)

On linux, situation is slightly different:

  1. Python shared library (libpython311.so)
  2. Python standard library (Either copy lib/python311/ folder from python installation, or zip its contents except lib-dynload folder and name it lib/python311.zip)
  3. (If using zipped standard library) Copy lib-dynload to lib/python3.11/lib-dynload

Of course, you have to replace 311 and 3.11 with your version of python.

  • Related