Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Environment:

  • Ubuntu 24.04.1 LTS
  • .NET SDK 8.0.112
  • Python 3.12.3

Details:

Trying to run some Python in a C# app. This is my code:

Runtime.PythonDLL = @"/usr/lib/x86_64-linux-gnu/libpython3.12.so";

string pathToVirtualEnv = @"/home/jadan/venvs/venv_mare";

string path = Environment.GetEnvironmentVariable("PATH")!.TrimEnd(Path.PathSeparator);
path = string.IsNullOrEmpty(path) ? pathToVirtualEnv : path + Path.PathSeparator + pathToVirtualEnv;
Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONPATH",
     $"{pathToVirtualEnv}/lib/site-packages{Path.PathSeparator}" + 
     $"{pathToVirtualEnv}{Path.PathSeparator}", EnvironmentVariableTarget.Process);

PythonEngine.PythonPath = PythonEngine.PythonPath + Path.PathSeparator + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process);
PythonEngine.PythonHome = pathToVirtualEnv;

PythonEngine.Initialize();

using (Py.GIL())
{
     dynamic np = Py.Import("numpy");
}

Unfortunately, this give the following error:

Python path configuration:
  PYTHONHOME = '/home/jadan/venvs/venv_mare'
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = ''
  sys._base_executable = '/usr/bin/python3'
  sys.base_prefix = ''
  sys.base_exec_prefix = ''
  sys.platlibdir = 'lib'
  sys.executable = '/usr/bin/python3'
  sys.prefix = ''
  sys.exec_prefix = ''
  sys.path = [
    '',
    '/home/jadan/venvs/venv_mare/lib/python3.12/site-packages',
    '/home/jadan/venvs/venv_mare',
    '',
  ]
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'

Can someone help me?

You must be logged in to vote

Replies: 1 comment

Comment options

Your sys.path is missing the path that contains the encodings module, along some other system-wide defaults. You can determine them by running this command:

python3.12 -Ic "import sys; print(sys.path)"

I tried this in a ubuntu docker container, and this is the output I get:

['/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/usr/local/lib/python3.12/dist-packages', '/usr/lib/python3/dist-packages']

The encodings package is in /usr/lib/python3.12.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2548 on April 11, 2025 04:55.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.