-
Notifications
You must be signed in to change notification settings - Fork 152
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
MacOS M4 - ModuleNotFoundError: No module named 'encodings' #572
Comments
I don't have time to look closely right now but some quick thoughts to help you troubleshoot
|
Thanks @bsteffensmeier but if I do not set Python home in my Java code, then I get: No module named 'jep' |
By setting python home you are causing errors earlier in initialization and don't make it to the code that tries to load jep. I am able to replicate the encoding problems in python without jep by using a venv and setting python home. Since the behavior of jep in a venv with python home set matches the behavior of python I do not think this is a problem jep should solve. bsteffensmeier@mac-mini ~ % python3 -m venv --system-site-packages /Users/bsteffensmeier/venvs/default
bsteffensmeier@mac-mini ~ % source ~/venvs/default/bin/activate
(default) bsteffensmeier@mac-mini ~ % export PYTHONHOME=/Users/bsteffensmeier/venvs/default
(default) bsteffensmeier@mac-mini ~ % python3
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000001eee37ac0 (most recent call first):
<no Python frame> After I #!/bin/sh
. /Users/bsteffensmeier/venvs/default/bin/activate
export PYTHONEXECUTABLE="/Users/bsteffensmeier/venvs/default/bin/python3.13"
jep_dir=/Users/bsteffensmeier/venvs/default/lib/python3.13/site-packages/jep
if [ ! -d "$jep_dir" ]
then
jep_dir=/Users/bsteffensmeier/venvs/default/lib/python3.13/site-packages/jep-4.2.1-py3.13-macosx-10.13-universal2.egg/jep
fi
cp="$jep_dir/jep-4.2.1.jar"
if test "x$CLASSPATH" != "x"; then
cp="$cp":"$CLASSPATH"
fi
jni_path=$jep_dir
args=$*
if test "x$args" = "x"; then
args="$jep_dir/console.py"
fi
exec java -classpath "$cp" -Djava.library.path="$jni_path" jep.Run $args Since jep detected a venv during install it is activating the venv and also setting PYTHONEXECUTABLE, both steps your jep script is missing. When I remove the line that set PYTHONEXECUTABLE then I get |
@bsteffensmeier Thanks a lot for this test! If I do not call pyConfig.setPythonHome in Java an set PYTHONEXECUTABLE environment variable before running my Java program it works. Questions: Is there a way to set PYTHONEXECUTABLE via i.e. PyConfig? Is it a separate defect/issue that "pip install jep" does not generate a proper script on MacOS? |
There is currently no way to set PYTHONEXECUTABLE from Java. Looking through the Python c-api I don't see an api for setting that exact option either. In the c-api there is an option to set the program name and it looks like this could be used for the same purpose. We have already had a request to set the program name(#515) and I plan to make that available from java in a future release which may help you. In the mean time the "No module named jep" seems to just be a problem with
The problem is that pip will cache any package you install and reinstall the cached package if you install again, even if the install is in a different venv or not in a venv at all. In your case it looks like your initial build was not in a venv and when you later installed in a venv it reused the jep script without the venv options. I can't find anyway to control this behavior for our project, it is a pip setting and something that they haven't done until recently. If you uninstall jep and install again using the |
Thanks again!! It works perfectly now with setIncludePath and jep script works also after reinstalling with the no cache dir open! |
Problem:
I try to run a Python script from my Java app (similar code works for me on Windows).
I am using a virtual environment for my python setup. This is also set as the default via .zshrc.
Below is the Java code I use and the exact error. I read through all similar issues here but none of the hints worked so far.
Happy to do some debugging or troubleshooting if that helps improving jep.
Setup steps taken:
python -m venv --system-site-packages /Users/stefan/venvs/default
source ~/venvs/default/bin/activate
pip install jep
pip list
--> shows jep 4.2.1
Java is 17 or 21, tested both
Java sample used to reproduce:
Error:
Environment (please complete the following information):
Not sure it matters or is related, but I noticed that the jep script in ~/venvs/default/bin is not correct for my machine. there is no jep jar in the referenced pipx jep dir (/Users/stefan/.local/pipx/venvs/jep/lib/python3.13/site-packages/jep) . This folder does not exist and therefore running jep causes a ClassNotFoundException.
If I set jep_dir in that script to /Users/stefan/venvs/default/lib/python3.13/site-packages/jep (where the jar is), then i get:
JEP Script
The text was updated successfully, but these errors were encountered: