使用特定版本的Python编译vim

时间:2021-12-21 17:28:38

I'm working on several Python projects who run on various versions of Python. I'm hoping to set up my vim environment to use ropevim, pyflakes, and pylint but I've run into some issues caused by using a single vim (compiled for a specific version of Python which doesn't match the project's Python version).

我正在研究几个在不同版本的Python上运行的Python项目。我希望设置我的vim环境以使用ropevim,pyflakes和pylint,但是我遇到了一些问题,这些问题是由于使用单个vim(为特定版本的Python编译而与项目的Python版本不匹配) 。

I'm hoping to build vim into each of my virtualenv directories but I've run into an issue and I can't get it to work. When I try to build vim from source, despite specifying the Python config folder in my virtualenv, the system-wide Python interpreter is always used.

我希望将vim构建到我的每个virtualenv目录中,但我遇到了一个问题,我无法让它工作。当我尝试从源代码构建vim时,尽管在virtualenv中指定了Python配置文件夹,但始终使用系统范围的Python解释器。

Currently, I have Python 2.6.2 and Python 2.7.1 installed with several virtualenvs created from each version. I'm using Ubuntu 10.04 where the system-default Python is 2.6.5. Every time I compile vim and call :python import sys; print(sys.version) it returns Python 2.6.5.

目前,我安装了Python 2.6.2和Python 2.7.1,并且每个版本都创建了几个virtualenv。我正在使用Ubuntu 10.04,其中系统默认的Python是2.6.5。每次我编译vim并调用:python import sys; print(sys.version)它返回Python 2.6.5。

configure --prefix=/virtualenv/project --enable-pythoninterp=yes --with-python-config-dir=/virtualenv/project/lib/python2.6/config

configure --prefix = / virtualenv / project --enable-pythoninterp = yes --with-python-config-dir = / virtualenv / project / lib / python2.6 / config

Results in the following in config.log:

config.log中的结果如下:

...
configure:5151: checking --enable-pythoninterp argument
configure:5160: result: yes
configure:5165: checking for python
configure:5195: result: /usr/bin/python
...

It should be /virtualenv/project/bin/python. Is there any way to specify the Python interpreter for vim to use?

它应该是/ virtualenv / project / bin / python。有没有办法为vim指定Python解释器?

NOTE: I can confirm that /virtualenv/project/bin appears at the front of PATH environment variable.

注意:我可以确认/ virtualenv / project / bin出现在PATH环境变量的前面。

5 个解决方案

#1


13  

I'd recommend building vim against the 2 interpreters, then invoking it using the shell script I provided below to point it to a particular virtualenv.

我建议对2个解释器构建vim,然后使用我在下面提供的shell脚本调用它来指向特定的virtualenv。

I was able to build vim against Python 2.7 using the following command (2.7 is installed under $HOME/root):

我能够使用以下命令在Python 2.7上构建vim(2.7安装在$ HOME / root下):

% LD_LIBRARY_PATH=$HOME/root/lib PATH=$HOME/root/bin:$PATH \
    ./configure --enable-pythoninterp \ 
    --with-python-config-dir=$HOME/root/lib/python2.7/config \
    --prefix=$HOME/vim27
% make install
% $HOME/bin/vim27

:python import sys; print sys.path[:2]
['/home/pat/root/lib/python27.zip', '/home/pat/root/lib/python2.7']

Your virtualenv is actually a thin wrapper around the Python interpreter it was created with -- $HOME/foobar/lib/python2.6/config is a symlink to /usr/lib/python2.6/config.

你的virtualenv实际上是一个围绕它创建的Python解释器的瘦包装 - $ HOME / foobar / lib / python2.6 / config是/usr/lib/python2.6/config的符号链接。

So if you created it with the system interpreter, VIM will probe for this and ultimately link against the real interpreter, using the system sys.path by default, even though configure will show the virtualenv's path:

因此,如果您使用系统解释器创建它,VIM将探测此并最终使用系统sys.path默认链接到真正的解释器,即使configure将显示virtualenv的路径:

% PATH=$HOME/foobar/bin:$PATH ./configure --enable-pythoninterp \
    --with-python-config-dir=$HOME/foobar/lib/python2.6/config \
    --prefix=$HOME/foobar
..
checking for python... /home/pat/foobar/bin/python
checking Python's configuration directory... (cached) /home/pat/foobar/lib/python2.6/config
..

% make install
% $HOME/foobar/bin/vim
:python import sys; print sys.path[:1]
['/usr/lib/python2.6']

The workaround: Since your system vim is most likely compiled against your system python, you don't need to rebuild vim for each virtualenv: you can just drop a shell script named vim in your virtualenv's bin directory, which extends the PYTHONPATH before calling system vim:

解决方法:由于您的系统vim很可能是针对您的系统python编译的,因此您不需要为每个virtualenv重建vim:您可以在virtualenv的bin目录中删除名为vim的shell脚本,该目录在调用系统之前扩展PYTHONPATH VIM:

