Forums

virtualenv - 'import' django imports different django version

Hello everyone :) .. I get the following error:

AttributeError: 'module' object has no attribute 'setup'

The error is prodduced when executing the last line of this (this is my wsgi.py file):

activate_this = '/home/tupini07/.virtualenvs/django17/bin/activate_this.py'
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))

import os
import sys

path = '/home/tupini07/Friendonator'
if path not in sys.path:
    sys.path.append(path)

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

import django
django.setup()

I know that older versions of django thank 1.7 don't have the atribute 'setup' so I'm assuming that I'm not activating properly the virtualEnv (which works great if activated from the console). I would really appreciate if someone could help me. thanks!

try adding a print after the import, to see where python is finding the module?

import django
print(django, file=sys.stderr)
print(sys.path, files=sys.stderr)
django.setup()

You'll be able to see the resuts of the prints in your error log...

ok it says that Django is being loaded from:

<module 'django' from '/usr/local/lib/python3.4/dist-packages/django/__init__.py'>

And the System Path is:

['/home/tupini07/.virtualenvs/django17/lib/python3.4/site-packages', 
'/var/www', '.', '', '/usr/local/lib/python3.4/dist-packages/setuptools-5.7-py3.4.egg',
'/usr/local/lib/python3.4/dist-packages/matplotlib-1.3.1-py3.4-linux-x86_64.egg',
'/usr/local/lib/python3.4/dist-packages/certifi-14.05.14-py3.4.egg', '/var/www', 
'/usr/lib/python3.4',
'/usr/lib/python3.4/plat-x86_64-linux-gnu', 
'/usr/lib/python3.4/lib-dynload', 
'/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python3/dist-packages', '/
home/tupini07/Friendonator']

So it looks like the virtualenv isn't being activated correctly? Does the same thing happen if you run the script with python -i, as in https://www.pythonanywhere.com/wiki/DebuggingImportError

Yes when I run the script using 'python -i' from my bash console I get the same error:

AttributeError: 'module' object has no attribute 'setup'

So it does sound like that activate command isn't working.... Can I have permission to look at your files?

Yes. I will be more than glad if you do so :)

Looks like you've created your virtualenv using Python 3.3, but the web app is configured to use Python 3.4?

try deleting and rebuilding the virtualenv, with --python=/usr/bin/python3.4?

I tried

mkvirtualenv --python=/usr/bin/python3.4 django7

and it gave me the following output:

Running virtualenv with interpreter /usr/bin/python3.4
Using base prefix '/usr'
New python executable in django7/bin/python3.4
Not overwriting existing python script django7/bin/python (you must use django7/bin/python3.4)
Failed to import the site module
Traceback (most recent call last):
File "/home/tupini07/.virtualenvs/django7/lib/python3.4/site.py", line 67, in <module>
import os
File "/home/tupini07/.virtualenvs/django7/lib/python3.4/os.py", line 614, in <module>
from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable django7/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/home/tupini07/.virtualenvs' (should be '/home/tupini07/.virtualenvs/django7')
ERROR: virtualenv is not compatible with this system or executable

Any suggestions ?? Thanks

Yes, you need to delete the old virtualenv with rmvirtualenv, or give the new one a different name.

hmm yes that was a different name, but I also tried deleting and different names and still the same problem :/

It looks like there's a bug in the version of virtualenv that we have installed that prevents it from working with Python 3.4. We'll look at getting a fixed version installed. In the meantime, you can do

pip install --user -U virtualenv

and then Harry's suggestion will work.

thank you!! now it's aactivating correctly the virtualenv and everrything! I'm really greatfull for your help :) . Now I'm receiving a different error telling me

ImportError: No module named 'gi._gi'

Searching around I found that it's a bug for python3.4 and a common workaround is to switch to python3.2. Do you guys know if there is something to be done or should I just switch to python3.2??

Thanks

gi._gi seems to be part of GTK and I don't believe that we have that installed for Python 3. We can have a look at getting that installed, but it will be a few weeks at least.

thanks guys!! I got it to work :D just removed my need for 'gi'.

just removed my need for 'gi'.

Hi, tupin07, I had the same issue (Django1.7, Python3.4). Just wondering how you accomplished that.

Well, I was following the begginers django tutorial and there is a part in the tutorial where the ask you to import some GI package (I really don't remember which one). This import I had in my views.py file. If you open your site's error log then you can see where are you doing this import.

Hope it helps!!

Ah, yes. Using an IDE instead of VIM and it pulled in the gi module for no good reason. Thanks! Everything is working! Happy hacking!