You're using your server IP for geolocation. When you visit that particular endpoint, it takes the client IP addresses, so when you deployed your app to Pythonanywhere - the client to that endpoint becomes the server.
When you were running the locally on your PC, you were requesting the API endpoint using your IP address - so if you switched to VPN, your IP address also changed.
In order to fix your current code, you need to pass the IP address of the client to freegeoip.net/json
endpoint. Try this:
from flask import request
@app.route('/')
def index():
url = 'http://freegeoip.net/json/{}'.format(request.remote_addr)
r = requests.get(url)
j = json.loads(r.text)
city = j['city']
print(city)
However, keep in mind that this API endpoint is deprecated and will stop working on July 1st, 2018. For more information please visit: https://github.com/apilayer/freegeoip#readme - you may want to start using their new service - my solution is still applicable to their new API.
In some cases (like in Pythonanywhere) there might be a proxy server in front of your web app, rendering the request.remote_addr
useless. In that case you should use the X-Forwarded-For
header. Remember to use the correct value though. Have a look at this good solution for more info.