1

Im having problems with python versions, im actually developing a web page with python3.5 under Windows 7. But in my server (CentOS 7) i created a virtualenv with python3.5 (because the default version of python in linux is 2.7).

The problem is that when i get an error, it says that django is using python2.7:

Request Method: GET
Request URL:    http://proyect/url/
Django Version: 1.9.8
Exception Type: UnicodeEncodeError
Exception Value:    
'ascii' codec can't encode character u'\xed' in position 9: ordinal not in range(128)
Exception Location: /usr/lib/python2.7/site-packages/django/utils/encoding.py in force_text, line 80
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Python Path:    
['/home/user/proyect',
 '/home/user/proyect_env/lib/python3.5/site-packages',
 '/usr/lib64/python27.zip',
 '/usr/lib64/python2.7',
 '/usr/lib64/python2.7/plat-linux2',
 '/usr/lib64/python2.7/lib-tk',
 '/usr/lib64/python2.7/lib-old',
 '/usr/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7/site-packages',
 '/usr/lib64/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages']

I'm almost 100% sure that this message is displayed because django is using a wrong python version.

In my django.conf inside /etc/httpd/conf.d/ i have this configured:

WSGIDaemonProcess proyect python-path=/home/user/proyect:/home/user/proyect_env/lib/python3.5/site-packages
WSGIProcessGroup project
WSGIScriptAlias / /home/user/proyect/proyect/wsgi.py

I followed this tutorial to configure my server.

Edit #1 After following @Ixer indications, i got this error traceback in /etc/httpd/logs/error_log

