-
Notifications
You must be signed in to change notification settings - Fork 115
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
ModuleNotFoundError: No module named 'xxx' and NameError: name 'xxx' is not defined #468
Comments
I don't understand your file layout, what is There is already a module named |
I tried to modify the file name of java.py for java execution |
Here's what I tried: import b
def a():
b.b()
def b():
xxx()
def xxx():
print('hi')
try (Context context = GraalPyResources.createContext()) {
context.eval("python", "from a import a");
context.getBindings("python").getMember("a").execute();
} That just prints |
Attempts to call the Main.java: a.py:
b.py:
c.py:
|
Well, you really don't define |
a.py is used to prompt, or to execute kotlin methods, because I use |
The modules have their own independent globals. context.eval("python", "import a");
Value aModule = bindings.getMember("a");
aModule.putMember("stats", ScriptStats); |
So if it's python, you can't use import in.py files, or you might have problems like mine? If I can't use context. eval("python", "import a ") in kotlin, how can I circumvent this problem? |
Of course you can use import. I showed you how to fix the |
Due to software design, I can't use |
But you can import |
Sorry, I can't add or modify the backend |
Why can't you import |
The back-end code has |
So you don't import |
sources is all of py code files
|
Don't eval all the files like this, you end up having the same module evaluated twice because they also import each other. For example, first you eval the source Instead of evaling the sources, add the directory with the sources to python path and then import them by names. |
I move the addStatsGroup method in a.py to b.py, and it works. why? |
Here's a demo that works with the 3 files you posted: package com.example;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
public class Main {
public static class StatsGroup {
public void addStatsGroup(String str) {
System.out.println(str);
}
}
public static class ScriptBase {
public Object traceContext() {
return "something";
}
public void logInfo(Object tc, String message) {
System.out.println(message);
}
}
public static void main(String[] args) {
try (Context context = Context.newBuilder("python")
.allowAllAccess(true)
.option("python.PythonPath", System.getProperty("user.dir"))
.build()) {
Value bindings = context.getBindings("python");
context.eval("python", "import a");
Value aModule = bindings.getMember("a");
aModule.putMember("stats", new StatsGroup());
aModule.putMember("base", new ScriptBase());
context.eval("python", """
import b
b.boot()
""");
}
}
} It looks for the python files in the current directory. If you use the maven or gradle plugins, you can also put them in |
I called python in kotlin and started reporting ModuleNotFoundError: No module named 'xxx'.
I added
The text was updated successfully, but these errors were encountered: