Skip to content

Is there some way to not change the order of the import and the code before that import? #4

Open

Description

@MisLink

Sorry for my English. The code is more obvious.

In Django I need a line django.setup() before importing django model for some scripts beyond the django project:

import django
django.setup()

from apps.model import Blah

zimports will change it to

import django
from apps.model import Blah

django.setup()

What is the right way in this situation?

Activity

  1. zzzeek commented on Aug 25, 2019

    @zzzeek
    Contributor

    hi there -

    this is in fact a major shortcoming in the project right now, unfortunately, there should be another comment directive, perhaps something like #noqa nomove that prevents it from moving an import at all.

    for now, for the few cases I have this problem, I use this workaround:

    
    if True:
        import django
    django.setup()
    
    from apps.model import Blah
    

    or

    
    import django
    django.setup()
    
    if True:
        from apps.model import Blah
    
  2. zzzeek commented on Aug 25, 2019

    @zzzeek
    Contributor

    Basically it only moves top level imports so anything inside of "if True:" is not going to move and will be considered to be where the code starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions