How to install downloaded django project in virtualenv - python

I am trying to follow the instructions from this Django/Ajax tutorial: https://realpython.com/blog/python/django-and-ajax-form-submissions/. They say:
Download the compressed pre-ajax Django Project from the repo https://github.com/realpython/django-form-fun/tree/master/part1
Activate a virtualenv
Install the requirements
Sync the database
Fire up the server
Here is what I am doing:
Create new virtualenv using virtualenv -p /usr/bin/python3 ajaxenv inside home/ajaxtutorial/ folder
Download the repo and unpack its django-form-fun/part1/pre-ajax/talk_project/ folder in the home/ajaxtutorial folder.
Put the requirements.txt provided with the downloaded repo in ajaxenv/bin
Run pip install -r requirements.txt inside ajaxenv. At this point I can see that Django gets installed.
At this point the tutorial says to "sync the database". The only way I know how to do this is to use python manage.py migrate, but this throws an error. I guess this method requires starting the project or an app first, which is not a case here, since both the app and the project are downloaded, not created by me.
I don't know how to proceed from here. python manage.py migrate throws the following error:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/conf/__init__.py", line 50, in _setup
self._configure_logging()
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/conf/__init__.py", line 72, in _configure_logging
from django.utils.log import DEFAULT_LOGGING
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/utils/log.py", line 7, in <module>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/views/debug.py", line 12, in <module>
from django.template import Template, Context, TemplateDoesNotExist
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/template/__init__.py", line 53, in <module>
from django.template.base import (ALLOWED_VARIABLE_CHARS, BLOCK_TAG_END,
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/template/base.py", line 19, in <module>
from django.utils.html import escape
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/utils/html.py", line 14, in <module>
from .html_parser import HTMLParser, HTMLParseError
File "/home/bart/ajaxtutorial/ajaxenv/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <module>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'

Related

Can't run a specific django project in a virtualenv

I need to edit but first run this project: https://pagure.io/fedora-commops/fedora-happiness-packets and it gives me errors even though I followed the instructions.
First I forked the repo, then cloned it to my computer and I followed the instructions listed in the setup section of the README.md file. When I run the command ./manage.py collectstatic I get this error:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/virtualenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/virtualenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/virtualenv/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/virtualenv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/virtualenv/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/happinesspackets/messaging/models.py", line 15, in <module>
from happinesspackets.tasks import send_html_mail
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/happinesspackets/tasks.py", line 3, in <module>
from happinesspackets._celery import app
File "/Users/alexmarginean/Desktop/fed/fedora-happiness-packets/happinesspackets/_celery.py", line 2, in <module>
from celery import Celery
ImportError: No module named celery
I tried importing celery but that method didn't work and I don't want to mess with the project.
Looking at issue 49, it looks like celery is a dependency but it isn not listed in the requirements. Until that is fixed, you'll have to install it manually.
pip install celery
Have you done pip install -r requirements in the virtualenv ?

Django - running manage.py produces fatal error for version.py: Not a git repository

I have copied my Django 1.4.3 app from one server to another. It is running using mod_wsgi daemon on the other server.
mod_python is installed, as verified by looking at php.info(). Python version is 2.7, and Apache version is 2.4.
I get the following error whenever I run manage.py runserver 0.0.0.0:8000:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 252, in fetch_command
app_name = get_commands()[subcommand]
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 101, in get_commands
apps = settings.INSTALLED_APPS
File "/usr/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/usr/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/lib/python2.7/site-packages/django/conf/__init__.py", line 128, in __init__
logging_config_module = importlib.import_module(logging_config_path)
File "/usr/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/python2.7/site-packages/django/utils/log.py", line 6, in <module>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/usr/lib/python2.7/site-packages/django/views/debug.py", line 9, in <module>
from django.http import (HttpResponse, HttpResponseServerError,
File "/usr/lib/python2.7/site-packages/django/http/__init__.py", line 19, in <module>
from mod_python.util import parse_qsl
File "/usr/lib64/python2.7/site-packages/mod_python/__init__.py", line 25, in <module>
from . import version
File "/usr/lib64/python2.7/site-packages/mod_python/version.py", line 3
version = "fatal: Not a git repository (or any of the parent directories): .git
The version.py file has the following contents:
# THIS FILE IS AUTO-GENERATED BY setup.py
version = "fatal: Not a git repository (or any of the parent directories): .git
3.5.0-"
# Some build-time constants:
HTTPD = "/usr/sbin/httpd"
HTTPD_VERSION = "2.4.6"
APR_VERSION = "1.4.8"
LIBEXECDIR = "/usr/lib64/httpd/modules"
SYSCONFDIR = ""
TEST_MOD_PYTHON_SO = "/usr/local/src/mod_python-3.5.0/src/mod_python.so" #NB: This is for test.py only
TESTHOME = "/usr/local/src/mod_python-3.5.0/test"
PYTHON_BIN = "/bin/python"
The app requires python 2.7, and I don't know how to reset mod_python to something that's compatible with version 2.7.

Cloning a heroku hosted django project to your local machine

I'm new to python, Django, heroku and even PostgreSQL but am working on a Django project with a team of 3 devs (they set it up), so I'm trying to set it up on my local machine so I can start developing.
Firstly I cloned the repository to my local machine, there was a requirement.txt file, so I ran pip install -r ./requirements.txt to install all the python packages the project uses. Theres a runtime.txt file which specifies python 3.5 so I need to upgrade that. I see that gunicorn is one of the packages in requiments.txt, so I'm guessing I need to use that as the web server.
Heres the packages in requirements.txt that look important to me:
Django==1.9.6
gunicorn==19.5.0
psycopg2==2.6.1
boto3==1.3.1
boto==2.40.0
whitenoise==3.2
django-extensions==1.6.7
The site also has a .buildpacks file, which seems to be a heroku specific thing:
https://github.com/a2ikm/heroku-buildpack-libjpeg62
https://github.com/heroku/heroku-buildpack-python
https://github.com/rafaelp/heroku-buildpack-wkhtmltopdf
Theres a Procfile that contains this:
web: gunicorn myproject.wsgi --log-file -
worker: celery worker --app=genome.tasks.app -l info --beat
I'm wondering what the best way to do this is. Is there a way to automatically generate a virtual environment that has all the requirements and software specified in the project? I'm already using a virtual dev environment with created with Vagrant so I could provision it with all the dependencies, but I get the feeling theres a better way to do this.
UPDATE:
Heres what what happens when I try to run the server with python manage.py runserver:
$ python manage.py runserver
Current environment: P
Current environment: P
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7fd0d08c71b8>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/sd2/selfdecode/decodify/urls.py", line 30, in <module>
url(r'', include("genome.urls")),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/sd2/selfdecode/genome/urls.py", line 7, in <module>
from . import views
File "/var/www/sd2/selfdecode/genome/views.py", line 20, in <module>
from decodify.aggregators import ArrayAgg, JsonAgg, ArrayaAggDelimited
File "/var/www/sd2/selfdecode/decodify/aggregators.py", line 29
def __init__(self, *expressions, distinct=False, **extra):
^
SyntaxError: invalid syntax
I'm guessing the parse error is due to using python 2.7 in a project that uses python 3.5. I installed python 3.5 with sudo apt-get install python3-all. Trying the same thing with python3:
$ python3 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'
> python3
The second error means the packages I installed with pip install requirements.txt, they were installed for python 2.7, not 3.5. So this is obviously where virtual environments can come in. I tried this sudo -H virtualenv -p /usr/bin/python3 py3env, both with and without sudo, and both times I got errors :
[06:08 PM]-[vagrant#machine1]-[/var/www/sd2/selfdecode]-[git master]
$ sudo -H virtualenv -p /usr/bin/python3 py3env
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /var/www/sd2/selfdecode/py3env/bin/python3
Also creating executable in /var/www/sd2/selfdecode/py3env/bin/python
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 2328, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 713, in main
symlink=options.symlink)
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 925, in create_environment
site_packages=site_packages, clear=clear, symlink=symlink))
File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 1370, in install_python
os.symlink(py_executable_base, full_pth)
OSError: [Errno 71] Protocol error: 'python3' -> '/var/www/sd2/myproj/py3env/bin/python'
I came across this thread:
protocol error setting up virtualenvironment through vagrant on ubuntu
Where someone was having this problem using virtualenv inside a vagrant VM, and a possible solution is setting up the venv outside of the shared vagrant folder.

