I am trying to run coverage on a django project inside of a virtual environment and am having some issues with ModuleNotFoundError. As per this answer I have installed coverage inside of the virtual environment but am finding that different python interpeter are being used with coverage compared to when running the file with python manually.
When running python manage.py test the output is:
(env) λ python manage.py test
C:\Users\omarh\Projects\hive\hive-proj\env\Scripts\python.exe
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
where the first line is the result of print(sys.executable)
However when running coverage run manage.py test with coverage 5.0.1 I get:
(env) λ coverage run manage.py test
c:\users\omarh\projects\hive\hive-proj\env\scripts\python3.exe
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\core\management\base.py", line 320, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\core\management\base.py", line 294, in create_parser
self.add_arguments(parser)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\core\management\commands\test.py", line 44, in add_arguments
test_runner_class = get_runner(settings, self.test_runner)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\test\utils.py", line 301, in get_runner
test_runner_class = test_runner_class or settings.TEST_RUNNER
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\conf\__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\site-packages\django\conf\__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "c:\users\omarh\projects\hive\hive-proj\env\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'hive'
the result of running python and python3 in the venv give an indentical response of:
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
and running python3 manage.py test is successful.
Running with coverage 3.6 installed instead is also successful however that errors with:
Exception: Couldn't find chunk # 330
when trying coverage html
Any ideas about why this is happening with Coverage 5.0 and how to fix it? Or how to get Coverage 3.6 to build the HTML report?
Related
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.
I'm writing project using docker and django (2.1).
I've installed module 'django-widget-tweaks' inside a container and added 'widget_tweaks' to INSTALLED_APPS. When I start project using docker-compose up everything works well, but when I want to run something like docker-compose run <container name> python manage.py test in another terminal an error occurs.
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/local/lib/python3.6/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 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'widget_tweaks'
But when I directly run the same command python manage.py test inside a docker container, it works. widget-tweaks has been installed both on container and on my machine. Could you advice something to solve the problem?
Thanks in advance!
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.
I'm running Django 1.9 on python 3.4 in a virtualenv. It has been working, but today, it's not.
When I run ./manage.py runserver, I get the following error:
(django1)dan#danvb:~/Share/django1/suorganizer$ ./manage.py runserver
Performing system checks...
System check identified no issues (0 silenced). Unhandled exception in
thread started by <function check_errors.<locals>.wrapper at
0x7f48a01d2c80> Traceback (most recent call last): File
"/home/dan/Share/django1/lib/python3.4/site-packages/django/utils/autoreload.py",
line 226, in wrapper
fn(*args, **kwargs) File "/home/dan/Share/django1/lib/python3.4/site-packages/django/core/management/commands/runserver.py",
line 117, in inner_run
self.check_migrations() File "/home/dan/Share/django1/lib/python3.4/site-packages/django/core/management/commands/runserver.py",
line 163, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "/home/dan/Share/django1/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 20, in __init__
self.loader = MigrationLoader(self.connection) File "/home/dan/Share/django1/lib/python3.4/site-packages/django/db/migrations/loader.py",
line 49, in __init__
self.build_graph() File "/home/dan/Share/django1/lib/python3.4/site-packages/django/db/migrations/loader.py",
line 170, in build_graph
self.load_disk() File "/home/dan/Share/django1/lib/python3.4/site-packages/django/db/migrations/loader.py",
line 105, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name)) File
"/home/dan/Share/django1/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 2212, in _find_and_load_unlocked File "<frozen importlib._bootstrap>",
line 321, in _call_with_frames_removed File "<frozen importlib._bootstrap>",
line 2254, in _gcd_import File "<frozen importlib._bootstrap>",
line 2237, in _find_and_load File "<frozen importlib._bootstrap>",
line 2224, in _find_and_load_unlocked ImportError: No module named 'core.migrations.delete'
I uninstalled and reinstalled Django==1.9, and get the same error. In lib/python3.4/site-packages/django/core/, I don't have a migrations directory. I'm not sure what to look for. The command ./manage.py runserver worked for me on this project not so long ago.
Thank you.
Solved.
In I have a Django migrations folder, core/migrations/ in which instead of actually deleting a migration module, I renamed it xxx.delete thinking I might want to revert to it. Upon actually deleting that module, my ImportError was resolved.
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).