Contents of ~/HOME/foobar/bin/vim:

〜/ HOME / foobar / bin / vim的内容:

#!/bin/sh
ROOT=`cd \`dirname $0\`; cd ..; pwd`
PYTHONPATH=$ROOT/lib/python2.6/site-packages /usr/bin/vim $*

When that is invoked, the virtualenv's sys.path is inserted:

调用它时,将插入virtualenv的sys.path:

% $HOME/foobar/bin/vim
:python import sys; print sys.path[:2]
['/home/pat/foobar/lib/python2.6/site-packages', '/usr/lib/python2.6']

#2


10  

For what it's worth, and no one seems to have answered this here, I had some luck using a command line like the following:

对于它的价值,似乎没有人在这里回答这个问题,我运气好,使用如下命令行:

vi_cv_path_python=/usr/bin/python26 ./configure --includedir=/usr/include/python2.6/ --prefix=/home/bcrowder/local --with-features=huge --enable-rubyinterp --enable-pythoninterp --disable-selinux --with-python-config-dir=/usr/lib64/python2.6/config

vi_cv_path_python = / usr / bin / python26 ./configure --includedir = / usr / include / python2.6 / --prefix = / home / bcrowder / local --with-features = huge --enable-rubyinterp --enable- pythoninterp --disable-selinux --with-python-config-dir = / usr / lib64 / python2.6 / config

#3


4  

I would like to give a similar solution to crowder's that works quite well for me.

我想给出一个类似的解决方案,对我来说效果很好。

Imagine you have Python installed in /opt/Python-2.7.5 and that the structure of that folder is

想象一下,你在/opt/Python-2.7.5中安装了Python,并且该文件夹的结构是

$ tree -d -L 1 /opt/Python-2.7.5/
/opt/Python-2.7.5/
├── bin
├── include
├── lib
└── share

and you would like to build vim with that version of Python. All you need to do is

并且您希望使用该版本的Python构建vim。你需要做的就是

$ vi_cv_path_python=/opt/Python-2.7.5/bin/python ./configure  --enable-pythoninterp --prefix=/SOME/FOLDER

Thus, just by explicitly giving vi_cv_path_python variable to configure the script will deduce everything on it's own (even the config-dir).

因此,只需显式提供vi_cv_path_python变量来配置脚本就可以推断出它自己的所有内容(甚至是config-dir)。

This was tested multiple times on vim 7.4+ and lately with vim-7-4-324.

这在vim 7.4+上进行了多次测试,最近在vim-7-4-324上进行了测试。

#4


1  

I was having this same issue with 3 different versions of python on my system.

我在我的系统上遇到了同样的问题,有3个不同版本的python。

for me the easiest thing was to change my $PATH env variable so that the folder that has the version of python I wanted was (in my case /usr/local/bin) was found before another.

对我来说最简单的事情就是更改我的$ PATH env变量,以便在我的情况下找到具有我想要的python版本的文件夹(在我的情况下为/ usr / local / bin)。

#5


0  

During my compiling vim80, the system python is 2.6, I have another python 2.7 under ~/local/bin, I find that, to make the compiling work:

在我编译vim80期间,系统python是2.6,我在〜/ local / bin下面有另一个python 2.7,我发现,为了编译工作:

  1. update $PATH to place my python path ahead
  2. 更新$ PATH以将我的python路径放在前面

  3. add a soft link, ln -s python python2 ( the configure file would try to locate python config by probing python2 )
  4. 添加一个软链接,ln -s python python2(配置文件将尝试通过探测python2来定位python配置)

  5. make distclean before re-run ./configure to make sure no cached wrong value is picked.
  6. 在重新运行./configure之前make distclean以确保没有选择缓存的错误值。

#1


13  

I'd recommend building vim against the 2 interpreters, then invoking it using the shell script I provided below to point it to a particular virtualenv.

我建议对2个解释器构建vim,然后使用我在下面提供的shell脚本调用它来指向特定的virtualenv。

I was able to build vim against Python 2.7 using the following command (2.7 is installed under $HOME/root):

我能够使用以下命令在Python 2.7上构建vim(2.7安装在$ HOME / root下):

% LD_LIBRARY_PATH=$HOME/root/lib PATH=$HOME/root/bin:$PATH \
    ./configure --enable-pythoninterp \ 
    --with-python-config-dir=$HOME/root/lib/python2.7/config \
    --prefix=$HOME/vim27
% make install
% $HOME/bin/vim27

:python import sys; print sys.path[:2]
['/home/pat/root/lib/python27.zip', '/home/pat/root/lib/python2.7']

Your virtualenv is actually a thin wrapper around the Python interpreter it was created with -- $HOME/foobar/lib/python2.6/config is a symlink to /usr/lib/python2.6/config.

你的virtualenv实际上是一个围绕它创建的Python解释器的瘦包装 - $ HOME / foobar / lib / python2.6 / config是/usr/lib/python2.6/config的符号链接。

