Django - Manage.py ModuleNotFoundError: No module named 'mysite' on windows - python

I'm trying to get Django to work. I installed Django used
django-admin startproject mysite
This created this structure
mysite
-- manage.py
-- mysite
- __init__.py
- settings.py
- etc etc...
now I'm when I run
python manage.py runserver
I get this error
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\MySoftware\python361\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
utility.execute()
File "C:\MySoftware\python361\lib\site-packages\django\core\management\__init__.py", line 307, in execute
settings.INSTALLED_APPS
File "C:\MySoftware\python361\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "C:\MySoftware\python361\lib\site-packages\django\conf\__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "C:\MySoftware\python361\lib\site-packages\django\conf\__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 936, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mysite'
Python cannot find 'mysite'
I've tried PYTHONPATH = 'my director' but so far nothing has worked. Problem seems to be that python cannot find mysite folder

Ok, I'm answering my own question again.
For some strange reason, Python is unable to find the app folder /mysite and even though I set
PYTHONPATH = C:\...\mysite
It did not work
I found a hack to include the directory by inserting this after
import sys
sys.path.append("C:\\xampp\\your\\path\\site1") <--- insert this line (3)
in manage.py, this forces python to look for your app folder when it runs the server. Put the double slashes as well.

Related

Django on Heroku: KeyError: 'collectstatic'

I'm trying to deploy my simple Django web app on Heroku, but the build fails with the following error:
Successfully installed asgiref-3.3.4 dj-database-url-0.5.0 django-3.2 django-heroku-0.3.1 gunicorn-20.1.0 numpy-1.20.2 pillow-8.2.0 psycopg2-2.8.6 pytz-2021.1 sqlparse-0.4.1 torch-1.8.1 torchvision-0.9.1 typing-extensions-3.7.4.3 whitenoise-5.2.0
-----> $ python pytorch_django/manage.py collectstatic --noinput
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 237, in fetch_command
app_name = commands[subcommand]
KeyError: 'collectstatic'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/build_6b3954ac/pytorch_django/manage.py", line 22, in <module>
main()
File "/tmp/build_6b3954ac/pytorch_django/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 244, in fetch_command
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'project'
! Error while running '$ python pytorch_django/manage.py collectstatic --noinput'.
I have successfully deployed other Django apps on Heroku and I don't get what's wrong here. What does KeyError: 'collectstatic' mean or where does it come from?
Locally, the app runs fine and also python manage.py collectstatic runs fine. What's the problem? Could it be related to Python 3.9? I have Python 3.8 locally.
Edit: This also happens with a freshly created Django app - no changes in the code whatsover.
The problem was my own fault but not in the Django app but in my Heroku configuration.
Inside my Heroku environmental variables (Settings > Config Vars), I had set the DJANGO_SETTINGS_MODULE incorrectly to project.prod_settings.
However, my Django project was called pytorch_django so I had to set it to pytorch_django.prod_settings.
That's what caused the error ModuleNotFoundError: No module named 'project'.
Changing DJANGO_SETTINGS_MODULE to the correct path solved the problem.

How do I solve: "ModuleNotFoundError: No module named error when starting Django shell" error

When running 'python manage.py shell', I'm getting the following message:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "D:\Anaconda3\envs\mysite\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "D:\Anaconda3\envs\mysite\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "D:\Anaconda3\envs\mysite\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Anaconda3\envs\mysite\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "D:\Anaconda3\envs\mysite\lib\site-packages\django\apps\config.py", line 116, in create
mod = import_module(mod_path)
File "D:\Anaconda3\envs\mysite\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'polls.apps.PollsConfigdjango'; 'polls.apps' is not a package
I have tried another solution where I had to use ./manage.py shell --plain, but it's does't work. Also tried a solution that stated the ipython version wasn't correct, but that solution didn't solve anything for me either.
You have an error in your Python code. Django cannot be started because the configuration is invalid. One of your INSTALLED_APPS is broken. A hint what could be wrong is given in the traceback.
For the actual error is, you need to figure it out yourself, as it would probably involve copy-pasting a lot of code from your project here and would not be a good fit for StackOverflow Q&A format.

no module name 'celery' after migrate

I have been following tutorial how to import celery and run rabbitmq to my django project. and I have been following a tutorial on youtube. I do not now what I have missed but after the part where i ran python manage.py migrate djcelery it gives the result of:
root#1095ea233e7b:/app/main# python3 manage.py migrate djcelery
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 302, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 99, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/app/main/actinbox/settings.py", line 6, in <module>
import djcelery
this is the tutorial that I have been following:
Django Celery Part 1
As I said in a comment, it's probably because you installed celery 4.0.0.
the tutorial you followed uses celery 3.0.23.
If you really want to use the tutorial use pip install django-celery==3.0.23
this is not recommended because it's an outdated version.

ImportError: No module named 'zope' for cryptoassets.django while running the initialize database command

Okay, Im trying to use cryptoassets.django here. Iv got some help from Mikko about the python version being an issue(I was using 2.7.x, cryptoassets uses 3.4.x). I just solved by doing this
$virtualenv -p python3 venv
I also reinstalled Django because manage.py was pulling up error, probably because it was overwriting the bin/activate.. files.
With that out of the way. I installed the cryptoassets.django app
$pip3 install cryptoassets.django
Wonderfull. I set it up like the tutorial on bitbucket for the cryptoassets django intergration.
The problem comes in when i try to initialize the cryptoasset database with
$python manage.py cryptoassets_initialize_database
I get an Import error:
ImportError: No module named 'zope'
Here is the Traceback
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site- packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site-packages/django/core/management/__init__.py", line 190, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site-packages/django/core/management/__init__.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/caleb/Desktop/bitkoin/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site-packages/cryptoassets/django/management/commands/cryptoassets_initialize_database.py", line 5, in <module>
from cryptoassets.core.service.main import Service
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site-packages/cryptoassets/core/service/main.py", line 25, in <module>
from ..configure import Configurator
File "/home/caleb/Desktop/bitkoin/lib/python3.4/site-packages/cryptoassets/core/configure.py", line 13, in <module>
from zope.dottedname.resolve import resolve
ImportError: No module named 'zope'
What could be the issue now.
Cryptoassets is Python 3.4+ only and you are trying to run it on Python 2.

Can't run django project on mac imported from ubuntu

My colleague started a django project on Ubuntu. I want to run the same project on OS X Yosemite. We both use python3.4 and following packages:
Django (1.8.2)
Pillow (2.8.1)
pip (7.0.3)
setuptools (17.0)
wheel (0.24.0)
And we both run project in virtual env.
When I cloned project, I used
python manage.py syncdb
And I got following results:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/Users/user/Desktop/projectName/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/Users/user/Desktop/projectName/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Users/user/Desktop/projectName/projectName/main/admin.py", line 3, in <module>
from models import ContactLink, ContactPost, Personnel, WorkCategory, Service, Skill, Work, Customer
ImportError: No module named 'models'
I have no idea why this happens and used several methods described in this site and other sites.
What happened and why?
This should have thrown an error on your Ubuntu install as well.
You need to change line 3 of projectName/main/admin.py to:
from .models import ContactLink, ContactPost, Personnel, \
WorkCategory, Service, Skill, Work, Customer
Notice the added dot . in the from .models part. The import should be relative (assuming all of these models exist in the same main app).

Categories

Resources