I am using django_cups in my project, and I am getting import error in the models line 4. Here is the models.py file
python ./manage.py runserver
...
File "/home/lex/myapp/django_cups/models.py", line 4, in <module>
import cups
ImportError: No module named cups
I have django_cups installed because when I import cups in the python shell it doesn't complain.
lex#lex-pc:~/django/mykapp$ python
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cups
>>>
>>>
But when I do it in the InteractiveConsole for Django, it complains...
(env)lex#lex-pc:~/django/myapp$ python ./manage.py shell
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import cups
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named cups
>>>
What could be the issue here?
The virtual environment, by default, does not include the system packages. To use both system and local packages, use virtualenv --system-site-packages when creating the environment.
Related
On my production environment only, my app uses America/Chicago as time zone while I have TIME_ZONE = "UTC" in my settings.
I'm running my code on Debian and the system locale is C.UTF-8 with Django 4.1.
I dug in Django code and find the source of my problem here: https://github.com/django/django/blob/c2118d72d61746f2462fca695dbf3adf44ebf8f7/django/utils/timezone.py#L72
I tried in ./manage.py shell to reproduce the problem:
# ./manage.py shell
Python 3.10.9 (main, Dec 21 2022, 08:51:48) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>> from django.utils import timezone
>>> import zoneinfo
>>> settings.TIME_ZONE
'UTC'
>>> timezone.get_current_timezone()
zoneinfo.ZoneInfo(key='America/Chicago')
And if I call the methods in get_current_timezone, I have a different result
>>> settings.USE_DEPRECATED_PYTZ
False
>>> zoneinfo.ZoneInfo(settings.TIME_ZONE)
zoneinfo.ZoneInfo(key='UTC')
Where could the America/Chicago come from ?
Willem was right, I used timezone.something() in my settings before the TIME_ZONE was set.
For posterity: I'm using django-split-settings and was using globs ("settings/*.py") and the order of the files was different locally and in production.
I installed django but when i run command django-admin startproject send_email it gives an error showing zsh: command not found: django-admin what can i do please help me. i search same question in google but the answers are not working
pathparakh#Paths-MacBook-Air send_email % django-admin startproject send_email
zsh: command not found: django-admin
pathparakh#Paths-MacBook-Air send_email % python3
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(3, 2, 5, 'final', 0)
>>>
I have the need to add version control for my models in the django admin and for that reason I have installed django-reversion from here.My server does not have have internet connectivity so I could not use pip.I had to manually download the zip file from github,upload it on to my server,extract the contents and run python setup.py install to install reversion.
However,when I try to import it,I get the following error :
root#ns1 /home/project# python
Python 2.7.3 (default, Feb 27 2013, 22:57:49)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 6, 0, 'final', 0)
>>> import reversion
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/reversion/__init__.py", line 11, in <module>
from reversion.revisions import default_revision_manager, revision_context_manager, VersionAdapter
File "/usr/local/lib/python2.7/site-packages/reversion/revisions.py", line 10, in <module>
from django.contrib.contenttypes.models import ContentType
File "/usr/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 83, in <module>
signals.request_started.connect(reset_queries)
File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 88, in connect
if settings.DEBUG:
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
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.
>>>
As per the github page,the current reversion version is 1.8.0 and should be compatible with django 1.6. What could be the problem ?
I know this is an old question, but in case anyone stumbles upon it: The problem here is that you're running the standard python interpreter, rather than the Django shell. You can do import django into a regular python interpreter to check the Django version, but it does NOT set all the various environment variables necessary to run django properly -- including importing django apps. In order to import a django app, you need to instead run the Django shell via
./ python manage.py shell
Can anyone help me. I have just installed django-tables2 using easy_install and when i try to import django_tables2 as tables in forms.py it gives me error unresolved import: tables then i try to reinstall again django_tables2 and give me the result as shown below.
$ easy_install django_tables2
Searching for django-tables2
Best match: django-tables2 0.13.0
Processing django_tables2-0.13.0-py2.7.egg
django-tables2 0.13.0 is already the active version in easy-install.pth
Using c:\python27\lib\site-packages\django_tables2-0.13.0-py2.7.egg
Processing dependencies for django-tables2
Finished processing dependencies for django-tables2
Thank you very much for help.
updated question.
I am still trying to figure out this problem. I am posting a new error on why I cannot import. By using python manage.py shell i get this error. I have no idea what is that mean.
$ python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django_tables2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build\bdist.win32\egg\django_tables2\__init__.py", line 3, in <module>
File "build\bdist.win32\egg\django_tables2\tables.py", line 4, in <module>
File "c:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "c:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
self._setup()
File "c:\Python27\lib\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.
Can anyone help me?:( Help is very much appreciated. Thank you.
django_tables requires that you actually be running within the django-"primed" python environment. You can access this by going to your project's directory and typing
python manage.py shell
Then you can import django_tables2 with no problems.
For further info, take a look at the following:
https://docs.djangoproject.com/en/dev/ref/django-admin/
Note that this is not about the admin template site (despite the url), it's about the django-admin.py (system-wide) and manage.py (project-wide) "helper" applications. manage.py shell actually starts up a standard python shell with several pre-loaded environment variables and a few imports.
Im trying to make an app with geoDjango but I'm actually having some problem to put it running.
When I excecute the "python manage.py syncdb" command, I have the following error:
File "/media/datos/Proyectos/CualBondi/git/cualbondi/myapp/models.py", line 10, in <module>
from django.contrib.gis.db import models
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/__init__.py", line 5, in <module>
from django.contrib.gis.db.models.aggregates import *
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/aggregates.py", line 2, in <module>
from django.contrib.gis.db.models.sql import GeomField
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/sql/__init__.py", line 2, in <module>
from django.contrib.gis.db.models.sql.query import GeoQuery
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/sql/query.py", line 4, in <module>
from django.contrib.gis.db.models.fields import GeometryField
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/fields/__init__.py", line 3, in <module>
from django.contrib.gis.db.backend import SpatialBackend, gqn
ImportError: cannot import name SpatialBackend
I give you here some extra information:
Django 1.3.1
Python 2.6
Ubuntu 10.04
PostgreSQL 8.4.8
Do you have any idea?
Thank you so much!
Maybe this helps, SpatialBackend seems deprecated. Read the warning below.
$ python manage.py shell
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.gis.db.backend import SpatialBackend, gqn
/usr/lib/pymodules/python2.7/django/contrib/gis/db/backend/__init__.py:5: UserWarning: The `django.contrib.gis.db.backend` module was refactored and renamed to `django.contrib.gis.db.backends` in 1.2. All functionality of `SpatialBackend` has been moved to the `ops` attribute of the spatial database backend. A `SpatialBackend` alias is provided here for backwards-compatibility, but will be removed in 1.3.
warn('The `django.contrib.gis.db.backend` module was refactored and '
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name gqn
I'm on django 1.3.0, there seems to work good, but only with that warning.