Pycharm - no django tests found but run in console - python

I followed this tutorial https://docs.djangoproject.com/en/3.0/intro/tutorial05/
The tests can be run by the command (polls is the application name)
python manage.py test polls
However under PyCharm IDEA, when I click on the green arrow (line 21-22).
The message is "No tests were found"
The stack trace is gibberish to me
Traceback (most recent call last): File "/Applications/PyCharm
CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py",
line 35, in
sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not
JB_DISABLE_BUFFERING)) File
"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py",
line 100, in init
self.parseArgs(argv) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py",
line 147, in parseArgs
self.createTests() File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/main.py",
line 159, in createTests
self.module) File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py",
line 220, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names] File
"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py",
line 220, in
suites = [self.loadTestsFromName(name, module) for name in names] File
"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py",
line 154, in loadTestsFromName
module = import(module_name) File "/Users/raychenon/Projects/python/django/mysite/polls/test_views.py",
line 8, in
from .models import Question File "/Users/raychenon/Projects/python/django/mysite/polls/models.py", line
8, in
class Question(models.Model): File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py",
line 107, in new
app_config = apps.get_containing_app_config(module) File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line
252, in get_containing_app_config
self.check_apps_ready() File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line
134, in check_apps_ready
settings.INSTALLED_APPS File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line
76, in getattr
self._setup(name) File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line
61, in _setup
% (desc, ENVIRONMENT_VARIABLE)) 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.
Process finished with exit code 1
Empty suite
PyCharm IDE setup
My python interpreter is set correctly
Research
I already looked at Pycharm - no tests were found? . Unrelated error, my test functions names start by test_*

Based on your comment saying your django dependencies cannot be found, I suspect you didn't setup the python interpreter properly for your project.
Eg. if you use virtualenv, you need to setup your python interpreter to point to the python bin inside your virtualenv, so Pycharm can find your packages.
Eg. photo below shows the python interpreter for my "pmas" virtualenv.

The reason we use manage.py when working with Django is that it manages environment for us.
But working with pycharm, unless you specify things manually, a python file that has test footprints in it (importing unittest and having method names startng with "test_") are just modules we can test without any external dependencies.
in cases you use a framework such as Django, then you need to play by their rules. you will either use their tools or set whatever is needed manually.
I don't know if pycharm had this support at the time but this pycharm/django-support may help.
if that does not cover your needs, then you will need to work on a few things mentioned in Django Settings (check for other versions if you use an older one, or newer if you are from the future). your error message already mentions about this.
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.
you can either open project settings in pycharm and add this key-value in environment variables for it:
DJANGO_SETTINGS_MODULE=mysite.settings
or use this in your code wherever it is needed. (check the settings page for details)
from django.conf import settings
settings.configure(DEBUG=True)

can you try
export DJANGO_SETTINGS_MODULE=mysites.settings
here, mysites is name of Django project where settings.py is available
paste the above line in bash shell before running standalone python script

Related

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. django 1.8

I have a problem with django 1.8. The console shows:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
commands = get_commands()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
There are answers elsewhere on Stackoverflow about how this could be actually about app registry, but I believe it is more often just an indicator that something is wrong in the settings and Django didn't handle failure well.
I just had the exact same error by just having a bad logger filename in LOGGING settings -- the directory I was referring to did not exist, so logger initialisation failed.
It looks like you are using a Virtual Environment.
Perhaps you have an app included under INSTALLED_APPS in settings.py, but it isn't installed (e.g. via pip install django-multisite) in your virtual environment.
(I received this same error after starting to use a Virtual Environment.)
I actually had the same error today, which is strange as everything was working properly yesterday and I do not think I have changed anything.
I got the error when trying to start up the dev server. I had recently upgraded this project to 1.8.
The solution for me was: I changed my PYTHONPATH on the startup script (to make sure it also points to the 1.8 version) of the dev server and everything seemed to work perfectly again.

Django Tutorial Error: Setting up the test client

