I have a Django project which seemed to work pretty well with settings.py, which also imported a local_settings.py without problem.
I've now added the following lines at the end of the settings file :
try:
from extras import *
except ImportError, e:
print "import extras failed :: " + `e`
extras.py is a file of extra configuration information sitting in the same directory as settings.py and local_settings.py, however, I'm now getting :
import extras failed :: ImportError('Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.',)
This seems to be due to me trying to
from django.contrib.auth.models import User,UserManager
from django.db import models
in that extras.py file.
Anyone have any ideas?
cheers
Typically, having a line like
from django.db import models
in settings.py will lead to a circular dependency. This causes an import error, which gets reported slightly differently in different versions of Django. For example, if I add that line to a working Django setup and invoke "manage.py shell", I get:
Error: Can't find the file
'settings.py' in the directory
containing './manage.py'. It appears
you've customized things. You'll have
to run django-admin.py, passing it
your settings module. (If the file
settings.py does indeed exist, it's
causing an ImportError somehow.)
If I remove that line, everything's fine again.
The reason is that Django's model loading machinery (located in the django.db.models package) imports settings.py, reads its INSTALLED_APPS to see what apps should be installed, and then loads those apps. (You can confirm this by adding a print statement to __init__.py for one of your installed apps.)
If you try to import django.db stuff in settings.py, that will lead to a circular import dependency and an ImportError-related error message.
One workaround is to move the functionality which requires the problematic imports (and the imports themselves) to an app.
So how is DJANGO_SETTINGS_MODULE set in your environment? It could be either that, or the result of the implicit import of settings which your other nested imports are causing while settings is being imported, a "circular dependency" that can have several nasty effects (though I don't believe it would have the specific one you're observing, so I lean towards the first hypothesis).
Related
I'm trying to run a .py file and in the file I have this import
from config.wsgi import *
import os
from django.template.loader import get_template
from weasyprint import HTML, CSS
from config import settings
The whole project works, if I set runserver, the project starts without any problem, but this file does not work. The structure of the project is as follows
NombreDelProyecto
--app
---config
----__init__.py
----asgi.py
----settings.py
----wsgy.py
----db.py
---core
----general
----login
----user
----archivodetest.py
the case as I say the project works, but in the views of the applications that I have been doing to put imports I get in red underlined but as I say it works for example:
from core.general.forms import ActividadForm
That comes out in red, if I put in front of the core, app.core as follows
from app.core.general.forms import ActividadForm
it does not show red but the project does not work and I get the following error
from app.core.general.forms import ActividadForm
ModuleNotFoundError: No module named 'app'
I understand that it is the routes or something I did wrong from the beginning, please could someone help me.
Thank you very much.
I tried adding the route, changing the app's route in settings, but to no avail.
You've named the file wsgy.py but it needs to be wsgi.py.
Rename your file in config and retry.
To your question, I think its because you're missing the __init__.py file in the general app.
If you haven't already go one, you'll likely need to have add the same again in your core app too.
You probably manually created all of these files and structures I suspect, and if that's the case, please take a look at the documentation regarding creating new apps inside a django project.
If you go a bit further up the page, it will also tell you how to create the initial django project structure with a command.
Thank you very much for the answer, I managed to solve it after a lot of testing.
There are two ways, open the project again from the app folder (I had it open in the ProjectName folder).
Or as a second option in pyCharm on the left where the project folders are, I went to the app folder (which is the root) and right clicked and in the menu, Mark Directorie as - Sources root. Then my problem is fixed.
I had all the arcvhiso init.py, and where I put the wrong name wsgi.py is that I wrote it wrong here but in the project it was right.
Thank you very much for the help.
I'm trying to add django-messages to my app but I'm getting this error when I add it to my installed apps
ImportError: cannot import name 'python_2_unicode_compatible' from 'django.utils.encoding' (C:\Users\Acer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\encoding.py)
I've looked up this error and apparently it's caused by this line in Django-Messages models.py
from django.utils.encoding import python_2_unicode_compatible
To fix it I should change it to
from django.utils.six import python_2_unicode_compatible
As this is a third party app, how should I change this line?
Should I copy the file structure into my own project (creating a django-messages folder among my apps then a models.py file inside it) and copy/paste the entire models.py into there and then change the line? This seems wrong, but I don't know how else to fix it.
Or does the fact that it's using an outdated import signify that the app isn't maintained and therefore shouldn't be used at all?
Thank you.
I can suggest two options. First, you can clone it on GitHub (i.e. download all the files) and then paste this into your project folder (along with your other apps). And then you can make any changes you wish. Of course it remains to be seen whether it will work then, as there might be other issues. It seems to be compatible with Django 1.11 - 2.2. But I would think that it's a better option than doing this all yourself. Unless you can find a different app that suits your needs.
Second, assuming you are using a virtual environment, you can navigate to where all your packages are installed. I'm not sure where this will be located for you, but it should be something like envs\<name of env>\Lib\site-packages. And there you can make any changes you wish - just search for the relevant file.
Hope this helps.
I am testing Google Cloud and first I wanted to develop something on my PC before I use in on google cloud.
I am using APACHE and configured it in that way, that when I am going to the page localhost/wsgi_app I see my page which physically is stored in folder /svc/http/webapp2. File wsgi_app.py which contains my app is stored in subfolder webapp2. All works fine. I provide this information just in case it might play any role.
The issue I have is with import from library.
I did it on Django and now try to move it to webapp2.
The first lines of my program look like this:
import webapp2
import MySQLdb
import json
I have file called test.py which contains some classess and funtions.
it is in the same folder as wsgi_app.py.
I want to include it, however this seems not to work:
import webapp2
import MySQLdb
import json
from test import *
my test.py contains definition of the class 'Quote', but when I call the page I see error
NameError: global name 'Quote' is not defined
When I put the inside of the file test.py in the file wsgi_app.py all works fine.
My goal is to separate the code into several files.
Any idea why :
from test import *
does not work ?
It worked on any other program I wrote, so why not here?
issue solved.
issue is not related to weapp2 but to WSGI and the path where python search for files.
Most simple solution is to add something like this ;
execfile("/srv/http/test.py")
it will import the file.
other than this here is quite good article
http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html
I'm having a weird problem with import views in Django project. I'm not sure whether it is a problem caused by PyCharm of Django. So PyCharm says that it can't import views.py file, but it works when I run the server.
Here is the picture:
Do you know where could be the problem?
EDIT:
According to Inlangers answer, I've tried to change import to from vlado_web.translations import views which did not helped, moreover, it raises
Exception Value:
No module named translations
When I have from translations import views there, it works correctly but PyCharm says that it can't be resolved.
PyCharm doesn't know where your source files are. Try this:
Right click on folder vlado_web (the folder that contains manage.py) within PyCharm. Go to Mark Directory As and choose Sources Root.
This will let PyCharm know that the vlado_web directory is the root folder for your source code, and will allow you to perform absolute imports from there, e.g.
from vlado_web.translations import views
Try from vlado_web.translations import views
I want to use a different settings file in django -- specifically settings_prod -- yet whenever I try to do a syncdb with --settings=settings_prod, it complains:
python2.6 manage.py syncdb --settings=settings_prod
Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
I've also tried setting the environment variable DJANGO_SETTINGS_MODULE=settings_prod to no end.
Edit: I have also set the environment variable in my wsgi file, also to no end:
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings_prod'
application = WSGIHandler()
Suggestions?
Try creating a settings module.
Make a settings folder in the same directory as manage.py.
Put your different settings files in that folder (e.g. base.py and prod.py).
Make __init__.py and import whatever settings you want to use as your default. For example, your __init__.py file might look like this:
from base import *
Run your project and override the settings:
$ python2.6 manage.py syncdb --settings=settings.prod
I do know that no matter what you do with manage.py, you're going to get that error because manage.py does a relative import of settings:
try:
import settings # Assumed to be in the same directory.
http://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---settings
Note that this option is unnecessary
in manage.py, because it uses
settings.py from the current project
by default.
You should try django-admin.py syncdb --settings=mysettings instead
this works for me:
DJANGO_SETTINGS_MODULE=config.settings.abc python manage.py migrate
this will help you:
create a another file "setting_prod.py" with your original settings.py file.
write down your setting which you need to run, in setting_prod.py file.
Then import setting_prod.py file in your settings.py file.
for ex.
settings.py:
VARIABLE = 1
import setting_prod
setting_prod.py
VARIABLE = 2
After importing setting_prod.py file in settings.py file, VARIABLE will set to new value to "2" from "1".
We can use this method to set different settings file, for example, I use different settings file for my unit test (settings_unit_test.py). Also I do have other settings file for different infrastructure environment settings_dev.py, settings_test.py and settings_prod.py.
In windows environment(same can done in linux as well)
set DJANGO_SETTINGS_MODULE=settings_unit_test
set PYTHONPATH=<path_of_your_directory_where_this_file_'settings_unit_test.py'_is_kept>