Not able to run the coverage with pytest-cov and pytest - django

Facing issues in running the pytest with coverage, I have gone through the SO posts and not able to fix this, I believe I'm missing something here..
Getting the following errors where users is an app of my project
ModuleNotFoundError: No module named 'users'
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
My pytest.ini file contents
[pytest] DJANGO_SETTINGS_MODULE = cloudstack.settings
python_files = tests.py test_*.py *_test.py
addopts = -v --ignore=venv --cov=. --cov-report=html
I have tried adding DJANGO_SETTINGS_MODULE as environment variable too then I'm getting different error saying that cloudstack module is not found.
I'm in the activated environment while performing these tests.
EDIT
I looked into every post of the stack overflow to fix the errors in running the pytest with coverage none of them helped me in resolving this.
Django upgrading to 1.9 error "AppRegistryNotReady: Apps aren't loaded yet."
PATH issue with pytest 'ImportError: No module named YadaYadaYada'
ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
And I had everything aligned as per the instructions in the SO posts'
$ type -a coverage coverage is
/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/coverage
$ type -a pytest pytest is
/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/pytest
$ type -a python python is
/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/bin/python
sys.path
['/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cloudstacko', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/cygdrive/c/#Work#/Python/Django/Projects/cloudstacko/cvenv/lib/python3.6/site-packages']

The library which saved my day.
$ pip install pytest-django

Related

Python Django set DJANGO_SETTINGS_MODULE when migrating source code

Task: Set up a new running environment by being provided python/Django source code and some additional details.
Problem: Cannot get Django-admin to validate due to missing/incorrect settings configuration
"django.core.exceptions.ImproperlyConfigured Requested setting USE_I18N, but settings are not configured. ..... "
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
Env Details: Ubuntu OS, Python 2.7, Django 1.7, PostgreSQL (also Supervisor + gunicorn) Running a venv located **/home/dave/python-env/vas/**bin/activate
Python sys.path
/usr/lib/python2.7/* (multiple defined)
/home/dave/python-env/vas/python2.7/site-packages
So tried several methods (including #export DJANGO_SETTINGS_MODULE=project-name.settings....) with little success.
How can one set the DJANGO_SETTINGS_MODULE variable?
os.environ.setdefault() is set in wsgi (I know this is the next step)
BUT this value is also set in /manage.py ...?
os.environ.setdefault("DJANGO_SETTINGS_MODULE", project_name.settings)
The directory /var/www/app/ (where the python source code is located) has several files, one of them is the project_name where the settings.py sits....
I am new to python/django...
Trying to get django-admin.py validate to validate.
Update: Running #python manage.py runserver runs OK. #python manage.py validate|check returns "System check identified no issues (0 silenced)
Running #django-admin.py check returns the error in question. "You must either define DJANGO_SETTINGS_MODULE ...."
UPDATE 2: Solution
Turns out you don't need django-admin.py as suggested by (Alasdair) and you can use manage.py.
Details - If the 'manage.py check' function returns no issues and #pip install -r requirements.txt completes within your virtual environment THEN one can run #manage.py createsuperuser
I was able to use #manage.py runserver after creating a super user, and using this new user (as the database tables were empty for security reasons) I was able to log into 127.0.0.1:8000/admin. From there the models/tables were visible and using the admin functions I could create a new user + group to access the original system that was being migrated as an admin user.
Also not that a database was required (running postgres) with db/username/pass as per settings files and a git repository (at least empty initialised) for raven...
hope this helps someone coming into python.

Problem while using mypy with my django project

I have implemented mypy in my django rest framework but I am getting errors ModuleNotFoundError: No module named 'config' while running mypy.Is there any wrong with my django_settings_module in my mypy.ini file ?
I used to run my project with the command python manage.py runserver --settings=config.settings.development which was working fine but while configuring this setting in the mypy it is giving me error. What I might be doing wrong ?
Any help would be appreciated.
mypy.ini
[mypy]
plugins =
mypy_django_plugin.main,
mypy_drf_plugin.main
ignore_missing_imports = True
warn_unused_ignores = True
strict_optional = True
check_untyped_defs = True
follow_imports = silent
show_column_numbers = True
[mypy.plugins.django-stubs]
export PYTHONPATH=$PYTHONPATH:D:\DjangoProjects\project\config
django_settings_module = config.settings.development
settings directory
/project
/config
__init__.py
urls.py
wsgi.py
/settings
__init__.py
base.py
development.py
wsgi.py
app_path = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
)
sys.path.append(os.path.join(app_path, "project"))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_wsgi_application()
If anybody happens to run into this and was as confused as I am, here is what I did to make sure the mypy django plugin works.
I indeed had to add my project root to PYTHONPATYH, such that the plugin will be able to find your project. But let me clarify. In windows, you can search for 'environment variables' and create an environment variable named PYTHONPATH there, or edit the one that is already there with an added path.
I had been banging my head against the walls for a couple hours, because I didn't know how to properly set the PYTHONPATH. When you add this variable, make sure it is the full path, and that the folder you're specifying is the folder that includes manage.py
For me, that was C:\Users\...\myproject (without the trailing slash).
I hope that works and that I can save someone from wasting hours of their life like I did.
Adding my two cents here - in my case I was running a system-wide mypy instead of the one from virtualenv:
Even though I was in the virtualenv
(env) ➜ project git:(develop) ✗ which python
/Users/Kramer/Documents/projects/example/env/bin/python
Mypy was not aliased:
(env) ➜ project git:(develop) ✗ which mypy
/usr/local/bin/mypy
The solution was to either:
run mypy directly
(env) ➜ project git:(develop) ✗ ../env/bin/mypy .
ensure that you have all the dependencies installed in the system-wide python installation

My Django secret key is in an environment variable but I can't do syncdb

I'm setting up a Django project with different files for local and production settings. I can confirm that my Django secret key is successfully in an environment variable in virtualenv and when I do runserver I get no error. However when I try manage.py syncdb I get
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
I don't understand why I can successfully browse to the site after runserver but I can't sync the database. When I run env I can see that the secret key is there and in my base settings file (imported into local settings) I am doing this:
SECRET_KEY = os.environ.get('MY_SECRET_KEY')
Any help debugging this would be greatly appreciated.
Euan
I'm not sure why the runserver command is working while syncdb isn't, but you can sort it out by adding a environment variable for DJANGO_SETTINGS_MODULE in the same way you did for the SECRET_KEY. The only difference is that you don't need to reference DJANGO_SETTINGS_MODULE within the django code anywhere. I'm running my own setup in exactly that way and the only problem I run into is forgetting to change the settings module when I switch between projects :-)
EDIT: I didn't realise that you were adding --settings=myapp.settings.local to runserver as well as syncdb. The reason you need to do this is that you are using settings on a different path from the default so python can't find them. Also, although you set the DJANGO_SETTINGS_MODULE in the wsgi file, this is only fired when the site is accessed via your webserver. When running a manage command the wsgi file is ignored (AFAIK) so adding DJANGO_SETTINGS_MODULE to your environment variables in the same way as SECRET_KEY makes your settings file available to the manage command.
Hope that helps
Somewhat similar situation here, using virtualenv running an outer script which involves django models.
To make this work please make sure:
Your sys.path list has a path to your virtualenv site-packages. For me it's: sys.path.append('/home/user/.virtualenvs/Project/local/lib/python2.7/site-packages')
Your django settings variable is added to os.environ. Eg: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Project.settings")

Error: Could not import settings

I'm switching a very large multi-package, multi-app Django (1.4.5) project from using Django's native test runner to py.test (2.3.5). The tests for the lowest level package, web-framework, were discovered and run with no errors after creating a setup.cfg (to manage the py.test options) and a conftest.py (to ignore setup.py). When running py.test (with a setup.cfg and conftest.py) on any higher level package, which all require web-framework, I receive the following error:
ERROR: Could not import settings 'high_level_package.test_settings' (Is it on sys.path?): No module named web_framework.settings
I'm using a virtual environment, and web-framework is in the venv at the following location: ENV/lib/python2.7/site-packages/
I've tried with the venv built in the package's root directory and with it built outside the project path, to no avail. Also, I can import web_framework.settings from the Python interactive command line in the higher level package's root directory.
My current conftest.py is just the following line: collect_ignore = ["setup.py"]
I've tried adding the following lines above it:
import os
import sys
sys.path.append(os.path.dirname(__file__))
I also tried hardcoding in the path to the web-framework package in the above sys.path.append.
Neither worked.
In case it's relevant, my setup.cfg is:
[pytest]
python_files = *test*.py
norecursedirs = node_modules ENV
addopts = --junitxml=python_result.xml, --doctest-modules
Edit:
Forgot to mention the traceback relationship. higher_level_package.test_settings imports higher_level_package.settings, which itself imports web_framework.settings.
in order to have it work you either need to have a develop-install of the worktree, or add . to the PYTHONPATH env var
Change to the folder where your main python website module is located
django-admin runserver --settings=mysite.settings
where mysite was created by
django-admin startproject mysite

Can't sign in to Django admin

I can't sign in to Django admin with any of the superusers I've created. Tried creating new superusers, changing passwords, etc. - no error messages for any of these processes, but still can't sign in.
I'm not sure if it's related, but I also can't run django-admin.py commands. Here is what I've done so far:
$ django-admin.py validate
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
$ export DJANGO_SETTINGS_MODULE=mysite.settings
$ django-admin.py validate
Error: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings
sys.path shows ['/Users/joerobinson/Sites/django/mysite' ... (other stuff on path)]
Is there something else I need to do to register the mysite module?
python manage.py runserver works fine.
Conclusion
For the question about logging in to django admin, I did not have django.contrib.auth.backends.ModelBackend included in my AUTHENTICATION_BACKENDS - adding this allowed me to sign in to admin.
I'm still working on the django-admin.py configuration question (which appears to not be related), and will reopen it in a separate question.
In my case, I had "SESSION_COOKIE_SECURE = True" because I was trying SSL configurations. Just commenting this line worked.
Well, to answer one of your questions, the parent folder of mysite is what needs to be on the python path. Adding mysite itself to your python path will expose the contents of mysite, not the mysite module to python.
As for runserver working / passwords not working, I haven't a clue. Have you customized anything with authentication?
Can you start the shell and try authenticating with your superuser?
http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate
You said in a comment that "is_staff was set appropriately... same problem." -- does that mean you were able to log in with the first superuser to set is_staff??
I ran into a similair problem but, I couldn't login on the admin interface even after removing the database and creating it from scratch. The problem was caused by a real silly mistake.
Don't run python manage.py testserver by accident. Make sure you use python manage.py runserver.