I'm trying to set up my Django project in production by using a virtual environment, using the documentation:
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#using-a-virtualenv
So, in my configuration I have:
WSGIPythonPath /srv/zboss/zboss:/srv/zboss/venv/lib/python3.4/site-packages
I restart Apache and I get the following error:
Internal Server Error: /prot/
InvalidTemplateLibrary at /
Invalid template library specified. ImportError raised when trying to load 'core.templatetags.wiki_formatter': No module named parse
Request Method: GET
Request URL: http://babylon/prot/
Django Version: 1.10.1
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path: ['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/srv/zboss/zboss']
However the error is something because that library is not used anymore in Python3. I see the python path and no trace of my virtual environment directory.
Your mod_wsgi is compiled for Python 2.7. You cannot point it at a virtual environment for Python 3.4. You must uninstall mod_wsgi and install a mod_wsgi version built for Python 3.4, the same version as your virtual environment.
Related
I'm trying to switch a django site from running in python 3.6 to python 3.7 on Ubuntu 16.04. To do so I created a new environment in the same path as the old and have re-installed all of the dependencies.
The Apache server, however, fails with the following error:
Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'
Which error, after a bit of research, seems to be due to the WSGI configuration, as per:
You cannot for example force mod_wsgi to use a Python virtual
environment created using Python 3.5 when mod_wsgi was originally
compiled for Python 2.7. This is because the Python library for the
Python installation it was originally compiled against is linked
directly into the mod_wsgi module. In other words, Python is embedded
within mod_wsgi. When mod_wsgi is used it does not run the command
line python program to run the interpreter and thus why you can’t
force it to use a different Python installation.
from: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
thanks to: Django Apache wsgi changes python version
The OP in the above ended up just wiping the setup clean and starting from scratch. This seems a bit extreme to me, as it seems like I should just be able to re-install/re-configure mod-wsgi to run with python3.7
In this line of thinking, I uninstalled and reinstalled both, libapache2-mod-wsgi-py3 and mod_wsgi. The former, however, just reconfigures to 3.6 automatically, as seen below:
root#server1:~# dpkg -S mod_wsgi
libapache2-mod-wsgi-py3: /usr/lib/apache2/modules/mod_wsgi.so
libapache2-mod-wsgi-py3: /usr/lib/apache2/modules/mod_wsgi.so-3.6
and the mod_wsgi installation fails with:
RuntimeError: The 'apxs' command appears not to be installed or is not executable.
How can I re-build wsgi with python 3.7?
I am running a django app on Windows server 2008 R2(64 bit). The same app works fine on my other system which has windows 7 64 bit. I am actually trying to move the app to the other machine. I am using Python 2.7 and django 1.4. The manage.py runserver works fine and is able to start the server.
0 errors found
Django version 1.4, using settings 'checkmeout.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[11/Aug/2016 05:22:09] "GET /admin/ HTTP/1.1" 500 115256
While trying to access the admin page or any other url from http://127.0.0.1/8000,I am getting the following error.
ImportError at /admin/
No module named apps
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.4
Exception Type: ImportError
Exception Value:
No module named apps
Exception Location: C:\Python27\lib\site- packages\django_extensions\admin\__init__.py in <module>, line 10
Python Executable: C:\Python27\python.exe
Python Version: 2.7.5
Python Path:
['C:\\HardwareReservation\\checkmeout2',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:/SoftwareServices/checkmeout']
The output of pip freeze (on the system where it does not work) is put below in case that helps.
amqp==1.4.9
anyjson==0.3.3
billiard==3.3.0.23
celery==3.1.23
Django==1.4
django-apps==0.1.0
django-celery==3.1.17
django-extensions==1.7.1
djangorestframework==3.2.0
kombu==3.0.35
MySQL-python==1.2.4
pytz==2016.6.1
six==1.10.0
South==1.0.2
web.py==0.37
The pip freeze on the system where it works is below :
amqp==1.4.9
anyjson==0.3.3
billiard==3.3.0.23
celery==3.1.23
Django==1.4
django-celery==3.1.17
django-extensions==1.6.7
kombu==3.0.35
MySQL-python==1.2.5
pysphere==0.1.7
python-ldap==2.4.25
pytz==2016.4
six==1.10.0
South==1.0.2
virtualenv==15.0.2
I ran into a similar problem. Try installing the older version of django-extensions that worked:
pip install django-extensions=="1.6.7"
I'm following this for django manage.py module
http://docs.ansible.com/django_manage_module.html
for e.g. one of my tasks looks like -
- name: Django migrate
django_manage: command=migrate
app_path={{app_path}}
settings={{django_settings}}
tags:
- django
this works perfectly fine with python2(default in ubuntu) but when I try with python3-django project it throws error
failed: [123.456.200.000] => (item=school) => {"cmd": "python manage.py makemigrations --noinput school --settings=myproj.settings.production", "failed": true, "item": "school", "path": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games", "state": "absent", "syspath": ["/home/ubuntu/.ansible/tmp/ansible-tmp-1432039779.41-30449122707918", "/usr/lib/python2.7", "/usr/lib/python2.7/plat-x86_64-linux-gnu", "/usr/lib/python2.7/lib-tk", "/usr/lib/python2.7/lib-old", "/usr/lib/python2.7/lib-dynload", "/usr/local/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages"]}
msg:
:stderr: Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
from this error it seems Ansible bydefault uses Python2. can we change this to python3 or anyother workaround?
PS: pip freeze ensure that django 1.8 has installed (for python3 using pip3)
Suggestions:
when I run ubuntu#ubuntu:/srv/myproj$ python3 manage.py migrate it works fine. so I'm thinking of passing command directly
something like
- name: Django migrate
command: python3 manage.py migrate
tags:
- django
but how do I pass the project path or manage.py file's path, there is only an option to pass settings, something like --settings=myproject.settings.main.
can we do by passing direct command?
From Ansible website http://docs.ansible.com/intro_installation.html
Python 3 is a slightly different language than Python 2 and most Python programs (including Ansible) are not switching over yet. However, some Linux distributions (Gentoo, Arch) may not have a Python 2.X interpreter installed by default. On those systems, you should install one, and set the ‘ansible_python_interpreter’ variable in inventory (see Inventory) to point at your 2.X Python. Distributions like Red Hat Enterprise Linux, CentOS, Fedora, and Ubuntu all have a 2.X interpreter installed by default and this does not apply to those distributions. This is also true of nearly all Unix systems. If you need to bootstrap these remote systems by installing Python 2.X, using the ‘raw’ module will be able to do it remotely.
If you edit the shebang in the Django manage.py file to be #!/usr/bin/env python3 then you can ensure that python 3 will always be used to run your Django app.
Tried successfully with Ansible 2.3.0 and Django 1.10.5. YMMV
Ansible is using python to run the django command: https://github.com/ansible/ansible-modules-core/blob/devel/web_infrastructure/django_manage.py#L237
Your only solution is thus to override the executable that will be run, for instance by changing your PATH:
- file: src=/usr/bin/python3 dest=/home/user/.local/bin/python state=link
- name: Django migrate
django_manage: command=migrate
app_path={{app_path}}
settings={{django_settings}}
environment:
- PATH: "/home/user/.local/bin/:/bin:/usr/bin:/usr/local/bin"
Hi I am getting the error below when going to the website url on ubuntu server 14.10 running apache 2 with mod_wsgi and python on django.
My django application uses python 3.4 but it seems to be defaulting to python 2.7, I am unable to import image from PIL and AES from pycrypto.
ImportError at / cannot import name _imaging Request
Method: GET Request URL: Django Version: 1.7.3
Exception Type: ImportError Exception Value: cannot import
name _imaging Exception
Location: /usr/local/lib/python3.4/dist-packages/PIL/Image.py in
, line 63 Python Executable: /usr/bin/python Python
Version: 2.7.6 Python Path: ['/var/www/blabla',
'/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages', '/var/www/blabla',
'/usr/local/lib/python3.4/dist-packages']
I believe that mod_wsgi is compiled against a specific version of python, so you need a py3.4 version of mod_wsgi. You may be able to get one from your os's package repository or you can build one without too much drama. From memory you'll need gcc and python-dev packages (python3-dev?) to build.
OK, quick google, for ubuntu 14.10: sudo apt-get install libapache2-mod-wsgi-py3 should install a py3 version of mod_wsgi (will probably want to remove the existing py2 version).
Adding a shebang line won't do any good as the python interpreter is already loaded before the wsgi.py script is read.
From what I see here your application is using py2 interpreter with py3 compiled modules, which is no-go.
One simple possible solution that comes me in mind is to add or change first line of manage.py to #!/usr/bin/python3. This will tell script to be interpreted with py3.
Next on guess list would be misconfiguration in *.wsgi file or apache config, whichever you are using.
Thanks guys,
I actually fixed the issue myself this morning by running the make install of mod_wsgi with .configure pointing to python3.4.
I think you were right Adam.
I am using a Virtualenv on my Django install and have loaded in Pycrypto using
pip install pycrypto
after sourcing my environment.
When I try to import Crypto into my model, I get an import error, however if load the shell using
manage.py shell
it does load in, with out an issue.
I am pretty sure that django is running from the virtualenv as is it running 1.3.1 which more current than what is installed on the path and I have installed South via pip which is working.
I have also tried to install Pycrypto using
setup.py install
Any thoughts on how I fix/debug this?
Thanks
Edit:
I am relatively new to Django and Python so it could be something simple
Edit 2:
Hey Issac,
Is there any way that you can find all the modules getting loaded in?
I don't understand why it wouldn't work in browser but would work in the interactive shell? I am not saying that it can't, i just don't know if/how that could happen.
The import error as requested:
ImportError at /
No module named Crypto
Request Method: GET
Request URL: http://*/
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:
No module named Crypto
Exception Location: /home/*************/project/app/models.py in <module>, line 1
Python Executable: /home/*************/env/bin/python
Python Version: 2.6.6
Python Path:
['/usr/local/dh/passenger/lib/phusion_passenger/wsgi',
'/home/*************/env/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg',
'/home/*************/env/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg',
'/home/*************/env/src/django-debug-toolbar',
'/home/*************/env/lib/python2.6',
'/home/*************/env/lib/python2.6/plat-linux2',
'/home/*************/env/lib/python2.6/lib-tk',
'/home/*************/env/lib/python2.6/lib-old',
'/home/*************/env/lib/python2.6/lib-dynload',
'/usr/lib/python2.6',
'/usr/lib64/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-tk',
'/home/*************/env/lib/python2.6/site-packages',
'/home/*************',
'/home/*************/project']
Server time: Tue, 3 Jan 2012 20:08:19 +1100
Just before the line which causes the import error insert this code:
import sys
assert False, sys.path
Compare both results.
In the shell (where you can import the module). You can do this:
import Crypto
assert False, Crypto.__file__
Is the directory of this result in sys.path of your webserver?