1

I want to use PythonInterpreter in Java code to remove file into recycle bin.

This is my Python code:

from send2trash import send2trash

send2trash('C:\Slides\dog.jpg')

This is my Java code:

import org.python.util.PythonInterpreter;

public class Main {
    public static void main(String[] args) {
        PythonInterpreter py = new PythonInterpreter();
        py.eval("from send2trash import send2trash");
        py.exec("send2trash('cat-2.jpg')");
    }
}

This is the error:

Exception in thread "main" Traceback (most recent call last):
File "", line 1, in
NameError: name 'send2trash' is not defined

My Python code works correctly, but not in Java.

The Python environment is installed correctly because the Python code works correctly.

1 Answer 1

0

I don't think you are supposed to execute line by line. Try instead to pass the whole string to py.exec :

py.exec("from send2trash import send2trash \nsend2trash('cat-2.jpg')");
Sign up to request clarification or add additional context in comments.

7 Comments

Exception in thread "JavaFX Application Thread" SyntaxError: ("no viable alternative at input 'from'", ('<string>', 1, 0, 'from send2trash import send2trash\n'))
try py.exec("import send2trash \nsend2trash.send2trash('cat-2.jpg')"); instead, maybe it does not support this syntax.
File "<string>", line 1, in <module> ImportError: No module named send2trash
The problem now is that this module is not found. How did you install this package ? Where is it located ? You did not provide enough info about that, and it is actually another problem.
The actual path with which the code works in Visual Studio is: C:\ProgramData\Anaconda3\Lib\site-packages\send2trash\plat_win_legacy.py
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.