How: django_manage in ansible with Python3 - django

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"

Related

Deploy django as war - Jython

I'm trying to deploy a django app as war (to use with JBOSS server). I have seen the documentation and I made this:
jython manage.py builder --include-java-libs=/usr/share/java/jython/jython.jar
And I have this error:
Traceback (most recent call last):
File "manage.py", line 16, in <module>
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
What should I do?
Thanks.
You have not activated that environment where you installed Python and other dependent packages for application.
Activate the environment and then run:
jython manage.py builder --include-java-libs=/usr/share/java/jython/jython.jar
Then it will work for you.
Try this:
jython -m pip list
In the list check the package name django is there or not. Also, check for package django-jython.
To install this package to jython installation directory, run this command:
jython -m pip install django
jython -m pip install django-jython
To build war file, use this command:
jython manage.py buildwar --include-java-libs=/path/to/jython-standalone.jar

django framework runserver not working

Django server isn't working on the command: python manage.py runserver (on providing the port number as well)
I'm using windows 8, the path is set in environment variables. I've tried re-installing both python and django but it didn't help.
I share the similar problem. I solved it by just running Python 2.7x.
While working with the runiserver
python manage.py runserver --noreload
also worked for me - at least for a while. Maybe install an older version of Django?
I've not literally found a solution...
however it worked when I used python 2.7.13 (this creates a folder where you've scripts, easy_install etc., but I couldn't find a folder "Python" when I was using Python 3.6.1)

django wont work with python3.3.3

Hello I recently installed the django on my Mac OS X 10.9 using the pip install Django.
The installation was successful, now my python3.3.3 is installed in the following directory:
/usr/local/lib/python3.3/site-packages
Inside this directory there is a directory for django so I am assuming that the installation was success on python 3.
The problem arise when I am trying the following procedure:
django-admin.py startproject mysite
The above command creates with success the directory with the files
Then on this command:
python manage.py runserver
or
python3 manage.py runserver
The error is:
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django'
Moreover, I do find a solution but wont work. Was to change the first comment of the file django-admin.py to link the compiler to the correct version of python, which was fine, but the problem is the same.
Any help?
Check out this tutorial on python development on OSX: http://hackercodex.com/guide/python-development-environment-on-mac-osx/
Virtualenv allows you to create sandboxes where you can install different python versions and python libraries without interfering with other virtual environments or with your system python version/libraries.
Your specific problem though, seems to be that Django was not added to your system path, so python doesn't know where to find the django files. It can be risky to add new things to your system path because it could end up conflicting with existing libraries.
If you use virtualenv, each environment will have its own "system" path and the python executable in that environment will know where to find the django files.

Running Django in Virtualenv on EC2 -- ImportError: No module named django.core.management

