i am using pycharm with django. When i do the runserver command, my project starts up and everything is fine.
if i use the pycharm run command - that green arrow at the top - then i get problems.
The problems are:
runnerw.exe C:\development\python\python.exe manage.py runserver 127.0.0.1:8000
Traceback (most recent call last):
File "manage.py", line 11, in
import settings
File "C:\development\PycharmProjects\dumpstown\settings.py", line 185, in
add_to_builtins('gravatar.templatetags.gravatar')
File "C:\development\python\lib\site-packages\django\template\base.py", line 1017, in add_to_builtins
builtins.append(import_library(module))
File "C:\development\python\lib\site-packages\django\template\base.py", line 963, in import_library
raise InvalidTemplateLibrary("ImportError raised loading %s: %s" % (taglib_module, e))
django.template.base.InvalidTemplateLibrary: ImportError raised loading
gravatar.templatetags.gravatar: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
Process finished with exit code 1
And stem from my use of add_to_builtins here: (this is in the settings.py file)
#gravatar stuff here.
add_to_builtins('gravatar.templatetags.gravatar')
I know this is the problem, because if i remove this line in the settings.py file? everything works fine.
Is there a way to remedy this problem for pycharm?
You need to set your Django settings module in Settings | Django Support | Settings
http://www.jetbrains.com/pycharm/webhelp/django-support.html
UPDATED
The problem with django-gravatar is that it's templatetags import django.contrib.auth.models.User which relies on settings module while setting module is loaded in Django by DJANGO_SETTINGS_MODULE environment variable, which is set internally by execute_manager function call in manage.py, which is executed AFTER import of settings. So when you use undocumented add_to_builtins feature just in settings.py at that point you have no DJANGO_SETTINGS_MODULE env variable set. So that is not a problem of PyCharm, but a problem of unset environment variable and usage of undocumented Django feature add_to_builtin.
When I ran the same project from Unix console I get the same error.
Probably you have DJANGO_SETTINGS_MODULE set in your environment if it works from console.
So to make it work in PyCharm you need to set up the variable in Django run configuration.
You can read here about that (see Environment variable section).
As i answered here: https://stackoverflow.com/a/11299516/1061426
pycharm is broken and doesn't work with add_to_builtins. The two obvious solutions are:
don't use pycharm, use some free django plugins for eclipse, or old school text editing?
use pycharm, just don't use add_to_builtins. This is the route i've gone down - it is annoying fixing all the template's to import a module, but it was a lot simpler in my case than the hassle of porting across to a new IDE.
Related
I’m having problems with one module in my Visual Studio Core project. It is not being found. It generates this error:
File "C:\Users\danj\source\repos\project_django\comstockapt\comstockapt\urls.py", line 28, in
from comstockapt.email_accounts import views as email_views
ModuleNotFoundError: No module named 'comstockapt.email_accounts'
I’ve gone through many, many similar questions, but I can’t get it to work. I’m using the virtual environment. When I execute “python” in the terminal and then run ‘help(“modules email_accounts”)’ it shows the module with all of its files.
When I run help(“modules”), I see all of my modules listed in the response.
Here is my urls.py file within module comstockapt.comstockapt.
Here is my installed_apps:
I've checked environment variables, the virtual environment, the directory structure. What can I try next?
You should import views from email_accounts like this:
from email_accounts import views as email_views
I am using a Python 2.7 virtualenv with the MySQLdb package installed.
If I run Python from the command line and execute import MySQLdb, this works without error. If I run it from the PyCharm terminal, however, I get an error:
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
The same pattern occurs if I execute a file test.py containing the line import MySQLdb. It works when executed from the command line and crashed when executed from PyCharm.
I have googled the error and it seems that uninstalling and reinstalling MySQLdb could fix it. But I would like to understand why the error only occurs in PyCharm.
I have made sure that both the command line and the PyCharm terminal use
the same virtual environment (by checking sys.executable)
the same working directory (by checking os.getcwd())
the same path (by checking sys.path)
I have also checked that PYTHONPATH is undefined.
What other difference could there be?
You have to point pycharm to your virtualenv. Go to settings -> project interpreter and give pycharm the path to your python executable. Once there it should work. Note if you have a hybrid WSL/windows setup you will need one virtualenv for WSL and a separate virtualenv for windows/pycharm.
I'm a beginner in django and I got a lot of errors when using template module from django.
The following works from the python shell:
from django import template
t = template.Template('My name is {{ name }}.')
When i use this code , i get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/django/template/base.py", line 123, in __init__
if settings.TEMPLATE_DEBUG and origin is None:
File "/usr/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DEBUG,but
settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Dose anyone have an idea about this error?
You can't access and run django projects from python shell. Django doesn't know what project you want to work on.
You have to do one of these things:
1. python manage.py shell
2. Set DJANGO_SETTINGS_MODULE environment variable in your OS to mysite.settings
3. Use setup_environ in the python interpreter:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
The first one is easiest and best method. Run your code in the django shell.
If you want to use the template system without the django shell, you can do:
from django.conf import settings
settings.configure()
and after this your code
t = template.Template('My name is {{ name }}.')
I also had the same problem.
The main point is that in the django book 2, this piece of code is not supposed for users to put into a Python IDLE started from your python.exe ( presumably in C:\Program Files\Python33 on Windows).
Rather, it is just showing how the piece of code will look like. The solution is simply run 'cmd' and change the directory to your 'mysite', and run
python manage.py shell
which ensure that the 'mysite.settings.py' is used.
Of course the #Sudipta's answer works correctly but (just for the future) for the explanation you can also visit the "Creating Template Objects" -> "A special Python prompt" section in Django book
To fix this issue, you need to run it in python manage.py shell instead of just python.
Reason: Well, many parts of Django rely on settings and you won't be able to use them until it knows which settings file to use. When you run it on python manage.py shell, Django knows which settings file to use and does the job for you.
The error message states that you need to set the DJANGO_SETTINGS_MODULE variable.
Running export DJANGO_SETTINGS_MODULE=mysite.settings, worked for me.
I'm trying to get an existing Django project to work on my mac.
I managed to configure everything and opening the project in PyCharm.
When I run I get the error:
TemplateSyntaxError at /
Caught BundleError while rendering: 'stylesheets' not found (using staticfiles finders)
We use Django 1.3 and Webassets 0.7 (just updated from earlier versions).
I have an assets.py defined in my application folder defining the various bundles.
Any suggestions on solving this?
EDIT: Ok, a bit further ... I added my project.assest to settings and now I don't have the Bundle error. I do still have another problem:
Caught BundleError while rendering: 'styles/libs/jquery-ui-timepicker-addon.css' not found
Path look ok, collectstatic works, copies, file is in place ...
any suggestions?
A project-wide assets.py (as opposed to one in an app-directory) is no longer automatically read, you need to define such files through a ASSETS_MODULES setting now.
If you are using staticfiles, pay attention to the fact that the staticfile finders will not be used unless Django is in debug mode (settings.DEBUG=True). In production mode, webassets will assume that collectstatic has been run first.
In settings.DEBUG=True mode, the reverse is true: ONLY the Django staticfile finders will be used. You could try opening a shell (./manage.py shell) and see if the following finds your file:
from django.contrib.staticfiles import finders
finders.find('styles/libs/jquery-ui-timepicker-addon.css')
If it does, then so should webassets.
I had a similar error arise
'[BUNDLE_NAME]' not found (using staticfiles finders)
This wasn't a very helpful message, so ended up looking into ./manage.py shell and running
>>> from django.conf import settings
>>> from [ASSETS_FILE_PATH.assets] import [BUNDLE_NAME]
>>> [BUNDLE_NAME]
If this isn't there it may give you another message.
I'm deploying my django application onto a CentOS 5.5 server, with django-1.1.4 over python-2.6.5.
I have multiple settings files inside the myapp/settings/ folder.
I would like to run the syncdb; here's what I do (with myapp inside myproject folder):
$> cd /var/www/apps/myproject
$> export PYTHONPATH=/var/www/apps/myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.my_serverconfig
$> python26 myapp/manage.py syncdb
Django then issues an error like this :
Error: Can't find the file 'settings.py' in the directory containing 'myapp/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.)
Traceback (most recent call last):
File "emon/manage.py", line 17, in <module>
execute_manager(settings)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 360, in execute_manager
setup_environ(settings_mod)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 343, in setup_environ
project_module = import_module(project_name)
File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named my_serverconfig
In the myapp.wsgi file, os.path is appended with myproject path, and the os.environ['DJANGO_SETTINGS_MODULE'] is set also. Apache (through mod_wsgi) can start the app with no such error.
Finally, this works under Windows, where I run python-2.6.6 with django-1.1.1.
$> d:
$> cd d:\Code\myproject
$> export PYTHONPATH=d:\Code\myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.dev_settings
$> python.exe myapp/manage.py syncdb
I know the versions are not the same, but I'm not sure that the minor differences may cause all my woe. Moreover I don't seem to find the exact same python version for Windows.
Any thoughts? Thanks a lot for reading.
O.
EDIT: added the manage.py content
#!/usr/bin/env python
from django.core.management import execute_manager
import os
if __name__ == "__main__":
settings = None
try:
if os.environ.has_key('LOCAL_SERVER_SETTINGS'):
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings.%s' % os.environ['LOCAL_SERVER_SETTINGS']
if os.environ.has_key('DJANGO_SETTINGS_MODULE'):
settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'])
if settings is None:
import settings
execute_manager(settings)
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
import traceback
traceback.print_exc()
sys.exit(1)
EDIT : more on what happens in the myapppackage
I patch some django functions/classes from within the myapp.__init__ module. I was thinking the import django part in this module was causing a circular reference. The code is executed when I load myapp.settings.[any_config] and could have caused the crash. But then, how come the correct settings module is loaded with no error by WSGI, and that it works fine also on Windows? More : after commenting out the said code parts, the ImportError is still there.
If you move your settings file, you need to modify manage.py to tell it where to find it. The default is for it to be in the same directory as manage.py, but if you move into another python module(folder with __init__.py) then you need to update the reference in manage.py.
Look in manage.py where it imports your settings module, and import settings.dev_settings in your case instead. Look for 2 important lines
imp.find_module('settings') # Assumed to be in the same directory.
...
import settings
Change these to reference where you moved the settings file to(assuming you moved to settings.dev_settings):
imp.find_module('settings.dev_settings') # Assumed to be in the same directory.
...
from settings import dev_settings as settings
You can also use the command line option --settings to specify which settings file to use.