Django - First project - runserver is an invalid syntax - python

I decided to try developing on python with django, while python is new to me right now.
[I'm using Windows 7 64bit]
Then, i installed python 2.7, added C:\Python27\;C:\Python27\Scripts; to PATH variable.
Next, i installed Eclipse Classic (last version) and installed PyDev, and Django.
I created my first project on Eclipse as a Django project but couldn't run it.
I'm trying (Right click on my project)-> Run as -> PyDev: Django, and i have the following console log:
Validating models...
Traceback (most recent call last): File "C:\Users\Matan\workspace\FirstSample\manage.py", line 10, in <module>
execute_from_command_line(sys.argv) File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 443, in execute_from_command_line
utility.execute() File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__) File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options) File "C:\Python27\Lib\site-packages\django\core\management\commands\runserver.py", line 69, in handle
self.run(*args, **options) File "C:\Python27\Lib\site-packages\django\core\management\commands\runserver.py", line 80, in run
and the list goes on...
So i tried to run the project with the python console, using:
import Django
manage.py runserver
and i have the following syntax error:
File "<stdin>", line 1
manage.py runserver
^ SyntaxError: invalid syntax
What is the problem with running my project ? How can i solve this?
Thanks.
Edit:
After trying to run the project with the following command:
python manage.py runserver on the project directory, i receive the following traceback log: Validating models...
Unhandled exception in thread started by <bound method Command.inner_run of <dja ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x000000 0002688748>> Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\core\management\commands\runserver. py", line 91, in inner_run
self.validate(display_num_errors=True) File "C:\Python27\lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app) File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin e 23, in get_validation_errors
from django.db import models, connection File "C:\Python27\lib\site-packages\django\db\__init__.py", line 40, in <modul e>
backend = load_backend(connection.settings_dict['ENGINE']) File "C:\Python27\lib\site-packages\django\db\__init__.py", line 34, in
__geta ttr__
return getattr(connections[DEFAULT_DB_ALIAS], item) File "C:\Python27\lib\site-packages\django\db\utils.py", line 92, in
__getitem
__
backend = load_backend(db['ENGINE']) File "C:\Python27\lib\site-packages\django\db\utils.py", line 24, in load_back end
return import_module('.base', backend_name) File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im port_module
__import__(name) File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 16 , in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo dule named MySQLdb

Download the pre-compiled python-mysql package for Windows, install it, then run the command again.
While you are there, you might as well also install setuptools if you haven't already.

Error loading MySQLdb module: No mo dule named MySQLdb
You have configured your settings to you MySQL but you have not installed MySQL-python:
pip install MySQL-python

You must be in the project directory to run the python manage.py runserver command. You also need to (1) set the PYTHONPATH environment variable to include your project directory's parent and (2) set the DJANGO_SETTINGS_MODULE environment variable to yourproject.settings
If you've done it correctly, you should be able to go to an unrelated directory (i.e. not your project directory) and do the following import in a python shell:
c:\temp> python
...
>>> from django.conf import settings
the settings module that is imported should then be the one in your project directory...

Related

Django runserver command failed

