Python manage.py ImportError: No module named django - python

I can't seem to get my manage.py script to recognize the django module. pip install req/dev.txt installs all of the modules I would expect it to into my my python virtual environment (stored at .venv).
I think I've narrowed it down to a problem with either my $PYTHON_PATH or with my pip requirements files. I've looped through sys.path, and I see that one of the values points to a folder in .venv that I can confirm contains the django module.
I'm unfortunately at a loss. I've searched through a dozen related questions on Stack Overflow and have yet to find a solution that works. Does anyone have any clues to point me in the right direction?
Error message:
$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/nathan/www/myapp/.venv/lib/python2.7/site-packages/django/apps/config.py", line 119, in create
import_module(entry)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named django
Contents of reqs/dev.txt
-r common.txt
django-debug-toolbar==1.3.2
Contents of reqs/common.txt
django==1.8
Fabric==1.10.2
ShopifyAPI>=2.1.5
Sphinx==1.3.1
celery>=3.1.20
django-compressor>=1.5
django-toolbelt>=0.0.1
jdcal>=1.0
kombu>=3.0.35
openpyxl==2.2.5
python-dateutil>=2.4.2
psycopg2>=2.5
requests==2.7.0
whitenoise==2.0
shippo==1.4.0
Contents of manage.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.dev")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

Problem solved after a good night's rest.
The issue was that my INSTALLED_APPS had a reference to kombu.transport.django, which seems to be a completely unnecessary dependency we added for celery. More information on that here: Celery, kombu and django - import error
How I discovered the solution, in case the debugging technique helps anyone else:
I started by diving into the stack trace. I (temporarily) modified the files in .venv/ to add print statements for the input variables until I found my first clue in populate() in .venv/lib/python2.7/site-packages/django/apps/registry.py
Printing through each entry in populate() allowed me to see that the package that was breaking was kombu.transport.django and NOT the django module proper. Deleting that entry from my INSTALLED_APPS solved the problem.
To cleanup I reverted the files in .venv/ by removing my print statements and I was off and running!

Related

In my src/ my config.py (project variables) won't import to other directories

In my src/ directory of my project as of this post I have a config.py I want to be used in importing most of my python files in this PyObj-C and py2app project. As of the current sent link, everything works fine when ran with python3. However, once I build it with my Makefile using make build which runs python setup.py py2pp -A I get this error when running the build:
Traceback (most recent call last):
File "/Users/leif/PycharmProjects/shoutout/src/dist/Shoutout!.app/Contents/Resources/__boot__.py", line 149, in <module>
_run()
File "/Users/leif/PycharmProjects/shoutout/src/dist/Shoutout!.app/Contents/Resources/__boot__.py", line 143, in _run
exec(compile(source, script, "exec"), globals(), globals())
File "/Users/leif/PycharmProjects/shoutout/src/main.py", line 2, in <module>
import AppDelegate
File "/Users/leif/PycharmProjects/shoutout/src/AppDelegate.py", line 11, in <module>
from WindowController import mainWindow
File "/Users/leif/PycharmProjects/shoutout/src/WindowController.py", line 14, in <module>
from sutils.tasks import backendTasks as tasks
File "/Users/leif/PycharmProjects/shoutout/src/sutils/tasks.py", line 17, in <module>
from config import ymlDir, scheduleDir, configDir, resourcesURL
ModuleNotFoundError: No module named 'config'
2022-11-11 15:45:00.625 Shoutout![60555:5326242] Launch error
2022-11-11 15:45:00.625 Shoutout![60555:5326242] Launch error
See the py2app website for debugging launch issues
It seems my tasks.py, which has some classmethods in it for background tasks, in my utilities folder can't import it once it's compiled. It can't seem to access config.py once it's built for some reason or another
I have my setup.py here, which includes the config.py and the rest of the utilities folder's files and beyond that directory included in the data_files variable within the setup method in a list. I have also tried not using sutils/config.py, 'sutils/tasks.py' and so on and just did 'config.py', 'tasks.py', but this didn't work either.
I am stumped here. Is there a way to get around this, or a clear reason I am missing right under my nose somehow?
Thanks for any input or comments!
Again the specific commit of my src/ of this post I want to solve:
https://github.com/leifadev/shoutout/tree/398846a045bbb0c17bbd6905a578a2907349d9fa/src

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. django 1.8

I have a problem with django 1.8. The console shows:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
commands = get_commands()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
There are answers elsewhere on Stackoverflow about how this could be actually about app registry, but I believe it is more often just an indicator that something is wrong in the settings and Django didn't handle failure well.
I just had the exact same error by just having a bad logger filename in LOGGING settings -- the directory I was referring to did not exist, so logger initialisation failed.
It looks like you are using a Virtual Environment.
Perhaps you have an app included under INSTALLED_APPS in settings.py, but it isn't installed (e.g. via pip install django-multisite) in your virtual environment.
(I received this same error after starting to use a Virtual Environment.)
I actually had the same error today, which is strange as everything was working properly yesterday and I do not think I have changed anything.
I got the error when trying to start up the dev server. I had recently upgraded this project to 1.8.
The solution for me was: I changed my PYTHONPATH on the startup script (to make sure it also points to the 1.8 version) of the dev server and everything seemed to work perfectly again.

