1

I currently have a python file that looks like this:

import os
import sys

PROJECT_FOLDER = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
os.chdir(PROJECT_FOLDER)
sys.path.insert(0, PROJECT_FOLDER)
from settings import datagenerator as settings

Due to the way the settings and modules are organised in the project, I need to set some common working directory before importing the various files.

Since I am quite lazy, I like to use the 'Organize imports' in VScode. Unfortunately, it moves the line from settings import datagenerator as settingson top of the block setting the current path.

Even though it is to be expected. I was wonderings if there was a way to tell VSCode to keep those lines together.

E. Jaep
  • 2,095
  • 1
  • 30
  • 56

2 Answers2

0

The extension uses isort to manage import resorting. So if isort provides a mechanism for marking an import as something to not move then the extension will also support it. Otherwise I'm afraid there isn't anything that can be done.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
0

As in this answer, explained, you can avoid sorting like this:

app = Flask(__name__)
from . import views  # isort:skip
i.e. Add the # isort:skip comment to the import you don't want to jump to the top of the file.

Source: https://github.com/timothycrosley/isort#skip-processing-of-imports-outside-of-configuration

Alvaro
  • 1,430
  • 2
  • 23
  • 41