Forums

Confused on how to use Virtual Env in Pythonanywhere

Hi all,

I cannot get my head around using virtualenv's in PyA.

Usually when I create virtual env in my laptop, I create it using

virtualenv my_demo_env

which then creates a directory structure

my_demo_env/
            bin/
            Scripts/
            and so on/

then I can create run.py in the root of my_demo_env and say python run.py to run my app. Please correct me if I am wrong. I recently got used to using virtualenv's

But in PythonAnywhere, you create a virtualenv, like above, the folder gets created here :

/home/username/.virtualenvs

no matter how many you create all of them end up here

/home/username/.virtualenvs /my_env/ /my_other_env/

and /my_env seems to have 
/bin
/include
/lib

so I believe you are not supposed to have your app in under /my_env but it should be in the same level as my_env?

and the directory .virtualenvs directory has run.py and someother files....

When I have multiple virtualenv's like this, how can I run any specific app ?

Hope my question is clear....

You may be thinking of two commands here.

One is mkvirtualenv, which is a command from virtualenv wrapper, meant to be a convenience wrapper around virtualenv (what you are used to using). For example, virtualenv wrapper allows you to "activate a virtualenv", and when you are inside of it, you can just say python and that will magically be the python that is within your virtualenv.

The actual source code for your website -- the python and html files you write don't need to and probably should not be placed in the same folder as your virtualenv. Instead, to call it, either use the full path (eg: ~/my_env/python ~/my_site/run.py) or if you have "activated your virtualenv", then just python will suffice.

Here's a quick guide on how to create a virtualenv just in case.

Thanks Conrad.

I went through the link and its a bit more clear now.

I activated VE on myaccount, but it cannot open the webpage but says something's wrong.

These are the settings at the moment:

Code:
What your site is running.

Source code:
/home/susarla/.virtualenvs/myvirtualenv/flask_demo_app_dir
Go to directory
Working directory:
/home/susarla/.virtualenvs/myvirtualenv/flask_demo_app_dir
Go to directory
WSGI configuration file:/var/www/susarla_pythonanywhere_com_wsgi.py
Python version:3.5

I've edited _wsgi.py file and add this code:

import sys

# add your project directory to the sys.path
#project_home = u'/home/susarla/mysite'
project_home = u'~/.virtualenvs/myvirtualenv/flask_demo_app_dir'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from flask_demo_app_dir import app as application

I've added "flask_demo_app_dir". this is what I created when using virtualenv. Hope this is correct... and in VirtualEnv this is what I added :

Virtualenv:
Use a virtualenv to get different versions of flask, django etc from our default system ones. More info here. You need to Reload your web app to activate it; NB - will do nothing if the virtualenv does not exist.

/home/susarla/.virtualenvs/myvirtualenv

 Start a console in this virtualenv

Does it seems correct to you ?

I you want to have a look at the log :

Access log:susarla.pythonanywhere.com.access.log
Error log:susarla.pythonanywhere.com.error.log
Server log:susarla.pythonanywhere.com.server.log

Ok. Finally I figured it out and its working now. It would be good if the app reloads itself when there are code changes...

Is there any feature request page for Py?

is it possible to hook pythonanywhere to some CI systems?

Yup- the problem with auto-reloading is that then you can't make any changes to your code/settings etc without reloading the site (which will may be bad for actual live/production sites).

What do you mean feature request page?

It is definitely possible to hook into a CI system. Most CI systems work with git (which you can use from the bash console etc). So if you setup your project with git, then travis, circle etc will all work.

Hi, I am having problems knowing how to proceed. I have set up my virtualenv;

(myvirtualenv) c:\python34>

I have a machine learning code I couldn't run on my laptop due to memory issues...I want to run this code in the cloud...how do I proceed? Ideally, I need a shell to run in the cloud.

Please help as I am a newbie

Wait- did you setup your virtualenv on your local machine?

A virtualenv is specific to an app on a particular machine and contains the libraries and packages your app depends on. For example, you might create a virtualenv for your webapp on your local machine and install a particular version of flask, django, or requests, etc into it, or you might then create a second virtualenv on your local machine for another project that you are working on.

It's not something you can copy from one machine to another, as different machines have different architectures (eg: windows vs linux, 64bit vs 32bit). Instead, you should make a list of all the libraries installed, save it as a requirements.txt, and after you create a fresh virtualenv on each machine you use, install the packages into it by referencing that requirements.txt. This can all be done within a couple commands as seen at the bottom of this intro here http://www.jontourage.com/2011/02/09/virtualenv-pip-basics/.

Thank you for your response.

Next one; I have a pythonanywhere account and want to set up a virtual env for a project on it. Could you please give me a step by step guide on how to do this and where...

This is urgent please. Thanks.

I am used to python in a windows environment.

This one that I referenced earlier is for linux environments.

