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

ModuleNotFoundError: No module named 'encodings' when trying to use virtual environment in C# app on Ubuntu #2548

Copy link
Copy link
@JelleAdan

Description

@JelleAdan
Issue body actions

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?

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

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