0

I'm trying to execute a python function, written inside a .py file from my java project (spring boot) deployed to AWS EC2.

At start I located the .py file in the resource folder, but it didn't work properly so I uploaded the .py file to S3 and I download it from there every time I need to.

I use the following to run the python file:

Process processBuilder = Runtime.getRuntime().exec("python word2vec.py");
   

Locally it works great, but since word2vec.py has dependencies, such as gensim, when executed in the EC2 instance (linux based) I get the error "ImportError: No module named gensim", which makes sense since I didn't install it on the machine.

Using IntelliJ plugin for python I created a virtual environment in the project and installed gensim so it works locally, is there a way to do it in the EC2 instance?

Since the project is packed into a jar file, even if the .py is located in the resource folder, the "Runtime.getRuntime().exec(...)" function can't access it, I get "file not found".

Is there a way to keep everything inside the java project/jar? or I need to install python virtual environment manually on the EC2 instance?

Itai Soudry
  • 338
  • 1
  • 6
  • 17

1 Answers1

0

I couldn't find a way to run the .py files from the resources, so I uploaded them to S3 and I download them before I want to run those scripts. Since the .py files are really small it works really fast.

In addition, I had to download and install python virtual environment on my EC2 Java machine.

This blog helped me understand python virtual env.

And this question for how to run a file using the python version in the virtual environment.

Itai Soudry
  • 338
  • 1
  • 6
  • 17