"module not found" when trying to run python script remotely

Viewed 490

Please bear with me, I'm a complete noob and have no idea what I'm doing.

I have a python project that I'd like to run on a remote server. It consists of maaaany files that I'm importing at the start of the main file I want to run.

There's a server I can ssh into and run things on, but if I try running

ssh [remote server] python3 <./my_script.py 

I get "ModuleNotFoundError: No module named helperfunctions.py", which is the first thing I'm trying to import.

I'm assuming the problem is that I'm trying to run my_script remotely while all files that it accesses are still stored locally. How can I resolve this?

I'll need explanations to be very basic because I really have no idea about any of this = (

Thanks!

3 Answers

You need to install the python packages you need on the server, for example with pip

You seem to be importing files/modules/libraries that are available locally, either in the same folder as the my_script.py file or in a path thats registered in the environment - search for that " helperfunctions.py" one to see where they are if not in the same folder, and then try copying all of them in the same folder on the server where you're trying to run the my_script.py file.

The error indicates that you have an extra file locally helperfunctions.py. You need to send this in the remote machine as well and make sure you have similar directory structure as you do locally -in your working environment.

Also you need to check if you have all the installations of the external packages that you maybe call in your scripts.

This is what you could do if you have not installed the packages in the remote server.

Create a file near your my_script.py and call it requirements.txt In this file add line by line the packages and the versions that you use. For example its file contents could be:

Flask-Admin==1.5.4
black==20.8b1
blinker==1.4
urllib3==1.25.11

Note that you can add packages without version specifiers. e.g

beautifulsoup4

Then you can execute in your remote server

ssh [remote server] pip install -r requirements.txt

This will install all the packages which you have declare to the file in your remote machine.

Of course you can go over them one by one and install them the above is not mandatory.

Related