Forums

Python3.3 VirtualEnv does not activate

I'm trying to run a Django app inside a Python3.3 virtualenv

I have followed the instructions at https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango and have this in my wsgi-file:

activate_this = '/home/alcesleo/.virtualenvs/Django/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

But when I try to run my app, the page shows "Unhandled Exception" and this is in the error log:

2013-11-19 09:07:10,742 :Traceback (most recent call last):
2013-11-19 09:07:10,754 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 250, in __call__
2013-11-19 09:07:10,755 :    self.load_middleware()
2013-11-19 09:07:10,755 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 47, in load_middleware
2013-11-19 09:07:10,755 :    raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
2013-11-19 09:07:10,755 :django.core.exceptions.ImproperlyConfigured: Error importing middleware django.middleware.clickjacking: "No module named clickjacking"
2013-11-19 10:09:47,218 :Traceback (most recent call last):
2013-11-19 10:09:47,221 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 250, in __call__
2013-11-19 10:09:47,221 :    self.load_middleware()
2013-11-19 10:09:47,221 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 47, in load_middleware
2013-11-19 10:09:47,221 :    raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
2013-11-19 10:09:47,221 :django.core.exceptions.ImproperlyConfigured: Error importing middleware django.middleware.clickjacking: "No module named clickjacking"

It's clearly trying to run with python2.7 when it should use python3.3 in the virtualenv. Have I configured it wrong somehow?

Python 3 is not supported for web apps I'm afraid, virtualenv or nay. It is high up on our to-do list though...

Oh, that explains it. Do you have an idea of when it will be available?

I'm afraid I can't make any promises at the moment...

That's okay. Thank you very much for the fast responses, keep up the good work!

Our webapps are Python 3.3 - 0.6 = 2.7...☺

Python 3 webapps went live today :-D

That is awesome!!

:-D

DANCES

So how do we do virtualenvs for python 3?

Exactly the same way as for Python 2, just choose Python 3 when you're going through the quickstart for your web app.

@glenn, that cannot work. There is no 'activate_this.py' file in virtualenv module for python 3.

How did you create the virtualenv? It looks like the virtualenvs created with the python 3 venv module don't have activate_this.py, but if you create the virtualenv with virtualenv-3.3, the virtualenv does have an activate_this script.

Also, Python 3 has removed execfile, so you can't use that directly. I have updated the wiki page with the Python 3 version.

I'm trying to get my web app up and running but having problems getting the virtualenv to work. I developed using Python 3.4, Django 1.6.5 and DjangoRestFramework 2.3.14. I have created a virtual env using below which works fine:

python3.4 -m venv env_name --without-pip
source env_name/bin/activate
python3.4 -m ensurepip

Then edited the wsgi.py file in my app to use the new virtual env. I had to copy the activate_this.py from python3.3. Then edited wsgi.py file in /var/www to pickup my app settings. Now if I set the webapp up choosing python2.7 I get:

"No module named clickjacking"

If I which to using python3.4 I get:

"No module named rest_framework"

Any further ideas for me to get this up and running? I dont think its picking up any virtual env.

There are some problems with virtualenvs in Python 3.4 right now -- do you think you could use Python 3.3?

ok - will give that a try. thanks

No problem!

Moved to a virtualenv with python3 with django, djangoframe. I can use the virtualenv from the cmdline with no probs.

But I still cant get the webapp to pickup this virtualenv. Anyone got any ideas?

You definitely used virtualenv or virtualenvwrapper to create the new virtualenv? And you've got the activate_this at the top of your wsgi file? If so it should work... If it's not working, what are the symptoms?

Yeah - created as per instructions from the Help using virtualenvwrapper.

import os
import sys

# assuming your django settings file is at '/home/copenat/mysite/settings.py'
path = '/home/copenat/JugaadMaster/'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'jugaad.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

/home/copenat/JugaadMaster/jugaad/wsgi.py :

activate_this = '/home/copenat/.virtualenvs/python33/bin/activate_this.py'
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))
#execfile(activate_this, dict(__file__=activate_this))

import os
import sys

path = '/home/copenat/JugaadMaster/'
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jugaad.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

When I connect with my browser I see the following exceptions raised in the error log: .... AttributeError: 'Settings' object has no attribute 'r' ..... ImportError: No module named 'rest_framework'

The file /home/copenat/JugaadMaster/jugaad/wsgi.py isn't used on PythonAnywhere -- you need to put that stuff into the WSGI file linked from the "Web" tab, in /var/www/copenat_pythonanywhere_com_wsgi.py.

Hi everyone, we've just released a new way to handle virtualenvs which should avoid the system-site-packages / shadowing issues. The old activate_this / exec system is now deprecated -- it should keep working, but the new version is better.

There's an overview here and a guide switching across from the old system here