I have installed django 1.6 using pip on Windows 7. When I try to run django-admin.py, I get this error
C:\django-admin.py startproject test007
Traceback (most recent call last):
File "C:\Python27\Scripts\django-admin.py", line 2 in ?
from django.core import management
File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 55
except ImportError as e:
^
SyntaxError: invalid syntax
in the \management__init__.py, the imports are
import collections
import os
import sys
from optparse import OptionParser, NO_DEFAULT
import imp
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError, handle_default_options
from django.core.management.color import color_style
from django.utils.importlib import import_module
from django.utils import six
# For backwards compatibility: get_version() used to be in this module.
from django import get_version
Here's the relevant block from the same file
parts = app_name.split('.')
parts.append('management')
parts.reverse()
part = parts.pop()
path = None
# When using manage.py, the project module is added to the path,
# loaded, then removed from the path. This means that
# testproject.testapp.models can be loaded in future, even if
# testproject isn't in the path. When looking for the management
# module, we need look for the case where the project name is part
# of the app_name but the project directory itself isn't on the path.
try:
f, path, descr = imp.find_module(part, path)
except ImportError as e:
if os.path.basename(os.getcwd()) != part:
raise e
else:
if f:
f.close()
while parts:
part = parts.pop()
f, path, descr = imp.find_module(part, [path] if path else None)
if f:
f.close()
return path
in which the same line 55 from the traceback is in the try/except block towards the bottom. I've uninstalled and reinstalled but, to no avail.
It works when I give the/full/path/to/django-admin.py but it shouldn't be required.
Related
stucture of the app
flask_app
__init__.py
models.py
routes.py
run.py
__init__.py
from flask import Flask
from flask_bcrypt import Bcrypt
import os
from flask_app.models import db
app = Flask(__name__)
db.init_app(app)
bcrypt = Bcrypt(app)
from flask_app import routes
models.py
from flask_sqlalchemy import SQLAlchemy
from flask_app import app, bcrypt
db = SQLAlchemy()
run.py
from flask_app import app
if __name__ == '__main__':
app.run(debug=True)
This happens first
Traceback (most recent call last):
File
"c:\users\cristovao\documents\mqs_development\flaskexperiment\env\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\run.py", line 4, in <module>
from flask_app import app # importing from __init__.py within flask_app package
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\__init__.py", line 6, in <module>
from flask_app.models import db
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\models.py", line 2, in <module>
from flask_app import app, bcrypt
ImportError: cannot import name 'app' from 'flask_app' (C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\__init__.py)
and then in models.py after this error I write from flask__app.__init__ import app bcrypt and I got another error
Traceback (most recent call last):
File "c:\users\cristovao\documents\mqs_development\flaskexperiment\env\lib\site-packages\flask\cli.py", line 240, in locate_app
__import__(module_name)
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\run.py", line 4, in <module>
from flask_app import app # importing from __init__.py within flask_app package
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\__init__.py", line 6, in <module>
from flask_app.models import db
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\models.py", line 2, in <module>
from flask_app.__init__ import app, bcrypt
File "C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\__init__.py", line 6, in <module>
from flask_app.models import db
ImportError: cannot import name 'db' from 'flask_app.models' (C:\Users\cristovao\Documents\MQS_Development\FlaskExperiment\flask_app\models.py)
when I run my app using flask run it gives me those import errors cannot import app and bcrypt from flask_app to models.py and can't import db from models.py to __init__py. I dont understand why since run.py is being separated from my package (flask__app).
When I use your code, I get an error because of a circular import.
ImportError: cannot import name 'app' from partially initialized module 'flask_app' (most likely due to a circular import) (/home/jugmac00/Tests/stackoverflow/flask_app/__init__.py)
I can get rid of that when I remove this line from models.py
from flask_app import app, bcrypt
The easiest way to get rid of circular imports is to use the app factory pattern.
https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/
This application factory pattern took me a while to understand, but it is really worthwile to try to understand and use it.
There is a mindblowing good video on that, from this year's Flask-Conference:
https://www.youtube.com/watch?v=xNo-eOfZH5Q
If this sounds to hard, then my advice would be... just put everything in one file - that is not too bad, unless the app grows really big.
I've searched SO and Google and can't find an answer to my problem.
I've launched my virtualenv and ran this command in the terminal:
python bin/process_messages.py
and this error occurs:
Stacktrace:
Traceback (most recent call last):
File "bin/process_messages.py", line 6, in <module>
from xyz.models import get_sku
File "/Users/myname/.environments/xyz_env/lib/python3.6/site-packages/xyz/models.py", line 19, in <module>
class Suppliers(models.Model):
File "/Users/myname/.environments/xyz_env/lib/python3.6/site-packages/django/db/models/base.py", line 110, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/myname/.environments/xyz_env/lib/python3.6/site-packages/django/apps/registry.py", line 247, in get_containing_app_config
self.check_apps_ready()
File "/Users/myname/.environments/xyz_env/lib/python3.6/site-packages/django/apps/registry.py", line 125, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I thought it wasn't running django.setup so I added that to the script. Here is my code:
#!/usr/bin/env python
import os
import django
import boto3
from xyz.settings import SQS_QUEUE_NAME
from xyz.models import get_sku
__author__ = 'me'
def check_django_environment(default_settings):
# Environment setup for Django project files:
os.sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if not os.environ.get('DJANGO_SETTINGS_MODULE'):
# Don't override settings if it is specified.
os.environ['DJANGO_SETTINGS_MODULE'] = default_settings
from django.conf import settings
return getattr(settings, 'DEBUG', None)
check_django_environment('xyz.settings')
django.setup()
# Get the service resource
sqs = boto3.resource('sqs')
# Get the queue
queue = sqs.get_queue_by_name(QueueName=SQS_QUEUE_NAME)
for message in queue.receive_messages():
print(message)
if message.message_attributes is not None:
print(message.message_attributes)
#sku = message.message_attributes
db_sku = get_sku(sku)
print(db_sku)
break
My Installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'xyz',
]
By importing get_sku, you are importing your models before django.setup() has run. You need to move this import down so it happens after django.setup().
In a stand-alone script that uses Django, I generally have two groups of imports. The first contains the bare minimum to get Django setup, the second contains all the other imports, including models etc.:
#!/usr/bin/env python
import os
import django
__author__ = 'me'
def check_django_environment(default_settings):
# Environment setup for Django project files:
os.sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if not os.environ.get('DJANGO_SETTINGS_MODULE'):
# Don't override settings if it is specified.
os.environ['DJANGO_SETTINGS_MODULE'] = default_settings
from django.conf import settings
return getattr(settings, 'DEBUG', None)
check_django_environment('xyz.settings')
django.setup()
import boto3
from xyz.settings import SQS_QUEUE_NAME
from xyz.models import get_sku
In addition to knbk's answer, you can also just use manage.py.
Here's an example script, let's say script.py:
from xyz.models import get_sku
if __name__ == '__main__':
print(get_sku)
And you run it like:
./manage.py shell < script.py
Maybe not what you're looking for, but worth knowing, nonetheless.
I have a package installed in my site-packages dir. Folder structure looks like this
MyPkg\
__init__.py
LogUtils\
__init__.py
logwrapper.py
Shortcuts\
__init__.py <-----this references LogUtils
somefile.py
When I do help ('modules') I see MyPkg listed. But I get the following error in IDLE:
>>> import MyPkg
>>> from MyPkg import LogUtils
>>> from MyPkg import Shortcuts
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from MyPkg import Shortcuts
File "C:\Python27\lib\site-packages\MyPkg\Shortcuts\__init__.py", line 1, in <module>
from GoToUrl import go_to_url
File "C:\Python27\lib\site-packages\MyPkg\Shortcuts\GoToUrl.py", line 1, in <module>
from LogUtils import logger, log
ImportError: No module named LogUtils
Why would LogUtils import fine standing alone, but throw an error when being imported via an init file??
Seems to me you are lacking some backslashes
MyPkg\
__init__.py
LogUtils\
__init__.py, \
logwrapper.py
Shortcuts\
__init__.py, \
somefile.py
As you see yourself, you are not importing the same module:
>>> from MyPkg import LogUtils
vs.
from LogUtils import logger, log
The first imports a package called MyPkg.LogUtils, the second imports a package called LogUtils. Whether they exist or not depends on your python paths, but in general, if the first one works, change the second one to
from MyPkg.LogUtils import logger, log
I am trying to make logparser.py in django project which parses the data coming from different servers.
And on running the command on terminal :
$ python logparser.py
This error is coming :
Traceback (most recent call last):
File "logparser.py", line 13, in <module>
SMTP_CONF = settings.SMTP_CONF
File "/home/arya/.virtualenv/Devel/.virtualenvs/hu/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/arya/.virtualenv/Devel/.virtualenvs/hu/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/home/arya/.virtualenv/Devel/.virtualenvs/hu/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'hma.settings' (Is it on sys.path?): No module named hma.settings
my logparser.py contains:
import re
import os
import fnmatch
import gzip
import bz2
from collections import defaultdict
from django.core.mail import send_mail
from django.core.mail.backends import smtp
import smtplib
from email.mime.text import MIMEText
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hma.settings")
from django.conf import settings
SMTP_CONF = settings.SMTP_CONF
def send_email(self,fromaddress,toaddresses,content,subject):
smtp_server = SMTP_CONF["SERVER"]
smtp_username = SMTP_CONF["USERNAME"]
smtp_password = SMTP_CONF["PASSWORD"]
smtp_port = SMTP_CONF["PORT"]
msg = MIMEText(content, 'html', _charset='utf-8')
msg['Subject'] ='Alert message for bad and internal server error'
msg['From'] = fromaddress
msg['To'] = toaddresses
server = smtplib.SMTP(smtp_server,smtp_port)
server.starttls()
server.login(smtp_username,smtp_password)
server.send_mail(fromaddress,toaddresses,msg.as_string())
server.quit()
return True
I know I am doing wrong something with command [python manage.py], but i need to run like this. Any solution for this exporting django settings to separate python file??
Well, This is the exact Usecase why Django provided an ability to create custom commands. You can use all the features of django, in your script, Its like your script will be running inside a Django Container. Here is the Documentation https://docs.djangoproject.com/en/dev/howto/custom-management-commands/.
In case you don't want to use custom management commands though there is also a simple way to run your code within Django's context. Simply put the following at the beginning of your python file, you want to run:
from django.conf import settings
from django.core import management
management.setup_environ(settings)
Trying to deploy a Django 1.4 project using mod_wsgi and virtualenv, i'm running into a 500. The Apache error_log reveals:
mod_wsgi (pid=30452): Exception occurred processing WSGI script '/path/to/project/site-packages/projectapp/wsgi.py'.
[...] Traceback (most recent call last):
[...] File "/path/to/project/env/myenv/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
[...] self.load_middleware()
[...] File "/path/to/project/env/myenv/lib/python2.6/site-packages/django/core/handlers/base.py", line 47, in load_middleware
[...] raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[...] ImproperlyConfigured: Error importing middleware projectapp.content.middleware: "cannot import name SomeModel"
From the error message i would expect that this is some kind of a path issue. However, when the offending middleware is removed from the Django settings, the site launches just fine, and there are other modules loaded from projectapp.content, SomeModel is also loaded in this case, otherwise the whole site wouldn't be able to run.
The import error raised doesn't come directly from the middleware, as it doesn't import the model. SomeModel is defined in a speparate app which is actually checked out into the src directory of the virtualenv. But the directory containing this app is also in the path.
The wsgi.py file i'm using:
import os
import sys
sys.stdout = sys.stderr
sys.path.insert(0, '/path/to/project/env/myenv/lib/python2.6/site-packages/')
# The module inside the following directory
# defines SomeModel from the error message
sys.path.insert(0, '/path/to/project/env/myenv/src/some-app/')
sys.path.insert(0, '/path/to/project/site-packages/')
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectapp.settings'
application = django.core.handlers.wsgi.WSGIHandler()
Printing sys.path after inserting the module paths shows everything in the expected order, /path/to/project/site-packages/ is listed first and /path/to/project/env/myenv/src/some-app/ (which defines SomeModel) second.
I've also tried a different variant, based on the example from the mod_wsgi docs:
import os
import sys
import site
ALLDIRS = [
'/path/to/project/site-packages/',
'/path/to/project/env/myenv/lib/python2.6/site-packages/',
'/path/to/project/env/myenv/src/some-app/',
]
# Remember original sys.path
prev_sys_path = list(sys.path)
sys.stdout = sys.stderr
# Add each new site-packages directory
for directory in ALLDIRS:
site.addsitedir(directory)
# Reorder sys.path so new directories are at the front
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
#activate_this = '/path/to/project/env/myenv/bin/activate_this.py'
#execfile(activate_this, dict(__file__=activate_this))
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectapp.settings'
application = django.core.handlers.wsgi.WSGIHandler()
The error logged by Apache is exactly the same.
I have to add that the middleware is loaded just fine when i'm running the development server on the same machine, so i have no idea what's going wrong. Is there a way to get a better traceback from mod_wsgi?
EDIT:
Fogot to mention that i'm using mod_wsgi in daemon mode. Here are the relevant parts from my vhost config:
<VirtualHost x.x.x.x:80>
WSGIDaemonProcess foo user=foo threads=10 umask=0002
WSGIProcessGroup foo
WSGIScriptAlias / /path/to/project/site-packages/projectapp/wsgi.py
</VirtualHost>
Okay after hours of debugging this turned out to be a race condition caused by the middleware. The middleware i'm using is similiar to the FlatpageFallbackMiddleware from Django contrib and actually the import of the view caused the problem.
from projectapp.content.views import generic_content_detail
class ContentFallbackMiddleware(object):
def process_response(self, request, response):
[...]
Moving the import statement inside the process_response method solved the problem for me:
class ContentFallbackMiddleware(object):
def process_response(self, request, response):
from projectapp.content.views import generic_content_detail
[...]