django simple captcha no such file /usr/share/dict/words - django

I am deploying my Django project on a virtual machine with Ubuntu Trusty.
To do this I am using Gunicorn (with Supervisor) and Nginx.
Here is the link to the tutorial I followed:
http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
When I start Nginx and Gunicorn, the website is accessible, but when I try to reach a page that is using an app like django-simple-captcha, I get a "Server Error (500)".
As I am using virtualenv, all these apps (rosetta, easymode, simple-captcha...) installed with pip are stored in myvirtualenv/myproject/lib/python2.7/site-packages.
After some research, I modified my wsgi.py file to add the site-packages directory into the path, like this:
import os, sys, site
# Tell wsgi to add the Python site-packages to its path
site.addsitedir(os.path('/webapps/project/lib/python2.7/site-packages'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
#activate_this = os.path('/webapps/project/bin/activate_this.py')
#execfile(activate_this, dict(__file__=activate_this))
#sys.path.append(os.path('/webapps/project/'))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
... but it still doesn't work, knowing that if I uncomment one of the three lines above, I get an error message from gunicorn when restarting it:
project: ERROR (abnormal termination)
Do you think the problem here is that I don't activate the virtualenv?
Should I find another way to activate it?
How can I have some more information about why gunicorn won't start with the execfile or sys.path.append line?
Do you have any suggestions on what to do?
Please let me know if it lacks information, like the gunicorn_start script (even if it is the same as what we can found on many tutorials) :)
UPDATE:
I didn't pay enough attention, the gunicorn_start script is activating the virtualenv, so I don't need to do it in the wsgi.py file.
But the problem is still here: I print sys.path in the wsgi.py file, and when I use the python manage.py runserver my.server.address:8000 command, I can see that all the needed directories are in:
['/webapps/project/project', '/webapps/project/lib/python2.7', '/webapps/project/lib/python2.7/plat-x86_64-linux-gnu', '/webapps/project/lib/python2.7/lib-tk', '/webapps/project/lib/python2.7/lib-old', '/webapps/project/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/webapps/project/local/lib/python2.7/site-packages', '/webapps/project/lib/python2.7/site-packages']
I can't figure out why I get Server Error (500) when I try to load a page that make use of django-simple-captcha app located in /webapps/project/lib/python2.7/site-packages...

After some tests, I saw that it was only django-simple-captcha at the origin of the problem. And don't ask me why I didn't think to do that earlier, but I turned DEBUG on, and I got an IO error:
no such file /usr/share/dict/words
Indeed I was using
CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.word_challenge'
in my settings.py...
Two solutions:
Use another generator for django-simple-captcha: http://django-simple-captcha.readthedocs.org/en/latest/advanced.html#generators-and-modifiers
Install needed dictionnaries:
sudo apt-get install wamerican
sudo apt-get install wfrench
...

Related

Running Gunicorn and Django

I don't know how to use gunicorn with django. Could you give me some help?
This is how I run the server with django. It is https
python3 manage.py runsslserver xx.8x.x3.x4:443 --certificate /etc/letsencrypt/live/callservicesvps.online/fullchain.pem --key /etc/letsencrypt/live/callserv.com/privkey.pem
And in the gunicorn documentation it is mentioned that it must be executed as follows:
gunicorn myproject.wsgi
And here I have 2 questions. What is myproject.wsgi? Where Can I find it? Because if I look in the directory where the django project is, the only thing I find with wsgi is a file called wsgi.py
Running the server as follows gives me an error
gunicorn /home/proyectdirectory/wsgi.py
It also gives me an error if I put:
gunicorn /home/proyectdirectory/wsgi:Some_directory_where_the_proyec_is
What is myproject.wsgi?
myproject.wsgi IS the wsgi.py file you have located inside your project.
[End of answer]
But a further explanation will clear up why this is..
I imagine most people will look atmyproject.wsgi and see a file with an extension file type .wsgi but this is just because of the way importing of modules is written in python.
I want to clarify what a module and a package before continuing an explanation.
What is a Module
From the jargon heavy python docs
A module is a file containing Python definitions and statements.
Put simply, a module in python is just a python file containing any sort of functions, variables, or classes etc.
What is a Package
A package is just a collection of modules. The most simplest example, a special directory containing python files. In order to tell python that a directory is a package it must have a file named __init__.py inside of it. You will find a few of these inside different directories inside your django project. This is why they are there.
Now, I can say what I want to say which is the structure of the module namespace in python.
package.subpackage.module
If you look inside your wsgi.py file you'll see a good example of importing from django's own wsgi module.
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')
application = get_wsgi_application()
More specifically
from django.core.wsgi import get_wsgi_application
If you look inside the django package you can see how this works.
django (package) core (subpackage) wsgi (module)
Remember, the command gunicorn myproject.wsgi should be run inside the base directory of your django project. I always remember this as the directory containing the manage.py file. That is how gunicorn can find the wsgi.py file using the module namespace in this way. gunicorn is written in python afterall.
gunicorn /home/proyectdirectory/wsgi.py will error because python module imports don't contain / and even if you tried gunicorn home.proyectdirectory.wsgi home and proyectdirectory are not python packages.
Now hopefully this makes sense:
"If gunicorn myproject.wsgi is referring to the wsgi.py file why not just put gunicorn myproject.wsgi.py?"
You can't put a .py extension because this will refer to a module inside the wsgi subpackage with a filename py.py!

Unable to run setup.py behind proxy

I'm new to python (and linux) and I'm trying to run the setup.py, however it's not working properly because there's a corporative proxy blocking te request to pypi.
I check this link to properly use the setup.py and also check this and this solutions in stackoverflow but I can't make them work (or I'm wrong in the way I'm applying them).
I'm using:
virtualenv
virtualenvwrapper
python 2.7
Ubuntu 14
I already add the http_proxy and https_proxy in .profile and .bashrc.
When I use pip install --proxy the.proxy:port some_module it's working properly (also I know the env variables do something is because before that I can't even get to stackoverflow.com, so I'm assuming they work just fine).
What I have already tried is:
Trying to use --proxy on python
Look for something similar to --proxy in python
Trying to add the proxy configuration described in one of the solutions mentioned earlier in my setup.py (which is add to the description of this problem)
Tried and successfully downloaded a couple of modules with pip --proxy (this is my current not-so-good-solution)
Messing with the python configuration files in the virtualenv in hope of find some proxy config
My setup.py file looks like this:
from setuptools import setup, find_packages
import requests
with open('development.txt') as file:
install_requires = file.readlines()
with open('development_test.txt') as file_test:
test_requires = file_test.readlines()
setup(
name="my_project",
version="1.0.0-SNAPSHOT",
packages=find_packages(),
install_requires=install_requires,
test_suite="nose.collector",
tests_require=test_requires,
)
proxies = {
"http": "http://proxy.myproxy.com:3333",
"https": "http://proxy.myproxy.com:3333",
}
# not sure what goes here... tried a few things but nothing happend
requests.get("https://pypi.python.org", proxies=proxies)
I'll try any suggestion, any help appreciated.
Thanks
After a deep search about how python works and not being able to find the problem I start looking to how the bash commands work.
It turn out you have to export the http_proxy variables with sudo -E.
A rocky mistake.

How do I set up Jupyter/IPython Notebook for Django?

I have been using the method described in this post for setting up IPython Notebook to play nicely with Django. The gist of the method is to create an IPython extension which sets the DJANGO_SETTINGS_MODULE and runs django.setup() when IPython starts.
The code for the extension is:
def load_ipython_extension(ipython):
# The `ipython` argument is the currently active `InteractiveShell`
# instance, which can be used in any way. This allows you to register
# new magics or aliases, for example.
try:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django
django.setup()
except ImportError:
pass
With a recent upgrade to Jupyter Notebook this setup is now broken for me. I am able to run Django code in the Jupyter notebook by adding a similar bit of code to the first cell of the notebook. However, I was not able to figure out how to get Jupyter to run the extension automatically so I would not have to do this again for each and every notebook I am creating.
What should I do to get Django and Jupyter to play nicely?
UPDATE:
For #DarkLight - I am using Django 1.8.5 with Jupyter 1.0.0. The code I run in the notebook is:
import os, sys
sys.path.insert(0, '/path/to/project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settingsfile")
import django
django.setup()
Install django-extensions from https://github.com/django-extensions/django-extensions/blob/master/docs/index.rst
pip install django-extensions
Change your settings file to include 'django-extensions'
INSTALLED_APPS += ['django_extensions']
Run your Django server like this:
python manage.py shell_plus --notebook
alter to suit, and run this in your first cell
import os, sys
PWD = os.getenv('PWD')
os.chdir(PWD)
sys.path.insert(0, os.getenv('PWD'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "local_settings.py")
import django
django.setup()
Now you should be able to import your django models etc. eg:
from app.models import Foobar
Foobar.objects.all()
Just for completeness (but it's 2018, so maybe things changed since this question was posted): you can actually install a Jupyter Python kernel in your Django environment that will then connect (run under) a different Jupyter server/environment (one where you've installed widgets, extensions, changed the theme, etc.). django_extensions right now still does only part of the required work :-)
This assumes you have a Jupyter virtual environment that's separate from Django's one and whose kernels/extensions are installed with --user. All the Jupyter extensions (and their dependencies) are installed in this venv instead of the Django's one/ones (you'll still need pandas, matplotlib, etc. in the Django environment if you need to use them together with Django code).
In your Django virtual environment (that can run a different version of Python, including a version 2 interpreter) install the ipython kernel:
pip install -U ipykernel
ipython kernel install --user --name='environment_name' --display-name='Your Project'
This will create a kernel configuration directory with the specified -–name in your user’s Jupyter kernel directory (on Linux it's ~/.jupyter/ while on OSX it’s ~/Library/Jupyter/) containing its kernel.json file and images/icons (by default the default Jupyter icon for the kernel we’re installing are used). This kernel will run inside the virtual environment what was active at creation, thus using the exact same version of python and all the installed modules used by our Django project.
Running ./manage.py shell_plus --notebook does something very similar, but in addition to requiring everything (including the Jupyter server and all the extensions) installed in the current venv, it’s also unable to run notebooks in directories different from the project’s root (the one containing ./manage.py). In addition it’ll run the kernel using the first executable called python it finds on the path, not the virtual environment’s one, making it misbehave when not started from the command line inside an active Django virtual environment.
To fix these problems so that we're able to create a Notebook running inside any Django project we have so configured and to be able to run notebooks stored anywhere on the filesystem, we need to:
make sure the first ‘argv’ parameter contains the full path to the python interpreter contained in the virtual environment
add (if not already present) an ‘env’ section that will contain shell environment variables, then use these to tell Python where to find our project and which Django settings it should use. We do this by adding something like the following:
"env": {
"DJANGO_SETTINGS_MODULE": "my_project.settings",
"PYTHONPATH": "$PYTHONPATH:/home/projectuser/projectfolder/my_project"
}
optional: change ‘display_name’ to be human friendly and replace the icons.
editing this environment kernel.json file you'll see something similar:
{
"display_name": "My Project",
"language": "python",
"env": {
"DJANGO_SETTINGS_MODULE": "my_project.settings",
"PYTHONPATH": "$PYTHONPATH:/home/projectuser/projectfolder/my_project"
},
"argv": [
"/home/projectuser/.pyenv/versions/2.7.15/envs/my_project_venv/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}",
"--ext",
"django_extensions.management.notebook_extension"
]
}
Notable lines:
"DJANGO_SETTINGS_MODULE": "my_project.settings": your settings, usually as seen inside your project's manage.py
"PYTHONPATH": "$PYTHONPATH:/home/projectuser/projectfolder/my_project": PYTHONPATH is extended to include your project's main directory (the one containing manage.py) so that settings can be found even if the kernel isn't run in that exact directory (here django_extensions will use a generic python, thus running the wrong virtual environment unless the whole Jupyter server is launched from inside it: adding this to the kernel.json created by django_extensions will enable it to run notebooks anywhere in the Django project directory)
"/home/projectuser/.pyenv/versions/2.7.15/envs/my_project_venv/bin/python": first argument (argv list) of the kernel execution, should be the full path to your project's virtual environment's python interpreter (this is another thing django_extensions gets wrong: fixing this will allow any notebook server to run that specific Django environment's kernel with all its installed modules)
"django_extensions.management.notebook_extension": this is the extension that will load the 'shell_plus' functionality in the notebook (optional but useful :-) )
Here's what just worked for me
install Django Extensions (I used 1.9.6) as per other answers
install jupyterpip install jupyter
some stuff I did to setup jupyter inside my Docker container -- see below if this applies to you †
from your base Django directory, create a directory for notebooks, e.g. mkdir notebooks
Go to that directory cd notebooks
start django extensions shell_plus from inside that directory: ../manage.py shell_plus --notebook
The notebook server should now be running, and may launch a new browser. If it doesn't launch a browser window, follow the instructions to paste a link or a token.
from the browser, open a new "Django Shell Plus" notebook, as per John Mee's answer's screenshot
AND, importantly, what didn't work was changing directories from inside the notebook environment. If I tried to work with any notebook that was not in the directory that manage.py shell_plus --notebook was run in, then the kernal was not configured correctly. For me, having the notebook be configured for just a single directory at a time was good enough. If you need a more robust solution, you should be able set PYTHONPATH prior to starting jupyter. For example add export PYTHONPATH="$PYTHONPATH:/path/to/django/project" to a virtualenv activate script. But I haven't tried this.
† Docker Setup (optional)
add a port mapping for your container for port 8888
For example, in your docker compose file;
ports:
- "8890:8888"
Configure your project settings file to use ip 0.0.0.0
This is what I did:
NOTEBOOK_ARGUMENTS = [
'--ip', '0.0.0.0',
'--allow-root',
'--no-browser',
]
Note: I am using Python 3.7 and Django 2.1, it works for Django 2.2. I don't have to run anything in my first cell, and this works like charm as long as you don't mind having the notebooks in the root of your Django project.
It is assumed that you have a virtual environment for your project, and it is activated. I use pipenv to create virtual environments and track dependencies of my python projects, but it is up to you what tool you use.
It is also assumed that you have created a Django project and your current working directory is the root of this project.
Steps
Install jupyter
Using pip
pip install jupyter
Using pipenv
pipenv install jupyter
Install django-extentions
Using pip
pip install django-extensions
Using pipenv
pipenv install django-extensions
Set up django-extensions by adding it to the INSTALLED_APPS setting of your Django project settings.py file.:
INSTALLED_APPS = (
...
'django_extensions',
)
Run the shell_plus management command that is part of django-extensions. Use the option --notebook to start a notebook:
python manage.py shell_plus --notebook
Jupyter Notebooks will open automatically in your browser.
Start a new Django Shell-Plus notebook
That's it!
Again, you don't have to run anything in the first cell, and you can corroborate by running dir() to see the names in the current local scope.
Edit:
If you want to put your notebooks in a directory called notebooks at the root directory, you can do the following:
$ mkdir notebooks && cd notebooks
$ python ../manage.py shell_plus --notebook
Thanks to Mark Chackerian whose answer provided the idea to make run the notebooks in a directory other than the project's root.
These are the modules that are imported automatically thanks to shell_plus:
# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When, Exists, OuterRef, Subquery
from django.utils import timezone
from django.urls import reverse
Actually turns out you (might not) need to do all that crap. Just install django-extensions and run jupyter!
(myprojectvenv)$ cd myproject
(myprojectvenv)$ pip install jupyter
(myprojectvenv)$ pip install django-extensions
(myprojectvenv)$ jupyter notebook
In the browser, start a new "Django Shell-Plus":
And you should be good to go. eg:
from myproject.models import Foobar
Foobar.objects.all()
While the accepted answer from RobM works, it was less clear than it could be and has a few unnecessary steps. Simply put, to run notebooks through Django from a notebook environment outside of the project directory:
Install:
pip install django-extensions
Add 'django-extensions' to your INSTALLED_APPS list in settings.py
INSTALLED_APPS += ['django_extensions']
Run a notebook from within Django, then close it:
python manage.py shell_plus --notebook
This will create your kernel, which we will now edit to point to an absolute path of Python rather than a relative path.
On OSX, the kernel file is at: ~/Library/Jupyter/kernels/django_extensions/kernel.json
On Linux: ~/.jupyter/kernels/django_extensions/kernel.json
We only need to make two changes:
The first is to edit the first value in the "argv" list from "python" to the full address of the python version in your Django virtual environment. E.g.: "/Users/$USERNAME/Documents/PROJECT_FOLDER/venv/bin/python"
Secondly, to the "env" dictionary, add "DJANGO_SETTINGS_MODULE": "mysite.settings", where mysite is the folder that contains your Django settings.
Optionally, change the value of "display_name".
Now when you run a notebook from any directory, choosing the "Django Shell-Plus" kernel will allow your notebooks to interact with Django. Any packages such as pandas will need to be installed in the Django venv.
The following does work for me using win10, Python 3.5, Django 1.10:
Install Python with the Anaconda distribution so Jupyter will be installed as well
Install Django and install django-extensions:
pip install Django
pip install django-extensions
Start a new Django project. You have to do that in that part of your tree of directories which can be accessed by Jupyter later.
django-admin startproject _myDjangoProject_
Start Jypter
navigate Jupyter to the directory myDjangoProject and enter the first/top myDjangoProject-directory
Start within the first/top myDjangoProject-directory a new Jupyter noteboke: new --> Django Shell-Plus
enter and run the following piece of code :
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myDjangoProject.settings")
import django
django.setup()
Note that this piece of code is the same as in manage.py, and note that "myDjangoProject.settings" points to myDjangoProject/settings.py
Now you can start with examples, e.g.:
from django.template import Template, Context
template = Template('The name of this project is {{ projectName }}')
context = Context({'projectName': 'MyJypyterDjangoSite'})
template.render(context)
Run this command.
PYTHONPATH=/path/to/project/root DJANGO_SETTINGS_MODULE=settings python manage.py shell_plus --notebook
I will add some information to the very complete answer of RobM, for the benefit of the very rare developers that use buildout along with djangorecipe djangorecipe as I do... I refer to jupyter lab as I use that but I think all info can be applied to old jupyter notebooks.
When using buildout you end up with a 'bin/django' handler you'll use instead of 'manage.py'. That's the script that defines the whole path. I added one more part in my buildout.cfg:
[ipython]
recipe = zc.recipe.egg
eggs = ${buildout:eggs}
extra-paths = ${buildout:directory}/apps
initialization = import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'web.settings'
so that another script named ipython will be created in ./bin directory. I point kernelspec to that interpreter. Moreover I use kernel argument rather than "-m", "ipykernel_launcher" so that the kernel definition I use is:
{
"argv": [
"/misc/src/hg/siti/trepalchi/bin/ipython",
"kernel",
"-f",
"{connection_file}",
"--ext",
"django_extensions.management.notebook_extension"
],
"display_name": "Trepalchi",
"language": "python"
}
Due to how the ipython script is created by buildout there's no need to add environmental variables in my case.
As Rob already mentioned, jupiterlab is only installed in one environment where I start it with the command:
jupyter lab
not in the environment of Django project whare I only install ipykernel (that has already a bunch of 20 dependencies).
Since I tend to have quite a lot of projects I find it usefull to have a single point where I start jupyter lab with many links to the projects so that I can reach them easily. Thanks to the extension provided by django_extension I don't need any extra cell to initialize the notebook.
Any single kernel added in this way can be found with the command:
jupyter kernelspec list
And clearly listed in the launcher of jupyter lab

Django management task won't work on CentOS in crontab or outside project directory

On my local machine (Mac OSX 10.6) I wrote a django custom admin command which works great. I can use it both within and outside my project directory just fine. For some reason on my CentOS 5.6 server, it won't work from outside the project directory. This is really annoying since using this custom admin command in a cron job requires it to run from the home directory.
in short:
When I run "python ./manage.py scrape" or "python manage.py scrape", everything is fine.
When I run "python /home/[username]/webapps/myproject/manage.py scrape" or "python myproject/manage.py scrape", I get the following error:
unknown command: 'scrape'
Type 'manage.py help' for usage.
On CentOS, when I run manage.py help inside the project directory, scrape shows up as a command; but if I run it outside the project directory, scrape does not appear as a valid command. On OS-X scrape appears as a valid command regardless of where I run manage.py help from.
Any idea how I can fix this?
I know CentOS ships with Python 2.4, so is your code running on 2.4 or are you using a contained environment, this is usually fixed by adding your PYTHONPATH correctly
import sys
print sys.path
verify such for starters
This should get you up and running: http://djangosnippets.org/snippets/374/

Works using Django development server, but throws import error with Apache using mod_wsgi

I have a Django project that works fine with the development server that comes with it.
No errors are produced at all when I use "django manage.py runserver" and the app works fine, but when I try to use it with mod_wsgi and Apache the browser displays "Internal Server Error" with a 500 error code and it generates an import error in the Apache error log.
Here's the error in the log:
ImportError: No module named registration
I'm using the Django registration module which is located in a path like this:
/opt/raj/photos/registration
I know that the registration app is in the path because I can fire up a Python shell, import sys, and get a list of paths using sys.path.
Here are some of the paths output from Python shell:
sys.path
['', '/opt/raj/pyamf', '/opt/raj', '/opt/raj/pictures', '/opt/raj/pictures /registration', '/usr/lib/python2.6',....]
Any thoughts would be appreciated.
Is it in the pythonpath for the webserver? All those '/opt' paths are typically not in the standard python path, so something is adding those for you I would guess. Are you sure it also gets added for the webserver process, or is PYTHONPATH set in some shell configfile somewhere for your user only?
There is a PythonPath directive when using mod_python, is there something similar for mod_wsgi?
This is almost certainly a case of the path not being the same for the webserver as it is for you, so I would focus my search in those areas.