使用特定版本的Python编译vim

发布于 2024-11-04 16:20:38 字数 1005 浏览 0 评论 0原文

我正在开发几个在不同版本的 Python 上运行的 Python 项目。我希望设置我的 vim 环境以使用 Ropevim、pyflakes 和 pylint,但我遇到了由于使用单个 vim 引起的一些问题(针对特定版本的 Python 编译,与项目的 Python 版本不匹配) 。

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

目前,我安装了 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

结果如下config.log:

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

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

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

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).

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.

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.

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

Results in the following in 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

音栖息无 2024-11-11 16:20:38

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

我能够使用以下命令针对 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']

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

因此,如果您使用系统解释器创建它,VIM 将探测这一点并最终链接到真正的解释器,默认情况下使用系统 sys.path,即使配置将显示 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']

解决方法:由于您的系统 vim 很可能是针对系统 python 进行编译的,因此您无需为每个 virtualenv 重建 vim:您只需将名为 vim 的 shell 脚本放入你的 virtualenv 的 bin 目录,其中在调用系统 vim 之前扩展 PYTHONPATH:

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

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

调用时,将插入 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']

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.

I was able to build vim against Python 2.7 using the following command (2.7 is installed under $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.

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:

% 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:

Contents of ~/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:

% $HOME/foobar/bin/vim
:python import sys; print sys.path[:2]
['/home/pat/foobar/lib/python2.6/site-packages', '/usr/lib/python2.6']
小鸟爱天空丶 2024-11-11 16:20:38

无论如何,似乎没有人在这里回答这个问题,我很幸运地使用了如下命令行:

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

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
揪着可爱 2024-11-11 16:20:38

我想为crowder提供一个类似的解决方案,该解决方案对我来说非常有效。

假设您已将 Python 安装在 /opt/Python-2.7.5 中,并且该文件夹的结构是

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

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

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

因此,只需将 vi_cv_path_python 变量显式提供给 configure ,脚本就会自行推断出所有内容(甚至是配置目录)。

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

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

$ 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

$ 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 its own (even the config-dir).

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

二智少女 2024-11-11 16:20:38

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

对我来说,最简单的事情是更改我的 $PATH 环境变量,以便具有我想要的 python 版本的文件夹是(在我的情况下 /usr/local/bin )先于另一个被发现。

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

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.

白衬杉格子梦 2024-11-11 16:20:38

在我编译vim80的过程中,系统python是2.6,我在~/local/bin下有另一个python 2.7,我发现,为了使编译工作:

  1. 更新$PATH以将我的python路径放在前面
  2. 添加一个软链接, ln -s python python2 (配置文件将尝试通过探测 python2 来定位 python 配置)
  3. make distclean 在重新运行 ./configure 之前确保没有选择缓存的错误值。

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:

  1. update $PATH to place my python path ahead
  2. add a soft link, ln -s python python2 ( the configure file would try to locate python config by probing python2 )
  3. make distclean before re-run ./configure to make sure no cached wrong value is picked.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文