I developed a django application locally, in a git repo. I launched an EC2 instance for the project and I set up a virtualenv with (what I believe to be) the correct packages/dependencies. I then proceeded to clone my repo into the virtualenv. Right now, I'm having difficulty as I'm receiving the following errors:
I attempted to use python manage.py runserver example.com/8080 to test. I was sure to activate the virtualenv using source bin/activate, just like I did in my local virtualenv. When I call ... runserver I get the following error:
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
Here is what sudo pip freeze produces:
Warning: cannot find svn location for distribute==0.6.24dev-r0
Cheetah==2.4.4
Django==1.5.2
Fabric==1.8.0
GnuPGInterface==0.3.2
Landscape-Client==12.05
M2Crypto==0.21.1
PAM==0.4.2
PyYAML==3.10
South==0.8.2
Twisted-Core==11.1.0
Twisted-Names==11.1.0
Twisted-Web==11.1.0
apt-xapian-index==0.44
argparse==1.2.1
boto==2.2.2
chardet==2.0.1
cloud-init==0.6.3
command-not-found==0.2.44
configobj==4.7.2
## FIXME: could not find svn URL in dependency_links for this package:
distribute==0.6.24dev-r0
django-s3-folder-storage==0.1
django-storages==1.1.8
django-tastypie==0.10.0
ecdsa==0.9
euca2ools==2.0.0
gunicorn==18.0
httplib2==0.7.2
keyring==0.9.2
language-selector==0.1
launchpadlib==1.9.12
lazr.restfulclient==0.12.0
lazr.uri==1.0.3
medusa==0.5.4
meld3==0.6.5
oauth==1.0.1
paramiko==1.12.0
psycopg2==2.5.1
pyOpenSSL==0.12
pycrypto==2.4.1
pycurl==7.19.0
pyserial==2.5
python-apt==0.8.3ubuntu7.1
python-dateutil==2.1
python-debian==0.1.21ubuntu1
simplejson==2.3.2
six==1.4.1
supervisor==3.0a8
ufw==0.31.1-1
unattended-upgrades==0.1
virtualenv==1.10.1
wadllib==1.3.0
wsgiref==0.1.2
zope.interface==3.6.1
...and this is my ./manage.py file:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Any thoughts on how I can fix this error? I tried to change #!/usr/bin/env python to #!/var/www/paletto-env/bin python, trying to direct it to my actual python path, but it did not lend to any apparent change, so I changed it back.
Thanks for the help.
Looks like you're environment can't find Django, even though it's clearly in your pip freeze.
Try opening a Python (not Django) shell from your virtual environment and entering:
import django
django.VERSION
If you run into the same error, there's probably an issue with your Django install. Your virtual environment probably can't find it. You could try modifying the path settings, or just reinstall Django.
If you can successfully import Django, or if you tried reinstalling and it doesn't work, you may have a permission problem. Ensure that the user responsible for running the server has access to wherever your python libraries are stored from the virtualenv.
I had pretty much the exact same problem as you and this is how I solved it (Disclaimer: I'm not sure if this is the absolute correct way but it worked for me and everything seems correct).
Short Answer:
Try pip install (package) instead of sudo pip install (package)
Long Answer:
I gave the Django complete install doc (See here) a quick read through and came across a bit that basically said you dont need super user privileges when using pip in the virtualenv. I just assumed that either would be fine but I now realize that's probably not the case.
I suspect that because I entered sudo pip install django it installed it somewhere above my local bin in my virtualenv for the project. I suspect this because when I enter python manage.py runserver I get an error; however, when I enter sudo python manage.py runserver everything functions properly.
Also, typing sudo pip freeze reveals my larger library whereas pip freeze reveals my local library for my virtualenv.
I didn't want to type sudo (do stuff) before everything and I wanted a nice, clean and proper virtualenv so I just reinstalled django but to my local virtual env with pip install django and now django is in the proper virtualenv library, pip freeze returns the proper contents, and python manage.py runserver functions properly!
django-admin.py startproject by default creates the shebang in manage.py with #!/usr/bin/env python.
If this is not the path to your python executable, or if you use python3, just edit the manage.py to reflect this.
Reinstall all python packages angain after you have activated your virtualenv.
Maybe the Django has lost some plugin with pip, so we can install Django with Tarball:
Go https://www.djangoproject.com/download/1.6.5/tarball/
Download Django-*.tar.gz
install it.
$ tar zxvf Django-1.6.5.tar.gz
$ cd Django-1.6.5/
$ python setup.py install
more ... (https://stackoverflow.com/a/24323774/686105

Django ImportError: No module named django

So, I have python 2.7.3 installed and I used the Django website https://docs.djangoproject.com/en/dev/topics/install/ to install Django on my Mac OS x Mountain Lion. I used pip and virtual env, I currently am able to use Django from my current terminal but, when I open a new terminal and try say:
$ cd Django
$ cd djangowork
$ cd firstproj
$ python manage.py runserver
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
This is the error I get.
I have looked online for a solution to my problem. It seems to be a common problem, dealing with the PATH. But, I cannot figure out how to fix it. Any help would be much appreciated thanks!
This is an example of what my working terminal looks like: (but if I exit out I lose everything)
(my_new_env)$ cd Django
(my_new_env)$ cd djangowork
(my_new_env)$ cd firstproj
(my_new_env)$ ls
firstproj manage.py testdb
(my_new_env)$ python manage.py runserver
Validating models...
0 errors found
October 31, 2012 - 18:51:22
Django version 1.6.dev20121029143154, using settings 'firstproj.settings'
Development server is running at http://.../
Quit the server with CONTROL-C.
Notice how in the second case you have (my_new_env) before the prompt, but not in the first? That's because only in the second case have you run virtual env properly. Make sure you run source my_new_env/bin/activate before you run any python commands at the prompt. You should always have (my_new_env) before the prompt in order to make sure your environment is set up properly. You have to do this in every terminal window that you open.