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
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.
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.
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.
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.
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.
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
Open settings
Search for autopep8. You should see the following results:
Click on "Edit in settings.json" under the first option
Add the following argument to the User Settings JSON file:
"python.formatting.autopep8Args": ["--ignore", "E402"]
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With