Project tree:
|___sb_website
| |___article
| | |__...py
| |___home
| | |__...py
| |___sb_website
| | |__dev_settings.py
| | |__...py
| |__manage.py
| |__...py
My django project development settings are dev_settings.py. I want to run my tests against these settings.
When I use a Python configuration in PyCharm with these parameters test --settings=sb_website.dev_settings, script path ..\sb_website\manage.py I get the following Error:
ERROR: sb_website.article (unittest.loader._FailedTest) ImportError: Failed to import test module: sb_website.article.
Same for sb_website.home and sb_website.sb_website
If use a Django Test configuration in PyCharm modules are imported and tests run fine.
I'd like to learn the root cause of my problem as it is not the first time I faced this.
What I tried:
changed all imports to explicit imports, e.g. *.models to home.models
The issue was not related to PyCharm, the answer why there was a difference between the Python configuration and Django Test configuration was that the Python configuration pointed directly to the test target. Django Test configuration uses the manage.py test command and executing it tried to import tests and found a __init__.py in the root folder.
By removing the __ini__.py in my root folder of sb_website the modules were found correctly and tests ran through.
The answer was found here Running django tests fails
Related
I'm building a flask application, located in a subdirectory within my project called myapp. Running gunicorn --bind 0.0.0.0:$PORT myapp.app:app works fine, no errors, regardless of what FLASK_APP is set to. When I try to use Flask's CLI, however, it fails to find my app (reasonable), but when I set FLASK_APP to myapp.app., it appears to be doubling up the import path, resulting in an import error:
FLASK_APP=myapp.app flask run
* Serving Flask app 'myapp.app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Try 'flask run --help' for help.
Error: While importing 'myapp.myapp.app', an ImportError was raised.
How can I solve this? Is this a bug in Flask?
There was a __init__.py in my project directory that was causing this.
I dug into the source code of flask in cli.py and found the following code in prepare_import:
# move up until outside package structure (no __init__.py)
while True:
path, name = os.path.split(path)
module_name.append(name)
if not os.path.exists(os.path.join(path, "__init__.py")):
break
Because I had an __init__.py in my project directory (also called myapp), this made flask try to import myapp.app from the ancestor of my project, resulting in myapp.myapp.app.
When trying to run my tests using either ./manage.py test or pytest, all of the apps in my Django project fail their tests with ModuleNotFoundError: No module named 'project.appname'. However, when running with ./manage.py test app.tests (if the tests are in a dedicated tests directory), they do progress beyond a Module Not Found error.
I think there is something wrong with my configuration that leads to this error but I have no idea where to poke to try fixing this: I'm not seeing anything that makes PyCharm yell at me when I look at any testing module nor when looking at Settings.py
Remove the __init__.py file from the project.
e.g.
project/
- appname
...
...
__init__.py # Remove this one.
I can't get my django app to run on google app engine.
It deploys successfully but throws an error when I try connect to it in my browser.
main.py throws the following error: from wsgi import application as app ModuleNotFoundError: No module named 'wsgi'
I've looked at the question here:
ModuleNotFoundError - No module named 'main' when attempting to start service
I don't know what file structure is 'expected' for my main.py to run without errors.
This is the current structure
--static root file--main.py, app.yaml, etc.
|
voting------------
|
--voting-----------settings.py, wsgi.py, etc.
|
other stuff
my main.py currently reads (voting is the name of my project)
from voting.wsgi import application as app
I have tried voting.voting.wsgi and wsgi also. Please help
Fixed the issue by moving the static root.
Look at this for an example of how to set out the folder structure and where to place the static root: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard_python37/django
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
With Django 1.4.5, I'm using django-nose 1.1.0.
I have two settings files with no diff.
-> % diff local_settings.py test_settings/sqlite.py
I run the tests with:
-> % python manage.py test foo --settings=local_settings
and I get
Ran 91 tests in 5.273s
OK (SKIP=6)
Running the same identical settings from the different location
-> % python manage.py test foo --settings=test_settings.sqlite
The tests bail without all running:
Ran 43 tests in 1.230s
FAILED (errors=1)
I get a traceback, DatabaseError: no such table: django_content_type
The traceback comes through the loading of the urls. Something that gets instantiated there calls ContentType.objects.get_for_model(self.model). How is this difference possible when the settings are identical?
My manage.py file is generic:
#!/usr/bin/env python
from django.core import management
if __name__ == "__main__":
management.execute_from_command_line()
Is your database NAME set to a relative path for the sqlite DB?
If so, you may simply need to syncdb with your settings file in test_settings.
DatabaseError: no such table: django_content_type means one of the django specific tables isn't found, which sounds like an issue with the DB itself, not your app or the settings file itself.