7:49:05.114720 2016] [:error] [pid 14836] [remote 10.105.40.106:49676] mod_wsgi (pid=14836): Exception occurred processing WSGI script '/home/user/proyect/proyect/wsgi.py'.
[Tue Sep 20 17:49:05.114779 2016] [:error] [pid 14836] [remote 10.105.40.106:49676] Traceback (most recent call last):
[Tue Sep 20 17:49:05.114810 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 158, in __call__
[Tue Sep 20 17:49:05.114862 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     self.load_middleware()
[Tue Sep 20 17:49:05.114883 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 51, in load_middleware
[Tue Sep 20 17:49:05.114910 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     mw_class = import_string(middleware_path)
[Tue Sep 20 17:49:05.114926 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/utils/module_loading.py", line 20, in import_string
[Tue Sep 20 17:49:05.114951 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     module = import_module(module_path)
[Tue Sep 20 17:49:05.114966 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
[Tue Sep 20 17:49:05.114991 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     __import__(name)
[Tue Sep 20 17:49:05.115007 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 3, in <module>
[Tue Sep 20 17:49:05.115031 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     from django.contrib.auth.backends import RemoteUserBackend
[Tue Sep 20 17:49:05.115046 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/backends.py", line 4, in <module>
[Tue Sep 20 17:49:05.115070 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     from django.contrib.auth.models import Permission
[Tue Sep 20 17:49:05.115085 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
[Tue Sep 20 17:49:05.115109 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
[Tue Sep 20 17:49:05.115124 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
[Tue Sep 20 17:49:05.115148 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     class AbstractBaseUser(models.Model):
[Tue Sep 20 17:49:05.115163 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
[Tue Sep 20 17:49:05.115187 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     app_config = apps.get_containing_app_config(module)
[Tue Sep 20 17:49:05.115203 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
[Tue Sep 20 17:49:05.115226 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     self.check_apps_ready()
[Tue Sep 20 17:49:05.115241 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]   File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
[Tue Sep 20 17:49:05.115263 2016] [:error] [pid 14836] [remote 10.105.40.106:49676]     raise AppRegistryNotReady("Apps aren't loaded yet.")

Edit #2

The problem was solved, the situation was that when i was installing apps via pip install -r requirements.txt, for some reason they were installed under python 2.7 (even when i sourced the virtualenv activate). So what i did was to install again the apps via /home/user/project_env/bin/pip install -r requirements... this fixed the error but now im having problems with an app:

[Tue Sep 20 21:34:49.998172 2016] [:error] [pid 18220] [remote 10.105.40.106:172] mod_wsgi (pid=18220): Target WSGI script '/home/user/project/project/wsgi.py' cannot be loaded as Python module.
[Tue Sep 20 21:34:49.998207 2016] [:error] [pid 18220] [remote 10.105.40.106:172] mod_wsgi (pid=18220): Exception occurred processing WSGI script '/home/rortega/smce/smce/wsgi.py'.
[Tue Sep 20 21:34:49.998229 2016] [:error] [pid 18220] [remote 10.105.40.106:172] Traceback (most recent call last):
[Tue Sep 20 21:34:49.998255 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project/project/wsgi.py", line 33, in <module>
[Tue Sep 20 21:34:49.998314 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     application = get_wsgi_application()
[Tue Sep 20 21:34:49.998326 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Tue Sep 20 21:34:49.998362 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     django.setup()
[Tue Sep 20 21:34:49.998372 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
[Tue Sep 20 21:34:49.998406 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     apps.populate(settings.INSTALLED_APPS)
[Tue Sep 20 21:34:49.998417 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
[Tue Sep 20 21:34:49.998516 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     app_config = AppConfig.create(entry)
[Tue Sep 20 21:34:49.998527 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
[Tue Sep 20 21:34:49.998590 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     module = import_module(entry)
[Tue Sep 20 21:34:49.998601 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
[Tue Sep 20 21:34:49.998637 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     __import__(name)
[Tue Sep 20 21:34:49.998647 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/stdimage/__init__.py", line 5, in <module>
[Tue Sep 20 21:34:49.998676 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     from .models import StdImageField  # NOQA
[Tue Sep 20 21:34:49.998686 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/stdimage/models.py", line 14, in <module>
[Tue Sep 20 21:34:49.998751 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     from PIL import Image, ImageOps
[Tue Sep 20 21:34:49.998761 2016] [:error] [pid 18220] [remote 10.105.40.106:172]   File "/home/user/project_env/lib/python3.5/site-packages/PIL/Image.py", line 67, in <module>
[Tue Sep 20 21:34:49.999163 2016] [:error] [pid 18220] [remote 10.105.40.106:172]     from PIL import _imaging as core
[Tue Sep 20 21:34:49.999185 2016] [:error] [pid 18220] [remote 10.105.40.106:172] ImportError: cannot import name _imaging
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Rafael Ortega
  • 434
  • 5
  • 15
  • Did you configure the web server to run python from your virtualenv instead of the system default python? – John Gordon Sep 20 '16 at 17:07
  • @JohnGordon, the only configuration i made to use my virtualenv python is the one i have in /etc/httpd/conf.d/django.conf... am i missing something else? – Rafael Ortega Sep 20 '16 at 17:17
  • for the second error, have a look at this: http://stackoverflow.com/questions/25340698/importerror-cannot-import-name-imaging – Alex Sep 20 '16 at 22:30
  • Have you successed?@lzuzvo – FavorMylikes Feb 10 '17 at 16:24
  • 1
    @FavorMylikes, yes i did. [This solution](https://github.com/GrahamDumpleton/mod_wsgi/issues/101) helped me. The trick is when you build Python and mod_wsgi. – Rafael Ortega Feb 13 '17 at 16:39

2 Answers2

2

You need to rebuild mod_wsgi from source for Python 3.

Download mod_wsgi. Unpack it.

tar xvfz mod_wsgi-X.Y.tar.gz

Configure for Python 3.5:

./configure --with-apxs=/usr/local/apache/bin/apxs \
  --with-python=/path/to/python3.5

Make and install:

make
make install

But it's recommended to use Nginx/Uwsgi or Nginx/Gunicorn. It's easy to configure for both python2 and python3.

Zulfugar Ismayilzadeh
  • 2,643
  • 3
  • 16
  • 26
  • Thanx @Zulfugar, il try rebuilding mod_wsgi and ill post here the results... i wanted to use Nginx, but i had more problems configuring it and i have a limit time for this project (;_; ) – Rafael Ortega Sep 20 '16 at 22:06
-1

That manual doesn't seem right, since it doesnt tell how to use the virtualenv on the server. Even though the last line of the apache config points to a wsgi config, it does not explain what should be in there.

try something like this:

#/home/user/project/project/wsgi.py

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/myprojectenv/local/lib/python3.5/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/home/django_projects/MyProject')
sys.path.append('/home/django_projects/MyProject/myproject')

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

# Activate your virtual env
activate_env=os.path.expanduser("~/.virtualenvs/myprojectenv/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Alex
  • 5,759
  • 1
  • 32
  • 47
  • Thanx for the explanation. But now i have an error in the wsgi.py:... execfile(activate_env, dict(__file__=activate_env)) calls an error on the activate file:... Syntax error on line 4 : deactivate() { – Rafael Ortega Sep 20 '16 at 17:37
  • Nevermind, i found the solution [here](http://stackoverflow.com/questions/24739643/syntaxerror-with-virtualenv-mod-wsgi-in-django) – Rafael Ortega Sep 20 '16 at 17:43
  • ok. I see it needed the activate_this.py file. fixed it anyway. – Alex Sep 20 '16 at 17:47
  • And it keeps using python2.7 ill put the traceback of the httpd error_log in edit #1 – Rafael Ortega Sep 20 '16 at 17:54
  • restart apache / uwsi ? – Alex Sep 20 '16 at 18:31
  • Yep, I restarted httpd service – Rafael Ortega Sep 20 '16 at 18:37
  • @Ixer, just letting you know that the problem is solved (but now i have a new error). See Edit #2 – Rafael Ortega Sep 20 '16 at 21:38
  • 1
    You will continue to have errors. Your mod_wsgi is compiled for Python 2.7. You cannot simply point it at a virtual environment using Python 3.5. That will not work. You must uninstall mod_wsgi using Python 2.7 and reinstall mod_wsgi from package or source code, compiled for Python 3.5. – Graham Dumpleton Sep 20 '16 at 23:20