ModuleNotFoundError: No module named 'project.appname' when running Django tests - django

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.

Related

While importing 'myapp.app' an import error was raised

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.

Can't runserver, ModuleNotFoundError[Django]

I'm trying to work with django, but I can't runserver it shows an error like this
"ModuleNotFoundError: No module named 'music.apps. MusicConfig'; 'music.apps' is not a package"
Note: I'm using python3.
Probably your music/apps directory doesn't contain __init__.py file. Create and empty one and apps will become a package

Django 2.0 - Make test worker to run tests in non-app folder

In Django 2.0, I have following project structure, which I can't change, no matter what:
grocery_store_website
manage.py
grocery_store # contains wsgi, settings,etc.
app1
app1
non-app-utils
__init__.py
helpers.py
serializers.py
model_mixins.py
tests
test_helpers.py # I want test runner to run these.
It turned out, I need to write unit tests for non-app-utils. Mentioned directory is not a registered Django App and never will be. These tests must be located in tests directory, located in non-app-utils. How can I make Django's test runner to discover and run also tests from non-app-utils directory?
If I run Django tests with directly specified path ./manage.py test utils.tests.test_helpers, it works. However ./manage.py test does not. Any ideas how to go on?
Jerin Peter George suggested adding __init__.py file into non-app-utils directory. However, problem was, there is __init__.py file missing in non-app-utils/tests directory!
After I add those and run ./manage.py test, Django's test runner found my tests and ran them as I needed to!

Django test not running specific app tests

I have an app named "sites" that runs normally through the built in server and I can interact with it without any issues.
I added tests in the standard tests.py file and ran "manage.py test sites"
I have two tests in my tests.py file all starting with "test".
When I run the manage.py command in verbose mode, I get:
test_get_current_site (django-contrib.sites.tests.SitesFrameworkTests) ... ok
test_save_another (django-contrib.sites.tests.SitesFrameworkTests) ... ok
etc.
It looks to me that I have a name conflict with an internal module.
Is there any way I can get manage.py to test my code or should I just bite the bullet and change my app name?
Rename your app. From the docs on the INSTALLED_APPS setting:
App names must be unique
The application names (that is, the final dotted part of the path to the module containing models.py) defined in INSTALLED_APPS must be unique. For example, you can’t include both django.contrib.auth and myproject.auth in INSTALLED_APPS.
In this case, your sites app clashes with the Django sites framework, django.contrib.sites.

what magic is "django-admin startapp" doing, that the test runner needs to find my tests.py

I was having a problem that the django test runner wasn't finding the tests for my app, like this one:
Django test runner not finding tests
One of the comments on that thread suggested creating a new app with django-admin.py and seeing if the tests ran there. e.g.
django-admin.py startapp delme
then
adding "delme" to my INSTALLED_APPS
then
copying my tests.py from the app where it wasn't getting found into delme/
and viola! the tests did run. So, OK, I have a work arround, but I don't understand. I re-read what I think should be the relevant parts of the django documentation but the penny refuses to drop.
BTW, the app works via runserver and wsgi, so there doesn't appear to be any gross configuration problem. And my tests all pass from their new home, so I obviously need more tests :)
Specifically, I'm running django in a virtualenv, so I had run "django-admin.py startapp" in the (activated) virtualenv where I wanted the tests to run. This doesn't make the tests run in my other virtualenvs, I still have the old symptoms there (Ran 0 tests). I have a multitude of virtualenvs, managed by non-trivial paver scripts. One uses "path.copytree" for deploying projects, rewrites apache config files, restarts apache, writes wsgi files using the appropriate virtualenv, etc. The other uses PIP/GCC/aptitude/etc for bootstrapping/tearing down the different environments, updating packages as per configuration, etc. So I want to understand the difference between django-startapp and simply copying files, so I can fix these paver scripts so the tests can run in any environment I want them to.
The only thing that makes sense to me, after reading your description, is the location of paths for your existing apps. Can you confirm the following things:
Your app is at the same folder level as the delme app
Your app folder contains an __init__.py file
Your app is listed in the INSTALLED_APPS setting
I'm going to guess that it's a missing __init__.py file, as that trips some people up. To answer your specific question, django-admin startapp doesn't do anything magical. It just creates the right folders and files in the correct place.
Your folder structure should be...
my_project/
__init__.py
manage.py
settings.py
my_app/
__init__.py
tests.py
models.py
delme/
__init__.py
tests.py
models.py
Also note this comment
You can't easily name the TestCase class directly. You name the app, the Django runner does it's discovery thing by looking in models.py and tests.py
solved (still with a little whiff of magic).
diff showed that my old app didn't have a models.py but the new app ("delme", working) did. I didn't think the old app needed one, it was importing all it's domain classes from other places.
Touching an empty models.py in my old app fixed it, now the test runner finds the tests.py and everything works as expected. Condlusion - if an app has no models.py, the django test runner won't find the app's tests.py.
What I said about not working in different virtualenvs was bogus (red herring), I was a bit confused about what my deploy scripts were doing.