How to resolve "No module named 'encodings' " using jep

3,292 views
Skip to first unread message

Ernst Reissner

unread,
Jun 11, 2020, 6:40:01 PM6/11/20
to Jep Project
I have been using jep for some weeks, come back again and cannot get it run again.
Sth i changed and i cannot find out what it is.
Maybe

First i must say that i run jep in a conda environment.
The problem to get jep to find libjep i solved by passing the CONDA_PREFIX
which is
```
/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3
```
as a property to java and load the libjep with the following code:
```
    String pythonInst = System.getProperty(DIR_PYTHON_INST);
    PyConfig pyConfig = new PyConfig().setPythonHome(pythonInst);
    try {
        MainInterpreter.setInitParams(pyConfig);
        SharedInterpreter.setConfig(new JepConfig());
    } catch (JepException e) {
        throw new IllegalStateException("Jep failed to initialize.");
    }
```
 Seemingly this works but then an error occurs when invoking
```
Interpreter pyInterp = new SharedInterpreter()
```
the following output pops up:
```
PYTHONHOME = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3'
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/bin/python3'
  sys.base_prefix = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3'
  sys.base_exec_prefix = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3'
  sys.executable = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/bin/python3'
  sys.prefix = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3'
  sys.exec_prefix = '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3'
  sys.path = [
    '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib/python38.zip',
    '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib64/python3.8',
    '/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib64/python3.8/lib-dynload',
  ]
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 0x00007efddc29f700 (most recent call first):
<no Python frame>
```

I am a bit surprised about that because this seems to be the right version of python.
The only thing which is strange:
I would have expected site-modules and no lib64.
In fact, if i run python in the condaenvironment directly,
i obtain another path:
```
['',
'/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib/python38.zip',
'/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib/python3.8', '
/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib/python3.8/lib-dynload',
/home/ernst/.local/lib/python3.8/site-packages',
'/home/ernst/.conda/envs/envForMbedOs5.15.1Py3.8.3/lib/python3.8/site-packages']
```
not talking about details but now the python3.8 site-packages are present as expected.

I have the impression, my python invoked from within java uses fallback modules
from the outer installation... is this possible??

Ben Steffensmeier

unread,
Jun 11, 2020, 9:20:18 PM6/11/20
to Jep Project
Where is your encodings module defined? I've noticed that in some virtual environments there is no encodings module so it makes sense that you cannot find encodings when the virtual environment is PYTHONHOME. It seems like when PYTHONHOME is set that only modules within the virtual env are used but without PYTHONHOME it is able to find modules such as encodings that aren't in the virtual environment. The only solution I have found is to avoid setting PYTHONHOME(Or calling setPythonHome) when using jep and instead use other methods to find the libraries such as java.library.path and LD_LIBRARY_PATH.
Reply all
Reply to author
Forward
0 new messages