So if you created it with the system interpreter, VIM will probe for this and ultimately link against the real interpreter, using the system sys.path by default, even though configure will show the virtualenv's path:

因此,如果您使用系统解释器创建它,VIM将探测此并最终使用系统sys.path默认链接到真正的解释器,即使configure将显示virtualenv的路径:

% PATH=$HOME/foobar/bin:$PATH ./configure --enable-pythoninterp \
    --with-python-config-dir=$HOME/foobar/lib/python2.6/config \
    --prefix=$HOME/foobar
..
checking for python... /home/pat/foobar/bin/python
checking Python's configuration directory... (cached) /home/pat/foobar/lib/python2.6/config
..

% make install
% $HOME/foobar/bin/vim
:python import sys; print sys.path[:1]
['/usr/lib/python2.6']

The workaround: Since your system vim is most likely compiled against your system python, you don't need to rebuild vim for each virtualenv: you can just drop a shell script named vim in your virtualenv's bin directory, which extends the PYTHONPATH before calling system vim:

解决方法:由于您的系统vim很可能是针对您的系统python编译的,因此您不需要为每个virtualenv重建vim:您可以在virtualenv的bin目录中删除名为vim的shell脚本,该目录在调用系统之前扩展PYTHONPATH VIM:

Contents of ~/HOME/foobar/bin/vim:

〜/ HOME / foobar / bin / vim的内容:

#!/bin/sh
ROOT=`cd \`dirname $0\`; cd ..; pwd`
PYTHONPATH=$ROOT/lib/python2.6/site-packages /usr/bin/vim $*

When that is invoked, the virtualenv's sys.path is inserted:

调用它时,将插入virtualenv的sys.path:

% $HOME/foobar/bin/vim
:python import sys; print sys.path[:2]
['/home/pat/foobar/lib/python2.6/site-packages', '/usr/lib/python2.6']

#2


10  

For what it's worth, and no one seems to have answered this here, I had some luck using a command line like the following:

对于它的价值,似乎没有人在这里回答这个问题,我运气好,使用如下命令行:

vi_cv_path_python=/usr/bin/python26 ./configure --includedir=/usr/include/python2.6/ --prefix=/home/bcrowder/local --with-features=huge --enable-rubyinterp --enable-pythoninterp --disable-selinux --with-python-config-dir=/usr/lib64/python2.6/config

vi_cv_path_python = / usr / bin / python26 ./configure --includedir = / usr / include / python2.6 / --prefix = / home / bcrowder / local --with-features = huge --enable-rubyinterp --enable- pythoninterp --disable-selinux --with-python-config-dir = / usr / lib64 / python2.6 / config

#3


4  

I would like to give a similar solution to crowder's that works quite well for me.

我想给出一个类似的解决方案,对我来说效果很好。

Imagine you have Python installed in /opt/Python-2.7.5 and that the structure of that folder is

想象一下,你在/opt/Python-2.7.5中安装了Python,并且该文件夹的结构是

$ tree -d -L 1 /opt/Python-2.7.5/
/opt/Python-2.7.5/
├── bin
├── include
├── lib
└── share

and you would like to build vim with that version of Python. All you need to do is

并且您希望使用该版本的Python构建vim。你需要做的就是

$ vi_cv_path_python=/opt/Python-2.7.5/bin/python ./configure  --enable-pythoninterp --prefix=/SOME/FOLDER

Thus, just by explicitly giving vi_cv_path_python variable to configure the script will deduce everything on it's own (even the config-dir).

因此,只需显式提供vi_cv_path_python变量来配置脚本就可以推断出它自己的所有内容(甚至是config-dir)。

This was tested multiple times on vim 7.4+ and lately with vim-7-4-324.

这在vim 7.4+上进行了多次测试,最近在vim-7-4-324上进行了测试。

#4


1  

I was having this same issue with 3 different versions of python on my system.

我在我的系统上遇到了同样的问题,有3个不同版本的python。

for me the easiest thing was to change my $PATH env variable so that the folder that has the version of python I wanted was (in my case /usr/local/bin) was found before another.

对我来说最简单的事情就是更改我的$ PATH env变量,以便在我的情况下找到具有我想要的python版本的文件夹(在我的情况下为/ usr / local / bin)。

#5


0  

During my compiling vim80, the system python is 2.6, I have another python 2.7 under ~/local/bin, I find that, to make the compiling work:

在我编译vim80期间,系统python是2.6,我在〜/ local / bin下面有另一个python 2.7,我发现,为了编译工作:

  1. update $PATH to place my python path ahead
  2. 更新$ PATH以将我的python路径放在前面

  3. add a soft link, ln -s python python2 ( the configure file would try to locate python config by probing python2 )
  4. 添加一个软链接,ln -s python python2(配置文件将尝试通过探测python2来定位python配置)

  5. make distclean before re-run ./configure to make sure no cached wrong value is picked.
  6. 在重新运行./configure之前make distclean以确保没有选择缓存的错误值。