Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JelleAdan opened this issue Feb 12, 2025 · 0 comments

Comments

@JelleAdan
Copy link

JelleAdan commented Feb 12, 2025

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?

@pythonnet pythonnet locked and limited conversation to collaborators Apr 11, 2025
@lostmsu lostmsu converted this issue into discussion #2578 Apr 11, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant