Allow statements before imports with Visual Studio Code and autopep8
Asked Answered
P

3

40

I'm using Visual Studio Code with the Python plugin and autopep8 with:

"editor.formatOnSave": true

I have local packages I need to import, so I have something like:

import sys
sys.path.insert(0, '/path/to/packages')
import localpackage

but when I save, Visual Studio Code/autopep8 moves all import statements before the code, so Python can't find my local package.

import sys
import localpackage
sys.path.insert(0, '/path/to/packages')

How can I tell Visual Studio Code/autopep8 that it's okay to put a statement before imports, or is there a more correct way of importing local packages?

As a workaround, it looks like it's fine if you import in an if statement:

import sys

sys.path.insert(0, '/path/to/packages')
if 'localpackage' not in sys.modules:
    import localpackage
Petuntse answered 3/1, 2019 at 21:53 Comment(2)
I don't know what the flag is to autopep8, but once you find it you can specify it with "python.formatting.autopep8Args".Terrorism
The entry in settings.json should be "python.formatting.autopep8Args": ["--ignore", "E402"] (see complete answer below).Carpus
C
55
  1. Open settings

  2. Search for autopep8. You should see the following results:

    Enter image description here

  3. Click on "Edit in settings.json" under the first option

  4. Add the following argument to the User Settings JSON file:

    "python.formatting.autopep8Args": ["--ignore", "E402"]
    

    Enter image description here

This tells autopep8 to ignore error 402 which is: "module level import not at top of file" (here's the list of errors in pep8)

You can use this same method to change any of the autopep8 settings. For example, if you only wanted to fix indentation, you can use "python.formatting.autopep8Args": ["--select", "E1"]

The autopep8 readme has more information on the available options.

Carpus answered 3/1, 2019 at 23:4 Comment(4)
As a side note, I've only gotten one argument at a time to work. If I try to do something like: "python.formatting.autopep8Args": ["--ignore", "E402", "--ignore", "E401"] only one of the two arguments work (usually the second one). I'm not sure why this is, and if someone has a solution, it would be greatly appreciated!Carpus
@wilk you need to separate them with commas like "--ignore", "E401,E701"Reichsmark
Is there something I can put in the setup.cfg?Eddieeddina
now in vscode, you need to search for autopep8, click "add item", add --ignore=E402 and click "OK".Devisable
S
44

If you don't want to generally disable import sorting, but just disable it for specific lines, you can use the following pragmas at the end of each line:

# noqa

or

# nopep8

Like so for your example:

import sys # noqa
sys.path.insert(0, '/path/to/packages') # noqa
import localpackage
Scarlettscarp answered 17/7, 2019 at 1:56 Comment(2)
Love this approach!Dictograph
You can also use a more specific line like this # noqa: E402Buffy
T
3

Due to changes in vscode (~1.84) the syntax in willk's answer "python.formatting.autopep8Args": ["--ignore", "E402"] no longer works, the new format is:

"autopep8.args": ["--ignore", "E402"]

docs

Translocation answered 23/11, 2023 at 2:23 Comment(1)
Lifesaver, I was curious of why that argument grayed out when I am coding recently and constantly adding the # noqa: E402 comments. This worked for me.Byssinosis

© 2022 - 2024 — McMap. All rights reserved.