I'm on part 5 of the Django Tutorial and I've hit a snag. I'm trying to setup the test client with the following commands in the python shell:
from django.test.utils import setup_test_environment
setup_test_environment()
I get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mohitgupta/.virtualenvs/Django/lib/python2.7/site-packages/django/test/utils.py", line 104, in setup_test_environment
mail._original_email_backend = settings.EMAIL_BACKEND
File "/Users/mohitgupta/.virtualenvs/Django/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
self._setup(name)
File "/Users/mohitgupta/.virtualenvs/Django/lib/python2.7/site-packages/django/conf/__init__.py", line 40, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I've looked into defining the DJANGO_SETTINGS_MODULE with no real progress. Just a note that I'm using PyCharm as an IDE but I don't use the internal terminal they provide.
Any suggestions?
As mentioned in previous sections of the tutorial, to start a shell in Django you should always do ./manage.py shell, rather than starting Python directly. That sets up the environment for you.
I had a similar problem and it worked after I gave the command in the shell
import django
django.setup()

"Can't extract file(s) to egg cache" error in GAE

I have Windows 7 and Python 2.7 with setuptools installed.
After i download pitz module (easy_install pitz), Google App Engine stop working:
bad runtime process port ['']
Traceback (most recent call last):
File "G:\Program Files (x86)\Google\google_appengine\_python_runtime.py", line 184, in <module>
_run_file(__file__, globals())
File "G:\Program Files (x86)\Google\google_appengine\_python_runtime.py", line 180, in _run_file
execfile(script_path, globals_)
File "G:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\python\runtime.py", line 28, in <module>
from google.appengine.ext.remote_api import remote_api_stub
File "G:\Program Files (x86)\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 75, in <module>
import yaml
File "G:\Program Files (x86)\Google\google_appengine\lib\yaml-3.10\yaml\__init__.py", line 14, in <module>
from cyaml import *
File "G:\Program Files (x86)\Google\google_appengine\lib\yaml-3.10\yaml\cyaml.py", line 5, in <module>
from _yaml import CParser, CEmitter
File "C:\Python27\lib\site-packages\pyyaml-3.11-py2.7-win-amd64.egg\_yaml.py", line 7, in <module>
File "C:\Python27\lib\site-packages\pyyaml-3.11-py2.7-win-amd64.egg\_yaml.py", line 4, in __bootstrap__
File "C:\Python27\lib\site-packages\pkg_resources.py", line 950, in resource_filename
self, resource_name
File "C:\Python27\lib\site-packages\pkg_resources.py", line 1607, in get_resource_filename
self._extract_resource(manager, self._eager_to_zip(name))
File "C:\Python27\lib\site-packages\pkg_resources.py", line 1667, in _extract_resource
manager.extraction_error()
File "C:\Python27\lib\site-packages\pkg_resources.py", line 996, in extraction_error
raise err
pkg_resources.ExtractionError: Can't extract file(s) to egg cache
The following error occurred while trying to extract file(s) to the Python egg
cache:
[Error 5] : 'C:\\Users\\Kostr\\AppData\\Roaming\\Python-Eggs\\pyyaml-3.11-py2.7-win-amd64.egg-tmp\\_yaml.pyd'
The Python egg cache directory is currently set to:
C:\Users\Kostr\AppData\Roaming\Python-Eggs
Perhaps your account does not have write access to this directory? You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.
How to solve this issue?
from: https://code.google.com/p/modwsgi/wiki/ApplicationIssues
To avoid this particular problem you can set the PYTHON_EGG_CACHE cache environment variable at the start of the WSGI application script file. The environment variable should be set to a directory which is owned and/or writable by the user that Apache runs as.
import os
os.environ['PYTHON_EGG_CACHE'] = '/usr/local/pylons/python-eggs'
Again, make sure this exists. For Windows users, maybe something like:
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
Alternatively, if using mod_wsgi 2.0, one could also use the WSGIPythonEggs directive for applications running in embedded mode, or the python-eggs option to the WSGIDaemonProcess directive when using daemon mode.
Note that you should refrain from ever using directories or files which have been made writable to anyone as this could compromise security. Also be aware that if hosting multiple applications under the same web server, they will all run as the same user and so it will be possible for each to both see and modify each others files. If this is an issue, you should host the applications on different web servers running as different users or on different systems. Alternatively, any data required or updated by the application should be hosted in a database with separate accounts for each application.
Sounds like you don't have permissions to write to the directories. Uninstall pitz, and reinstall using the -Z tag to install it as the unzipped version. Then, you don't need the cache directory to extract each time. That should solve the directories / permissions issue.

