1

Imports like this:

from .file import *

are "anti-patterns". How can I import one python file into another, without breaking flake8 F430 rule? I have settings.py in the Django project that I want to "overwrite" in test_settings.py like this,

from settings import *

# make tests faster
DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}

to test with in-memory database, and I cant figure out how to do it without breaking the rule.

4
  • 1
    Simply don't use from ... import *. Import what you need: from settings import DATABASES.
    – Martijn Pieters
    Nov 25, 2020 at 19:46
  • well you're modifying the dictionary from settings so you're actually breaking the base module too! Nov 25, 2020 at 19:46
  • from something import * is great for interactive use cases like a Jupyter notebook. Don't use it in general production code and you should be good.
    – Martijn Pieters
    Nov 25, 2020 at 19:47
  • 1
    importing only DATABASES will not work. I have to import everything that is in the settings.py I would have to import each variable in the settings.py one by one.
    – Najiva
    Nov 26, 2020 at 7:58

1 Answer 1

0

According to authors of the book "Two Scoops of Django 3.x" this is actually an exception when they advocate using

import *

the singular case of Django setting modules we want to override all the namespace

in order to allow usage of multiple settings files. Therefore in this situation, the best solution is to exclude test_settings.py from the flake8 scan, in the flake8 configuration file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.