Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent PyDev's autopep8 import formatter from moving site.addsitedir() calls?

The Eclipse PyDev plugin includes fantastic integrated autopep8 support. It formats the code to PEP8 style automatically on save, with several knobs and options to tailor it to your needs.

But the autopep8 import formatter breaks site.addsitedir() usage.

import site

site.addsitedir('/opt/path/lib/python')

# 'ourlib' is a package in '/opt/path/lib/python', which 
# without the above addsitedir() would otherwise not import.
from ourlib import do_stuff

And after PyDev's autopep8 import formatter, it changes it to:

import site

from ourlib import do_stuff

site.addsitedir('/opt/path/lib/python')

Which breaks from ourlib import do_stuff with ImportError: No module named ourlib.

Question:

Is there a PyDev setting or autopep8 command-line option to keep it from moving site.addsitedir() calls?

like image 542
CivFan Avatar asked Feb 07 '15 00:02

CivFan


2 Answers

Oldie but still relevant as I found this one issue.

I'm using VSCode and autopep8. You can disable formatting by adding # nopep8 to the relevant lines.

ps. Checked the docs for a link but could not find it :(

like image 141
Whyhankee Avatar answered Nov 01 '22 14:11

Whyhankee


The best option I can find is to turn off import sorts in PyDev. This is not a complete solution, but it's better than completely turning off autopep8 code formatting.

Just uncheck the Sort imports on save? option in the Eclipse/PyDev Preferences.

For Eclipse Kepler, Service Release 2, with PyDev 3.9.2, you can find it here:

Windows -> Preferences 
--> PyDev -> Editor -> Save Actions
----> "Sort imports on save?" (uncheck)
like image 28
CivFan Avatar answered Nov 01 '22 13:11

CivFan