Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

ModuleNotFoundError: No module named 'encodings' at PythonEngine.Initialize() in Linux#2217

Closed Answeredbyzhanxh
zhanxh asked this question inQ&A
Discussion options

Environment

  • Pythonnet version:2.0.21
  • Python version: 3.10.11
  • Operating System: Ubuntu 2210
  • .NET Runtime: 6.0.412

Details

  • ModuleNotFoundError: No module named 'encodings' in linux

This error occurs in virtualenv and conda, and has not been tested in other ways. (Initialized successfully in windows.)

  • error message:
/opt/miniconda3/envs/py310/lib/python310.zip;/opt/miniconda3/envs/py310/lib/python3.10;/opt/miniconda3/envs/py310/lib/python3.10/lib-dynload;/opt/miniconda3/envs/py310/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packagesPython path configuration:  PYTHONHOME = '/opt/miniconda3/envs/py310/bin/python'  PYTHONPATH = (not set)  program name = 'python3'  isolated = 0  environment = 1  user site = 1  import site = 1  sys._base_executable = '/opt/miniconda3/envs/py310/bin/python3'  sys.base_prefix = ''  sys.base_exec_prefix = ''  sys.platlibdir = 'lib'  sys.executable = '/opt/miniconda3/envs/py310/bin/python3'  sys.prefix = ''  sys.exec_prefix = ''  sys.path = [    '/opt/miniconda3/envs/py310/lib/python310.zip;/opt/miniconda3/envs/py310/lib/python3.10;/opt/miniconda3/envs/py310/lib/python3.10/lib-dynload;/opt/miniconda3/envs/py310/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packages',  ]Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encodingPython runtime state: core initializedModuleNotFoundError: No module named 'encodings'Current thread 0x00007f348e961740 (most recent call first):  <no Python frame>
  • export PYTHONPATH & run

export PYTHONPATH="/opt/miniconda3/envs/py310/lib/python310.zip;/opt/miniconda3/envs/py310/lib/python3.10;/opt/miniconda3/envs/py310/lib/python3.10/lib-dynload;/opt/miniconda3/envs/py310/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packages"

  • result:
/opt/miniconda3/envs/py310/lib/python310.zip;/opt/miniconda3/envs/py310/lib/python3.10;/opt/miniconda3/envs/py310/lib/python3.10/lib-dynload;/opt/miniconda3/envs/py310/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packagesPython path configuration:  PYTHONHOME = '/opt/miniconda3/envs/py310/bin/python'  PYTHONPATH = '/opt/miniconda3/envs/py310/lib/python310.zip;/opt/miniconda3/envs/py310/lib/python3.10;/opt/miniconda3/envs/py310/lib/python3.10/lib-dynload;/opt/miniconda3/envs/py310/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packages'  program name = 'python3'  isolated = 0  environment = 1  user site = 1  import site = 1  sys._base_executable = '/opt/miniconda3/envs/py310/bin/python3'  sys.base_prefix = ''  sys.base_exec_prefix = ''  sys.platlibdir = 'lib'  sys.executable = '/opt/miniconda3/envs/py310/bin/python3'  sys.prefix = ''  sys.exec_prefix = ''  sys.path = [    '/opt/miniconda3/envs/py310/lib/python310.zip;/opt/miniconda3/envs/py310/lib/python3.10;/opt/miniconda3/envs/py310/lib/python3.10/lib-dynload;/opt/miniconda3/envs/py310/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packages',  ]Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encodingPython runtime state: core initializedModuleNotFoundError: No module named 'encodings'Current thread 0x00007fe65d843740 (most recent call first):  <no Python frame>
stringwinPath=@"D:\tools\python310";stringlinuxPath="/opt/miniconda3/envs/py310";if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)){stringpathToVirtualEnv=winPath;Runtime.PythonDLL=Path.Combine(pathToVirtualEnv,"python310.dll");PythonEngine.PythonHome=Path.Combine(pathToVirtualEnv,"python.exe");PythonEngine.PythonPath=@$"{pathToVirtualEnv}\Lib\site-packages;{pathToVirtualEnv}\Lib;{pathToVirtualEnv}\DLLs";}elseif(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){//VirtualenvvarpathToVirtualEnv="/opt/miniconda3/envs/py310";Runtime.PythonDLL=$"{pathToVirtualEnv}/lib/libpython3.10.so";varpath=Environment.GetEnvironmentVariable("PATH").TrimEnd(';');path=string.IsNullOrEmpty(path)?pathToVirtualEnv:path+";"+pathToVirtualEnv;Environment.SetEnvironmentVariable("PATH",path,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PATH",pathToVirtualEnv,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PYTHONHOME",pathToVirtualEnv,EnvironmentVariableTarget.Process);Environment.SetEnvironmentVariable("PYTHONPATH",$"{pathToVirtualEnv}/lib/python310.zip;{pathToVirtualEnv}/lib/python3.10;{pathToVirtualEnv}/lib/python3.10/lib-dynload;{pathToVirtualEnv}/lib/python3.10/site-packages;~/.local/lib/python3.10/site-packages",EnvironmentVariableTarget.Process);varpythonpath=Environment.GetEnvironmentVariable("PYTHONPATH",EnvironmentVariableTarget.Process);;Console.WriteLine(pythonpath);PythonEngine.PythonHome=$"{pathToVirtualEnv}/bin/python";PythonEngine.PythonPath=pythonpath;}else{Console.WriteLine("This OS is not support");return;}PythonEngine.Initialize();
You must be logged in to vote

Oh my god!
It's a mistake.
We must use ":" instead of ";"

            string winPath = @"D:\tools\python310";            string linuxPath = "/opt/miniconda3/envs/py310";            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))            {                string pathToVirtualEnv = winPath;                Runtime.PythonDLL = Path.Combine(pathToVirtualEnv, "python310.dll");                PythonEngine.PythonHome = Path.Combine(pathToVirtualEnv, "python.exe");                PythonEngine.PythonPath = @$"{pathToVirtualEnv}\Lib\site-packages;{pathToVirtualEnv}\Lib;{pathToVirtualEnv}\DLLs";            }            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))…

