fisadev / vim-isort

Vim plugin to sort python imports using https://github.com/timothycrosley/isort
MIT License
219 stars 32 forks source link

Support to use isort from a virtualenv #12

Closed seocam closed 7 years ago

seocam commented 7 years ago

Personally I dislike using python libraries installed in my system using pip. So what I usually do is to use a default virtualenv for development. I do have isort installed in my virtualenv and that virtualenv is open when I open vim but vim-isort can't find it.

Is there a way to set in the vimrc what's the python_path my vim should use? I also think that make vim use the active python version in the terminal would be even better.

Thanks!

fisadev commented 7 years ago

Hi! This is not vim-isort specific, but related to the way vim supports plugins written in python. Vim doesn't use a python interpreter from your system to run the plugins. It has an internal python interpreter, compiled at the same time vim is compiled, and bundled as part of itself. And as far as I know, when you run vim from inside a virtualenv, it should be able to import the python packages inside that virtualenv, as long as both the virtualenv's python and vim's python are the same major version (either 2 or 3). Is that true for your scenario?

seocam commented 7 years ago

I've tried in two different virtualenvs (one running 3.5 and other running 2.7) both had isort installed and both gave me the same output: No isort python module detected, you should install it. More info at https://github.com/fisadev/vim-isort

fisadev commented 7 years ago

Lets check if vim's python interpreter is able to import the module, regardless of the plugin. Try this:

:py import isort
:py3 import isort

Run both commands inside vim, running it in both virtualenvs, and tell me if they fail or not.

fisadev commented 7 years ago

Also: which OS and vim version are you using?

seocam commented 7 years ago

I'm on OS X 10.11.6 using Vim 7.4.1817.

Venv 3.5:

:py import isort
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named isort

:py3 import isort
E319: Sorry, the command is not available in this version

Venv 2.7

:py import isort
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/seocam/.virtualenvs/crave-deploy/lib/python2.7/site-packages/isort/__init_
_.py", line 25, in <module>
    from . import settings
ImportError: cannot import name settings

:py3 import isort
E319: Sorry, the command is not available in this version
fisadev commented 7 years ago

Ok! that clears things a lot, and gives me confidence over what I said before. First: your vim was compiled with python 2 support. Second: there is something wrong with isort.

Try this under venv 2.7, outside of vim: open a python terminal, and run import isort. Does it break the same way that it breaks inside vim?

seocam commented 7 years ago

Running isort in the terminal or inside the python interpreter it works fine.

Python 2.7.11 (default, Dec 26 2015, 17:47:15)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import isort
>>>
$ isort -c test.py ; echo $?
0
fisadev commented 7 years ago

Strange, so isort is importable outside vim, but not inside it. Run this two commands inside vim, and tell me the output of the second:

:py import sys
:py print sys.version

Sorry for the delay, had some very complicated days :)

seocam commented 7 years ago

Hey @fisadev. No problem at all. Thanks for helping me debug it.

inside vim:

2.7.11 (default, Dec 26 2015, 17:47:15)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]

from python interpreter:

2.7.11 (default, Dec 26 2015, 17:47:15)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]

It's exactly the same... :(

fisadev commented 7 years ago

Tried lots of things, but I wasn't able to reproduce it under my environment (ubuntu 16.04). It seems that something works different in the mac version of vim, regarding python paths and virtualenvs. The only thing left I could suggest you to try is this plugin, that my be able to fix the issue: https://github.com/jmcantrell/vim-virtualenv

seocam commented 7 years ago

Hello @fisadev! I've tried this module using my vim and it didn't work. Then I've recompiled my vim with python 3 support (using homebrew: brew install vim --with-python3) and now it works. If if remove my vim-virtualenv it doesn't work but using it everything works fine!

Thanks! ;)

fisadev commented 7 years ago

Nice! Glad you got it working! :)

seocam commented 7 years ago

One last question: isn't it possible to get vim-isort to work without python support by calling the command line script isort?

fisadev commented 7 years ago

I think it should be possible, I didn't think in doing that before because I didn't anticipate this kind of problems.

seocam commented 7 years ago

I think vim-flake8[1] works using cli since I never had issues using it.

[1] https://github.com/nvie/vim-flake8