Java repl has messed up Python environment

Question: Why can’t I install Python packages in a Java repl?

I understand that it’s a Java repl, not a Python one. However, it would still be really useful to be able to install some Python packages.

Specifically, I am trying to find a getch alternative, because the getch that I normally use doesn’t seem to work in Replit.

Repl link: https://replit.com/@JasonRoman/FreakingMath

Here’s some things I tried:

python3 -m pip install ... # No module named pip
python3 -m ensurepip --user # success
python3 -m pip install ... # No module named pip... wait, what?
pip # worked, showed help message for pip.
pip install ... # Can not execute `setup.py` since setuptools is not available in the build environment.
pip install setuptools --user # success
pip install ... # Same error as before.

Hi @sillypantscoder you wouldn’t be able to use the Python package from Java.

I’m assuming you want to use getch (the Python package) to capture keypresses? If so have you investigated using Java keylistener events instead?

https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

I wrote my own getch in Python, not knowing about the builtin package.
The keylistener events look like they work on a window, not in the terminal.
I just started rewriting my program in Java:

class Getch {
	public static void main(String[] args) {
		// Example:
		stopBuffering()
		String g = getch();
		System.out.println(g);
		// this is a working solution!
		// this does not require the enter key to be pressed!
		// The only thing is that the arrow keys return "A", "B", "C", and "D"
		// instead of a unique value
	}
	public static void stopBuffering() {
		try {
			ProcessBuilder pb = new ProcessBuilder("stty", "-icanon", "min", "1");
			pb.inheritIO();
			pb.start().waitFor();
		} catch(IOException e) {
			System.out.println(e);
		} catch(InterruptedException e) {
			System.out.println(e);
		}
	}
	public static String getch() {
		try {
			// Read a byte
			byte[] i = new byte[4];
			System.in.read(i);
			byte[] retBytes = new byte[] {
				(byte)(i[0])
			};
			System.out.print("\u001b[1D");
			// Check if byte is escape code
			if (i[0] == 27) {
				System.out.print("\u001b[3D");
				retBytes = new byte[] {
					(byte)(i[2])
				};
			}
			System.out.print("\u001b[0K");
			String str = new String(retBytes, StandardCharsets.UTF_8);
			return str;
		} catch(IOException e) {
			return "";
		}
	}
}

Ok. Best of luck with this! I know from some research that it’s a common question online and I’ve not yet found a solution that doesn’t require the ENTER key to be pressed at some point.

There will be a lot of happy programmers out there if you can solve this!