Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow statements before imports with Visual Studio Code and autopep8

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
like image 756
waspinator Avatar asked Jan 03 '19 21:01

waspinator


People also ask

How do I use autopep8 in terminal?

If you're using eclipse + PyDev you can simply activate autopep8 from PyDev's settings: Windows -> Preferences -> type 'autopep8' in the search filter. Show activity on this post. There are many. IDEs usually have some formatting capability built in.

How do I add autopep8 to my path?

Go to Python extension's extension setting. 2. Find Python>Formatting:Autopep8 Path term 3. Add the full path of the autopep8 that you have installed.

How do I add autopep8 to a VSCode file?

This is a vscode-extension that applies autopep8 to your current file. Installation. Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter. Copy. Copied to clipboard. More Info. Overview Version History Q & A Rating & Review. Python-autopep8 Description.

Where can I find the settings Editor in Visual Studio Code?

See more in Key Bindings for Visual Studio Code. By default VS Code shows the Settings editor, you can find settings listed below in a search bar, but you can still edit the underlying settings.json file by using the Open Settings (JSON) command or by changing your default settings editor with the workbench.settings.editor setting.

How do I enable linting in VS Code?

Just select the python3/2 with virtualenv enabled. This will ensure that Vs code picks up tools we installed in virtual env. Next we finally activate linting on Vs code. Your workspace should match the above linting settings. After editing your json save the settings and start coding.

How do I change the default language in Visual Studio Code?

Change language mode. Keyboard Shortcut: ⌘K M (Windows, Linux Ctrl+K M) If you want to persist the new language mode for that file type, you can use the Configure File Association for command to associate the current file extension with an installed language. Customization. There are many things you can do to customize VS Code. Change your theme


2 Answers

  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.

like image 59
willk Avatar answered Oct 20 '22 08:10

willk


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
like image 27
leosh Avatar answered Oct 20 '22 09:10

leosh