django could not import settings

I once wrote an django app, named superlists, and settings was in superlists.settings. It was for tutorial, and I was changing differnt settings, little realizing what I was doing.
And now when i starting new django project - it tells me it could not import superlists.settings
I reinstalled Pycharm, django, python (both versions 2.7 and 3.3), deleted all virtual envs, deleted everything that might be connected to it several times... IT IS STILL THERE! on a new, clean version of python, which even dont have django - there is DJANGO_SETTINGS_MODULE in sys.path pointing to superlists.settings. Each new project on a clean, with default settings, version of pycharm, tells me that it cannot import superlists.settings.
I delete DJANGO_SETTINGS_MODULE pointing to that settings from sys.path from cmd - exit then watch again - its there.
Where it takes it? I got no idea.
here is traceback:
Traceback (most recent call last):
File "D:/Python27/Lib/site-packages/django/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 261, in fetch_command
commands = get_commands()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'superlists\settings.py' (Is it on sys.path? Is there an import error in the settings file?): Import by filename is not supported.
It turned out that I created new environment system variable DJANGO_SETTINGS_MODULE. So evident, but it almost cost me sanity.
Change the environment variable from
DJANGO_SETTINGS_MODULE=superlist.superlist.settings
To DJANGO_SETTINGS_MODULE=superlist.settings
or use an __init__.py in superlist

Upgrading a Python Project from version 2.7 to 2.7.4

I have a Django "project" that I have inherited, which I am developing in Eclipse. On my OS (windows 7 32 bit), I have Python 2.7.4 installed, likewise for my virtualenv. However, on my project (extracted from SVN) the Python version is 2.7 only.
This causes a conflict when trying to create another superuser (I do not know the original superuser name/password) where I get the message:
cannot import maxrepeat
How do I upgrade the python version located at:
c:\users\"username"\workspace\"project"\scripts
from 2.7 to 2.7.4?
Apologies if I have omitted some important details, or if I am asking the wrong question as I am newbie to Django/python development.
EDIT
Having spoken to a friend before referring back to these responses (thanks btw), he advised me to copy over the contents of the 'scripts' folder within my virtualenv to the folder:
c:\users\"username"\workspace\"project"\scripts
I did that, so in theory, they are both now running from python version 2.7.4.
However, when I run the script
python manage.py createsuperuser
I get the following FULL Traceback:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 453, in execute_from_command_line
utility.execute()
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 263, in fetch_command
app_name = get_commands()[subcommand]
File "C:\users\alecc\workspace\hub\lib\site-packages\django\core\management\__init__.py"
, line 109, in get_commands
apps = settings.INSTALLED_APPS
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 53,
in __getattr__
self._setup(name)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 48,
in _setup
self._wrapped = Settings(settings_module)
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 134,
in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SE
TTINGS_MODULE, e))
ImportError: Could not import settings 'hub.settings' (Is it on sys.path?): No module name
d hub.settings
I have checked the system variables and I'm sure my project is on the Python system path. I have also checked 'django.contrib.auth' is enabled in my INSTALLED_APPS in the settings.pyfile.
Edit 2
Many other posts suggest it's a cross over of Python versions. However when I check the version number using the command:
$scripts\python.exe --version
I get Python 2.7.4 for each installation (Project & virtualenv)
Based on this information:
File "C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py", line 134,
in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SE
TTINGS_MODULE, e))
ImportError: Could not import settings 'hub.settings' (Is it on sys.path?): No module name
d hub.settings
It looks like your application is called hub? The problem here is that it's trying to import hub.settings - but it can't find it. So for some reason, your settings.py is not on the path.
You can check this by editing C:\users\alecc\workspace\hub\lib\site-packages\django\conf\__init__.py, and somewhere before line 134 you can put import sys; print(sys.path). Then check to see that the path where your settings.py file lives is available. If it is, something else weird is happening.
If not, just go ahead and pull those lines from the __init__, and try running manage from the same directory your settings file is in.

Categories