Django, cannot make messages

I'm writing this post because after hours of research around, no real solution was found about my issue.
I've a Django project that used to run on another computer. Now, I've a new one, I've reinstalled Django and VirtualEnvWrapper.
I've cloned my project from my git and I can work and deploy without any issues.
Running ./manage.py works when I'm on the correct virtualenv (via workon 'my_project_name').
My problem is that I can't generate translations any more.
Here is the trace-back:
(MyProject)--- python/MyProject ‹master*➔ M› »django-admin.py makemessages --all
Traceback (most recent call last):
File "/usr/local/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 376, in execute
sys.stdout.write(self.main_help_text() + '\n')
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 240, in main_help_text
for name, app in six.iteritems(get_commands()):
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'MyProject.settings.local' (Is it on sys.path? Is there an import error in the settings file?): No module named MyProject.settings.local
When I'm checking my sys.path, here is the content:
/Users/jbcollet/.virtualenvs/MyProject/lib/python27.zip
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/plat-darwin
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/plat-mac
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/plat-mac/lib-scriptpackages
/Users/jbcollet/.virtualenvs/MyProject/Extras/lib/python
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/lib-tk
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/lib-old
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/jbcollet/.virtualenvs/MyProject/lib/python2.7/site-packages
Can someone help me with this?
Thanks a lot