https://help.pythonanywhere.com/pages/Virtualenvs

Hi! I found a generic guide on how to setup a virtualenv . That guide is not for python anywhere especific, but it help me a lot!

Thanks

Help ... What is it that I don't know or understand? My app is Flask with mysql ... python 3.6 It all worked before I went to virtualenv . Now, under virtualenv database SELECTs often fail when responding to ajax calls. (ajax proven to work)

environment string is properly set I think: /home/peterl3233/.virtualenvs/myvirtualenv/

in virtualenv I did "pip install mysqlclient" and installed Flask

from flask import Flask, session, redirect, app, render_template, request, url_for,json
from flask import jsonify
from flaskext.mysql import MySQL
import re
import os
from passlib.hash import sha256_crypt
from functools import wraps
import sys
import time
from helpers import mytest, locate
import requests

app = Flask(__name__)
app.config["DEBUG"] = True
mysql = MySQL(app)
app.config['MYSQL_DATABASE_USER']     = 'peterl3233'
app.config['MYSQL_DATABASE_PASSWORD'] = '
app.config['MYSQL_DATABASE_DB']       = 'peterl3233$mesh'
app.config['MYSQL_DATABASE_HOST']     = 'peterl3233.mysql.pythonanywhere-services.com'
mysql.init_app(app)
conn = mysql.connect()  
db = conn.cursor()

And set wsig file to: :::python import sys

path = '/home/peterl3233/mysite'
if path not in sys.path:
   sys.path.append(path)

from flask_app import app as application

Maybe there is something else I need to do to stabilise the database?

What is the exact error and error traceback that you are seeing with SELECTs?

HI ... no errors at all. So .. after more discovery I see that the query works and returns appropriate values EXCEPT the fields that contain DECIMAL(M,D) fields. When I retrieve all the fields like in the example below, it quietly (no errors) returns empty fields. I tried playing around (widening) with the DECIMAL declaration syntax .. like DECIMAL(14,7) and that didn't work but changing them to floats does.

So far as I know its a pretty normal query and it worked fine before I went virtualenv.

    if qtype == "gethomelatlong" and table == "networks":
    try:
        db.execute("SELECT * FROM networks WHERE (status = %s and userid = %s)",("selected",uid))
        data = db.fetchone()
        return jsonify(data)
    except:
        if data == None:
            return jsonify("No data")

I wonder; Could it be that somehow I am connecting to a wrong version of pyMySQL? So far as I can figure out, this is the defining documentation: https://pypi.python.org/pypi/PyMySQL

If you are changing the DB schema, you will have to migrate your db/change your db tables accordingly. Perhaps you didn't do that previously, and trashed the decimal field?

Yes .. I get that; Hmmmm ... there is no change in schema and the same code works as expected in non-virtualenv. In virtualenv I can isolate all the field one by one with data[0], data[1] etc .. (simple array) and that works until I hit the DECIMAL ones .. they show up as blank. When I change them to FLOAT in the schema, it works fine ... but I should not need to - Its just wrong !

I'd appreciate your opinion on the correct syntax for mysql setip on flask on Pythoneverywhere. Here is how I have it now: maybe i am just missing something really stupid?

from flask import Flask, session, redirect, app, render_template, request, url_for,json
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL(app)
app.config['MYSQL_DATABASE_USER']     = 'peterl3233'
app.config['MYSQL_DATABASE_PASSWORD'] = 
app.config['MYSQL_DATABASE_DB']       = 'peterl3233$mesh'
app.config['MYSQL_DATABASE_HOST']     = 'peterl3233.mysql.pythonanywhere-services.com'
mysql.init_app(app)
conn = mysql.connect()
db = conn.cursor()

Some posts seem to say that "from flaskext.mysql import MySQL" is depreciated but Flask documentation seems to think its right. Maybe I need to PIP install a different module? Do you think there may be a different module for flaskext that maybe I can't find. So far I have only needed to PIP install Flask?

I did do "pip install mysqlclient" and I use python 3.6

Thanks for your time on this

If setting the database field to DECIMAL returns a Python Decimal object, then it's possible that the Decimal object is not being jsonified (the Python json module does not necessarily jsonify objects in a way that you expect). Look at the database response before you jsonify it and see. That would also explain why it works when you set the field type to FLOAT, since I would expect that to return a Python float which will jsonify.

Thanks for this .. I'll check for sure .. and get back ... ( just temporarily busy with another issue)

Hmmm, one thing that occurs to me -- are you using a virtualenv? If so, try installing simplejson into it. Someone else reported a similar-sounding problem over email, which occurred in one environment and not in another, and it turned out to be caused by that package not being present in the environment with the problem.

(I ask about the virtualenv because simplejson is installed globally on PythonAnywhere, so if you're not using one, it's probably not the cause in this case.)