I would set isort to ignore all the django migration files.
is there any way to do this?
Hi @luzfcb,
isort supports directory ignoring directories with the skip settings. From the command line you can do --skip migrations and it will skip and occurrence of a file within a folder named 'migrations' recursively.
Hope you find this helpful.
Thanks!
~Timothy
Is there also a way to do this in the settings file? I am trying to configure a pre-commit hook and a command line argument doesn't really solve this.
HI @Micromegass,
Good question! Almost without exception settings that can be set via CLI can be set in a config file and vice versa. The mapping of the options is on the online documentation, here is the one for skip: https://timothycrosley.github.io/isort/docs/configuration/options/#skip and here is the documentation for using config files: https://timothycrosley.github.io/isort/docs/configuration/config_files/.
So an example .isort.cfg config file to skip migrations would look like:
[settings]
skip=migrations
Thank you for your quick reply and help.
I already tried various options with the documentation but isort keeps ignoring my settings and keeps formatting the migrations. That's why I came here.
I am running a django project and at the root-folder I have a setup.cfg in which I tried the settings. I also created an isort.cfg with the following settings, but it still keeps formatting the migrations:
[settings]
known_third_party = allauth,crispy_forms,django,environ,factory,geopy,leaflet,numpy,pandas,pytest,requests,rest_framework,rest_framework_jwt,selenium,sphinx_theme,storages
line_length = 89
multi_line_output = 3
include_trailing_comma = True
skip=migrations
I also tried this with skip:migrations/** and with skip_glob but it still formats my migrations.... Is there something I am doing wrong? Or an alternative way to achieve what I am trying?
Hi @Micromegass,
I'm happy to do anything I can to help you resolve this issue! Your config looks correct to me.
Some follow up questions:
isort . --show-config? Does it pick up your config settings / file. It's important to note that config files don't merge, and isort will only pick up one. So if it finds a different config file first it may never make its way to the intended config. If possible could you paste the output here?isort . do the right thing? In which case, I can work to fix the precomit command.Thanks!
~Timothy
@Micromegass
Just striked me what the issue may likely be! skip doesn't take effect by default for files you explicitly pass in, and precommit directly passes in files. If you want isort to skip these files, simply set filter_files to True within your config.
Thanks!
~Timothy
@Micromegass one final update, if your issue is related to precommit, I've updated isorts official precommit configuration to filter files by default, and I've released a new version that improves this behaviour (5.2.1)
So first of all thank you so much for this great help and support, I really do appreciate it.
According to your suggestions, I tried the following:
location of config: It is in the root of my django project where I have manage.py. Both configs (isort.cfg and setup.cfg) are located there, so yes maybe this is a problem. But I also tried moving everything into a single config (setup.cfg) as described in the doc and it ignored the skip-directive. UPDATE: I removed the setup.cfg but still nothing changes
When I run isort . --show-config in my root-dir I get isort: error: unrecognized arguments: --show-config and when I run isort .I get Unable to parse file . Is a direcotry. If I use isort and specifiying a path to a file afterwards it works perfectly fine. Also when I add show-config after a specified path I get isort: error: unrecognized arguments: --show-config. So in short, the recursive option does not work for me.
Then I tried the most recent version you posted (thank you again). Now I can't set the filter anymore, so I removed it from the settings. Something odd happens now: Before isort used to remove unneccessary lines from the bottom of the migrations-file. This doesn't happen anymore now. But when I mix up the imports it still sorts them in the migrations file.
I will try more things, but this is as far as I've gotten right now. Thanks again for all your effort and I hope I was clear enough in my explanations.
Hi @Micromegass,
It looks like your stuff on the old 4.x.x version somehow! Can you confirm what isort --version gives you?
Happy to work with you till we get your issue resolved :)
~Timothy
Thank you :). And you are right, the versions are off... I have isort installed in a virtual environment and isort --version gives me version 5.0.7. Even though I specified version 5.2.1 in my pre-commit-config.yml...
So I removed isort from my virtual env. And then I cleaned the cache of pre-commit.... But still the same problem
And if it helps here is my pre-commit config-file:
repos:
- repo: https://github.com/asottile/seed-isort-config
rev: v1.9.3
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.2.1
hooks:
- id: isort
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.8
exclude: |
(?x)(
migrations/|
config/|
_build/|
buck-out/|
build/|
dist/
)
So I assume that this takes the correct version (5.2.1) without me even having to install isort locally... I had it installed because I was using it manually before
I think we're close! I'd recommend that you do the following:
.isort.cfg)skip=migrations is in that config.- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.2.1
hooks:
- id: isort
with
- repo: https://github.com/timothycrosley/isort
rev: 5.2.1
hooks:
- id: isort
seed-isort-config it should no longer be necessary on the 5.x.x release and could lead to some weird inconsistencies.skip_glob=*/migrations/* and filter_files=true to the configAnother thing to keep in mind @Micromegass, after changing .isort.cfg you have to check it in for precommit to pick it up.
@timothycrosley you my friend, are a hero.
I worked by removing seed-isort-config and adding https://github.com/timothycrosley/isort as the repo. The rest was no longer needed. Thanks so much, I am very happy that I can use this great project of yours now in my projects. Also stumbled across your documentation project which I will soon check out more :)
So thank you a bunch for the fantastic and continuous help and support! Really appreciated
Most helpful comment
Hi @luzfcb,
isort supports directory ignoring directories with the skip settings. From the command line you can do --skip migrations and it will skip and occurrence of a file within a folder named 'migrations' recursively.
Hope you find this helpful.
Thanks!
~Timothy