I've been learning Python for a few months and a couple days ago i decided to learn one of Web frameworks. I found some tutorials on Django and it was looking really cool until i tried to use "runserver" command. As you can see i used the basic line "python -m django runserver manage.py" and got a lot of calls. I've just installed Django and i wasn't changing anything in it's scripts. I only started by creating a new project that worked perfectly with "python -m django startproject djangoLearn". Do you have any thoughts on how can i fix this problem? This problem prevents me from further learning beacuse i can't see my own project :((
Thanks in advance. Hope y'all have a great day.
Calls that i get while trying to run server:
D:\Projects\djangoLearn>python -m django runserver manage.py
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\__main__.py", line 9, in <module>
management.execute_from_command_line()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 61, in execute
super().execute(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 68, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 82, in __getattr__
self._setup(name)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I hope your virtualenv is activated ??
use this to create project django-admin startproject projectname . take note of the period it works without the period though.
use python manage.py startapp appname to create an app
don't forget to register the app in INSTALLED_APP
i think the code you want to use is python manage.py runserver
but if you use python3 use python3 manage.py runserver just use whatever you use to start your python before manage.py runserver
you should first activate your virtual environment. with a command like this in linux:
source "path-to-activate-file"
and command like this in windows:
\pathto\env\Scripts\activate

First steps Django errors

I need to ask because I'm starting in Django and it was impossible for me to continue with the tutorial that I'm following. I've just completed a Python course, I know the basics of python. The case is that I've installed Django in my mac and it's OK because when I do in the IDLE the
import django
print django.VERSION
It makes a good answer without errors in IDLE (is the software that I use to write python code). But when I execute in the Terminal the command:
python manage.py runserver
It raises this error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/Current/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 8, in <module>
from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 26, in <module>
from django.views import static
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/views/static.py", line 95, in <module>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 75, in gettext_noop
return _trans.gettext_noop(message)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 48, in __getattr__
if settings.USE_I18N:
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I also want to ask you some questions:
Can I use XAMPP instead of Django? How?
What is Django used for exactly?
I've installed PyDev in Eclipse. How can I set Django to execute code properly?
call command django-admin startproject xxx under the target directory. You can get three .py files: setting.py, urls.py and manage.py. And you can execute python manage.py runserver command
I think it's slightly outdated now, but I'd seriously recommend going through the Djangobook examples. It'll explain how to set up Django properly, and give a pretty good foundation in how to use it.

Django 1.4.1 error loading MySQLdb module when attempting 'python manage.py shell'

I am trying to set up MySQL, and can't seem to be able to enter the Django manage.py shell interpreter. I did install MySQL-python but that did not solve the issue.
Getting the output below:
rrdhcp-10-32-44-126:django pavelfage$ python manage.py shell
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 45, in handle_noargs
from django.db.models.loading import get_models
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
return import_module('.base', backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named _mysql
Any suggestions really appreciated.
You are probably missing the MySQLdb module. You basically just have to install it.
By the looks of it, you are using a Mac, so you might want to check out this answer for detailed installation instructions: How to install MySQLdb (Python data access library to MySQL) on Mac OS X?
On a totally alternate solution, as you mentionned Django development you might want to use sqlite3 instead of MySQL, which could be simpler to install.
You should be able to import MySQLdb from python shell.
Try importing MySQLdb from python shell not django
If you can not import MySQLdb from python shell;
It may caused because of 32-64 bit mismatch,
Try this link

Django-admin.py producing errors on my shared hosting environment

I tried to install django on my shared hosting environment, which has SSH access.
I downloaded the newest version of Django using
svn export http://code.djangoproject.com/svn/django/trunk/django django
I can import Django in python shell without any error throwing.
But then I tried to start a new project using django-admin.py and it started throwing me a bunch of error, in which I tried to google for the past few hours. I still can't find it
Below is the actual error:
k4660061#server42222:~$ django-admin.py startproject klikevent
/u/k4660061/home/local/lib/python/site-packages/django/core/management/templates.py:155: Warning: 'with' will become a reserved keyword in Python 2.6
Traceback (most recent call last):
File "/u/k4660061/home/local/lib/python/site-packages/django/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/u/k4660061/home/local/lib/python/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/u/k4660061/home/local/lib/python/site-packages/django/core/management/__init__.py", line 381, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/u/k4660061/home/local/lib/python/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/u/k4660061/home/local/lib/python/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/u/k4660061/home/local/lib/python/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/u/k4660061/home/local/lib/python/site-packages/django/core/management/commands/startproject.py", line 2, in <module>
from django.core.management.templates import TemplateCommand
File "/u/k4660061/home/local/lib/python/site-packages/django/core/management/templates.py", line 155
with open(old_path, 'r') as template_file:
^
SyntaxError: invalid syntax
k4660061#server42222:~$
Thanks
UPDATE
After a good night sleep, I got it working! See answer by Bernie. I have to see the stacktrace over and over, but I just need to add the import statement to multiple file
the file are
django/utils/archive.py
django/core/management/templates.py
and any file that throw the warning with_statement
Thanks #Bernie
If the syntax error is related to the with statement, it could be that your prod server has an older version of Python than your dev server.
If your prod server is running version 2.5 or higher you can do this:
from __future__ import with_statement
Please note: any from __future__ imports must be the very first import.
Reference: http://docs.python.org/reference/simple_stmts.html#future

python manage.py runserver fails

I am trying to learn django by following along with this tutorial. I am using django version 1.1.1
I run
django-admin.py startproject mysite
and it creates the files it should. Then I try to start the server by running
python manage.py runserver
but here is where I get the following error.
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 213, in execute
translation.activate('en-us')
File "/Library/Python/2.6/site-packages/django/utils/translation/__init__.py", line 73, in activate
return real_activate(language)
File "/Library/Python/2.6/site-packages/django/utils/translation/__init__.py", line 43, in delayed_loader
return g['real_%s' % caller](*args, **kwargs)
File "/Library/Python/2.6/site-packages/django/utils/translation/trans_real.py", line 205, in activate
_active[currentThread()] = translation(language)
File "/Library/Python/2.6/site-packages/django/utils/translation/trans_real.py", line 194, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/Library/Python/2.6/site-packages/django/utils/translation/trans_real.py", line 172, in _fetch
for localepath in settings.LOCALE_PATHS:
File "/Library/Python/2.6/site-packages/django/utils/functional.py", line 273, in __getattr__
return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'LOCALE_PATHS'
Now, I can add a LOCALE_PATH atribute and set to an empty tuple to my settings.py file but then it just complains about another setting and so on. What am I missing here?
Something is broken in your django installation. maybe you have a (very) old version somewhere in the path?
LOCALE_PATHS was given a default value in the global settings file a long time ago.
Can't really explain that. Try removing the project directory and starting again.
Are you definitely running the manage.py from within the directory with the settings file?

Categories

Resources