django could not import settings

I once wrote an django app, named superlists, and settings was in superlists.settings. It was for tutorial, and I was changing differnt settings, little realizing what I was doing.
And now when i starting new django project - it tells me it could not import superlists.settings
I reinstalled Pycharm, django, python (both versions 2.7 and 3.3), deleted all virtual envs, deleted everything that might be connected to it several times... IT IS STILL THERE! on a new, clean version of python, which even dont have django - there is DJANGO_SETTINGS_MODULE in sys.path pointing to superlists.settings. Each new project on a clean, with default settings, version of pycharm, tells me that it cannot import superlists.settings.
I delete DJANGO_SETTINGS_MODULE pointing to that settings from sys.path from cmd - exit then watch again - its there.
Where it takes it? I got no idea.
here is traceback:
Traceback (most recent call last):
File "D:/Python27/Lib/site-packages/django/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 261, in fetch_command
commands = get_commands()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'superlists\settings.py' (Is it on sys.path? Is there an import error in the settings file?): Import by filename is not supported.
It turned out that I created new environment system variable DJANGO_SETTINGS_MODULE. So evident, but it almost cost me sanity.
Change the environment variable from
DJANGO_SETTINGS_MODULE=superlist.superlist.settings
To DJANGO_SETTINGS_MODULE=superlist.settings
or use an __init__.py in superlist

Upgrading a Python Project from version 2.7 to 2.7.4

I have a Django "project" that I have inherited, which I am developing in Eclipse. On my OS (windows 7 32 bit), I have Python 2.7.4 installed, likewise for my virtualenv. However, on my project (extracted from SVN) the Python version is 2.7 only.
This causes a conflict when trying to create another superuser (I do not know the original superuser name/password) where I get the message:
cannot import maxrepeat
How do I upgrade the python version located at:
c:\users\"username"\workspace\"project"\scripts
from 2.7 to 2.7.4?
Apologies if I have omitted some important details, or if I am asking the wrong question as I am newbie to Django/python development.
EDIT
Having spoken to a friend before referring back to these responses (thanks btw), he advised me to copy over the contents of the 'scripts' folder within my virtualenv to the folder:
c:\users\"username"\workspace\"project"\scripts
I did that, so in theory, they are both now running from python version 2.7.4.
However, when I run the script
python manage.py createsuperuser
I get the following FULL Traceback:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 453, in execute_from_command_line
utility.execute()
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 263, in fetch_command
app_name = get_commands()[subcommand]
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 109, in get_commands
apps = settings.INSTALLED_APPS
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 53,
in __getattr__
self._setup(name)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 48,
in _setup
self._wrapped = Settings(settings_module)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 134,
in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SE
TTINGS_MODULE, e))
ImportError: Could not import settings 'hub.settings' (Is it on sys.path?): No module name
d hub.settings
I have checked the system variables and I'm sure my project is on the Python system path. I have also checked 'django.contrib.auth' is enabled in my INSTALLED_APPS in the settings.pyfile.
Edit 2
Many other posts suggest it's a cross over of Python versions. However when I check the version number using the command:
$scripts\python.exe --version
I get Python 2.7.4 for each installation (Project & virtualenv)
Based on this information:
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 134,
in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SE
TTINGS_MODULE, e))
ImportError: Could not import settings 'hub.settings' (Is it on sys.path?): No module name
d hub.settings
It looks like your application is called hub? The problem here is that it's trying to import hub.settings - but it can't find it. So for some reason, your settings.py is not on the path.
You can check this by editing C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py, and somewhere before line 134 you can put import sys; print(sys.path). Then check to see that the path where your settings.py file lives is available. If it is, something else weird is happening.
If not, just go ahead and pull those lines from the __init__, and try running manage from the same directory your settings file is in.

Django manage.py custom command on Heroku

I am trying to run a cron job with a Heroku hosted website. I am using a Django back-end. I have written a custom manage.py command that works locally (status_crawl.py). I have seen a few guides and posts on how to fix this, but the suggestions did not work (such as Running Django custom manage.py task on Heroku - Importing Issues and this guide).
Even weirder is that it is recognizing the other commands. (And I managed to get the basic task to work by using runscript from the django-extensions framework). It is still bothering me though.
My basic file structure likes like:
likes:
...
management:
__init__.py
commands:
__init__.py
status_crawl.py
The traceback is:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named commands.status_crawl
The other weird thing is that when I run 'heroku run python manage.py help' the command is showing up as an option. It's just not running when I try to run the script myself using the manage.py command.
Thanks!
Probably it's related to your PYTHONPATH variable on Heroku. This variable on Heroku may differ from your local PYTHONPATH. Try to import from the root of your project, like:
from likes.management.commands import status_crawl

Categories