Unable to run Python on a hosted server

Viewed 131

I am trying to run python on a hosted server. I'm using a Strato server.

The problem I have is that, when I run my Python code, it uses the Python 2.7 version - as it is set the default, and I've installed my python libraries in Python 3, so it shows an "500 Internal Error".

I tried to change the python path at the beginning of my python file to the Python 3 location, but it didn't work.

So, my question is, how can I run Python 3 (and not python 2) when opening the file?

Here's my code:

#!/usr/bin/python3 

import requests

print("Content-Type: text/plain;charset=utf-8")
print()

print("Hello World!")

EDIT: I looked into the question that Maurice suggested, and checked my logs. As I supposed, the error says "No module named 'requests'", but I have it installed in Python 3, so it doesn't solve the problem

1 Answers

If you have python3 installed in the server, you can run using python3 [filename] instead of python [filename].

This points to the python3 that is installed in the machine and should give you the correct behaviour.

Edit: Oh, got it. I don't know if Stratos servers have a special feature for running python scripts remotely like you are trying.

I see that you have a php background where it can be done easily, but to do it with python you would need to create a web API in order to expose your code on the web.

The most simple way to achieve this is using Flask. Flask is a simple web framework that allows you to expose your code like you are describing.

You can check the documentation here. Also, you can use this tutorial to implement the API.

Related