Django/Celery can't find importlib

So I just updated django to 1.2.3 and now when I try to run 'python manage.py shell' to work in the django environment, I'm getting the following error.
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/opt/local/lib/python2.5/site-packages/django/core/management/commands/shell.py", line 18, in handle_noargs
loaded_models = get_models()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/loading.py", line 167, in get_models
self._populate()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/loading.py", line 64, in _populate
self.load_app(app_name)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/loading.py", line 78, in load_app
models = import_module('.models', app_name)
File "/opt/local/lib/python2.5/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/opt/local/lib/python2.5/site-packages/django_celery-2.0.3-py2.5.egg/djcelery/models.py", line 7, in <module>
from celery import conf
File "/opt/local/lib/python2.5/site-packages/celery-2.0.3-py2.5.egg/celery/conf.py", line 6, in <module>
from celery import routes
File "/opt/local/lib/python2.5/site-packages/celery-2.0.3-py2.5.egg/celery/routes.py", line 2, in <module>
from celery.utils import instantiate, firstmethod, mpromise
File "/opt/local/lib/python2.5/site-packages/celery-2.0.3-py2.5.egg/celery/utils/__init__.py", line 9, in <module>
import importlib
ImportError: No module named importlib
Any ideas. I can't seem to find what's going on here and for all I can tell I'm running the same versions on my web server and I don't have the same error showing up.
importlib which was added in Python 2.7/3.1, I believe. You can download a port for pyton 2.5 here:
importlib 1.0.1 - Backport of importlib.import_module() from Python 2.7
Also check the setup.cfg for celery near the bottom and make sure all the other requirements are met (toward the bottom of the script).
importlib was added to Python in version 3.1, and then backported to Python 2.7. Third party backports are available on PyPI.
Also note that 'backported to 2.7' doesn't imply that all versions after 2.7 will have importlib. Python 3.0, I believe, does not have importlib.

Categories

Resources