Replies: 3 comments 1 reply

Comment options

The reason for not accepting the answer is that there is a question in the python environment.
Please give the correct configuration reference.
I have referred towiki

You must be logged in to vote
0 replies
Comment options

I had a similar issue, but a completely different environment, so I'm not sure if it applies.

By default, pythonnet works with the .NET Framework environment, not core.

Instructions for loading core:
https://github.com/pythonnet/pythonnet#net-core

In my environment that alone isn't enough, because some of my dependencies could work with .NET 6 or 7, but there was no directive either way. My options were to load the .NET 7 runtuime or add in a reference to the runtimeconfig. You're running out of a .NET 6 application, so I would think the runtimeconfig.json for your app would be used?

You must be logged in to vote
1 reply
@zhanxh
Comment options

PythonPath, We must use ":" instead of ";" in Linux

Comment options

Oh my god!
It's a mistake.
We must use ":" instead of ";"

            string winPath = @"D:\tools\python310";            string linuxPath = "/opt/miniconda3/envs/py310";            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))            {                string pathToVirtualEnv = winPath;                Runtime.PythonDLL = Path.Combine(pathToVirtualEnv, "python310.dll");                PythonEngine.PythonHome = Path.Combine(pathToVirtualEnv, "python.exe");                PythonEngine.PythonPath = @$"{pathToVirtualEnv}\Lib\site-packages;{pathToVirtualEnv}\Lib;{pathToVirtualEnv}\DLLs";            }            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))            {                //Virtualenv                var pathToVirtualEnv = "/opt/miniconda3/envs/py310";                Runtime.PythonDLL = $"{pathToVirtualEnv}/lib/libpython3.10.so";                var path = Environment.GetEnvironmentVariable("PATH").TrimEnd(':');                path = string.IsNullOrEmpty(path) ? pathToVirtualEnv : path + ":" + pathToVirtualEnv;                Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);                Environment.SetEnvironmentVariable("PATH", pathToVirtualEnv, EnvironmentVariableTarget.Process);                Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process);                Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}/lib/python3.10:{pathToVirtualEnv}/lib/python3.10/lib-dynload:{pathToVirtualEnv}/lib/python3.10/site-packages:~/.local/lib/python3.10/site-packages", EnvironmentVariableTarget.Process);                // {pathToVirtualEnv}/lib/python310.zip;                var pythonpath = Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process); ;                Console.WriteLine(pythonpath);                PythonEngine.PythonHome = $"{pathToVirtualEnv}/bin/python";                //PythonEngine.PythonHome = $"{pathToVirtualEnv}";                PythonEngine.PythonPath = pythonpath;            }            else            {                Console.WriteLine("This OS is not support");                return;            }            PythonEngine.Initialize();
You must be logged in to vote
0 replies
Answer selected byzhanxh
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@zhanxh@DeniseSkidmore
Converted from issue

This discussion was converted from issue #2216 on August 09, 2023 07:01.


[8]ページ先頭

©2009-2025 Movatter.jp