简体   繁体   English

如何在 Eclipse 中使用 Pyenv virtualenv?

[英]How to use a Pyenv virtualenv from within Eclipse?

I am using Eclipse on Linux to develop C applications, and the build system I have makes use of make and python .我在 Linux 上使用 Eclipse 来开发 C 应用程序,我的构建系统使用makepython I have a custom virtualenv installed and managed by pyenv , and it works fine from the command line if I pre-select the virtualenv with, say pyenv shell myvenv .我有一个由pyenv安装和管理的自定义 virtualenv ,如果我用pyenv shell myvenv预先选择 virtualenv ,它可以pyenv shell myvenv

However I want Eclipse to make use of this virtualenv when building (via "existing makefile") from within Eclipse.但是,我希望 Eclipse 在从 Eclipse 内部构建(通过“现有的makefile”)时利用这个 virtualenv。 Currently it runs my Makefile but uses the system python in /usr/bin/python , which is missing all of the packages needed by the build system.目前它运行我的 Makefile 但使用/usr/bin/python的系统 python ,它缺少构建系统所需的所有包。

It isn't clear to me how to configure Eclipse to use a custom Python interpreter such as the one in my virtualenv.我不清楚如何配置 Eclipse 以使用自定义 Python 解释器,例如我的 virtualenv 中的解释器。 I have heard talk of setting PYTHONPATH however this seems to be for finding site-packages rather than the interpreter itself.我听说过设置PYTHONPATH但这似乎是为了查找站点包而不是解释器本身。 My virtualenv is based on python 3.7 and my system python is 2.7, so setting this alone probably isn't going to work.我的 virtualenv 基于 python 3.7,我的系统 python 是 2.7,所以单独设置它可能行不通。

I am not using PyDev (this is a C project, not a Python project) so there's no explicit support for Python in Eclipse.我没有使用 PyDev(这是一个 C 项目,不是 Python 项目),所以在 Eclipse 中没有对 Python 的明确支持。 I'd prefer not to install PyDev if I can help it.如果可以的话,我宁愿不安装 PyDev。

I've noticed that pyenv adds its plugins , shims and bin directories to PATH when activated.我注意到 pyenv 在激活时将其pluginsshimsbin目录添加到 PATH 中。 I could explicitly add these to PATH in Eclipse, so that Eclipse uses pyenv to find an interpreter.我可以在 Eclipse 中将这些显式添加到 PATH,以便 Eclipse 使用 pyenv 来查找解释器。 However I'd prefer to point directly at a specific virtualenv rather than use the pyenv machinery to find the current virtualenv.但是,我更愿意直接指向特定的 virtualenv,而不是使用 pyenv 机制来查找当前的 virtualenv。

I had the same trouble and after some digging, there are two solutions;我遇到了同样的麻烦,经过一番挖掘,有两种解决方案; project-wide and workspace-wide.项目范围和工作空间范围。 I prefer the project-wide as it will be saved in the git repository and the next person doesn't have to pull their hair.我更喜欢整个项目,因为它将保存在 git 存储库中,下一个人不必拉扯头发。

For the project-wide add /Users/${USER}/.pyenv/shims: to the start of the "Project properties > C/C++ Build > Environment > PATH".对于整个项目,将/Users/${USER}/.pyenv/shims:添加到“项目属性 > C/C++ 构建 > 环境 > PATH”的开头。

I couldn't figure out the other method fully (mostly because I'm happy with the other one) but it should be with possible to modify "Eclipse preferences > C/C++ > Build > Environment".我无法完全弄清楚另一种方法(主要是因为我对另一种方法很满意)但应该可以修改“Eclipse 首选项 > C/C++ > Build > Environment”。 You should change the radio button and add PATH variable.您应该更改单选按钮并添加 PATH 变量。

For me, following steps worked ( mac os 10.12, eclipse photon version, with pydev plugin)对我来说,以下步骤有效(mac os 10.12,eclipse photon 版本,带有 pydev 插件)

  1. Project -> properties项目 -> 属性
  2. Pydev-Interpreter/Grammar Pydev-解释器/语法
  3. Click here to configure an interpreter not listed (under interpret combobox)单击此处配置未列出的解释器(在解释组合框下)
  4. open interpreter preference page打开解释器首选项页面
  5. Browse for python/pypy exe -> my virtualenvdirectory/bin/python浏览 python/pypy exe -> my virtualenvdirectory/bin/python
  6. Then the chosen python interpreter path should show ( for me still, it was not pointing to my virtual env, but I typed my path explicitly here and it worked)然后选择的python解释器路径应该显示(对我来说,它仍然没有指向我的虚拟环境,但我在这里明确输入了我的路径并且它起作用了)

In the bottom libraries section, you should be able to see the site-packages from your virtual env在底部库部分,您应该能够从您的虚拟环境中看到站点包

Extra tip - In my mac os the virtual env was starting with .pyenv, since it's a hidden directory, I was not able to select this directory and I did not know how to view the hidden directory in eclipse file explorer.额外提示 - 在我的 mac os 中,虚拟环境以 .pyenv 开头,因为它是一个隐藏目录,我无法选择这个目录,我不知道如何在 eclipse 文件资源管理器中查看隐藏目录。 Therefore I created an softlink ( without any . in the name) to the hidden directory (.pyenv) and then I was able to select the softlink因此,我创建了一个指向隐藏目录 (.pyenv) 的软链接(名称中没有任何 .),然后我可以选择该软链接

Typing CMD+SHIFT+.键入 CMD+SHIFT+。 will show you dotfiles & directories that begin with dot in any Mac finder dialog box...将在任何 Mac 查找器对话框中显示以点开头的点文件和目录...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM