Python 如何在 PyCharm 中正确设置 pipenv?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46251411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:32:41  来源:igfitidea点击:

How do I properly setup pipenv in PyCharm?

pythonpycharmvirtualenv

提问by Chris Jung

I need krakenex in a project, so I import it with

我在一个项目中需要 krakenex,所以我用

import krakenex

I have one version of krakenex in

我有一个版本的 krakenex

/Users/x/Library/Python/3.6/lib/python/site-packages

/Users/x/Library/Python/3.6/lib/python/site-packages

. When I execute the script and

. 当我执行脚本和

print(krakenex)

it shows me the path mentioned above.

它向我展示了上面提到的路径。

In the future, I want to use the modules from the packages I installed with e.g.

将来,我想使用我安装的软件包中的模块,例如

pipenv install krakenex

with priority.

有优先权。

How do I manage to do that? Is it sufficient to add the path of the virtual environment to the sys path, or is there a more elegant way?

我该如何做到这一点?把虚拟环境的路径加到sys路径就够了,还是有更优雅的方式?

回答by ForFunAndProfit

You should be pointing your Project Interpreter to the virtualenv python bin. So in PyCharm File->Settings->Project: ProjectName->Project Interpreter, then a windows showing the Project Interpreter should be displayed.

您应该将项目解释器指向 virtualenv python bin。因此,在 PyCharm File->Settings->Project: ProjectName->Project Interpreter 中,应该会显示一个显示 Project Interpreter 的窗口。

Project Interpreter

项目口译员

Next to the top dropdown is a gear and your going to want to Add Local and navigate to the virtualenvs python bin. Something like virtualenvs/virtualenv_name/bin/python. Then your project should be pointing to the right place.

顶部下拉列表旁边是一个齿轮,您将想要添加本地并导航到 virtualenvs python bin。类似的东西virtualenvs/virtualenv_name/bin/python。那么你的项目应该指向正确的地方。

回答by Lawrence Coleman

To add more clarification on how to setup PyCharm with pipenv for now:

现在添加有关如何使用 pipenv 设置 PyCharm 的更多说明:

  1. Execute in your project directory

    pipenv --venv

  1. 在你的项目目录中执行

    pipenv --venv

Save the output, you'll reference this path later

保存输出,稍后您将引用此路径

  1. In PyCharm, Open Project Settings, and then select project interpreter Preferences >Project Interpreter

  2. Click Add a Python Interpreter > System Interpreter > Select Python Interpreter and paste the output from the first command, appending /bin/python onto the end. enter image description here

  1. 在 PyCharm 中,打开项目设置,然后选择项目解释器 首选项 >项目解释器

  2. 单击“添加 Python 解释器”>“系统解释器”>“选择 Python 解释器”并粘贴第一个命令的输出,将 /bin/python 附加到末尾。 在此处输入图片说明

Note that you will need to use the command line to install any packages since PyCharm currently doesn't support pipenv in their package management tools. However, I haven't had a problem with this method.

请注意,您将需要使用命令行来安装任何包,因为 PyCharm 目前在其包管理工具中不支持 pipenv。但是,我对这种方法没有问题。

回答by lmiguelvargasf

PyCharm natively supports pipenv since version 2018.2. PyCharm 2018.2 will automatically create a pipenv when you open a project with a Pipfile, and makes it easy to create new projects with pipenvs.

PyCharm 自版本以来就支持 pipenv 2018.2。PyCharm 2018.2 使用 Pipfile 打开项目时会自动创建一个 pipenv,使用 pipenvs 可以轻松创建新项目。

For existing projects

对于现有项目

As previously stated, for existing projects with a Pipfile, when you open a Python file, PyCharm will ask you if you want to install the dependencies from the Pipfile.lock.

如前所述,对于具有现有项目Pipfile,当你打开Python文件,PyCharm会,如果你想安装从依赖问你Pipfile.lock

pipenv for existing projects

pipenv 用于现有项目

For new projects

对于新项目

For a new project, you can use the project interpreter panel to create a Pipenvbased project interpreter.

对于新项目,您可以使用项目解释器面板来创建一个Pipenv基于项目的解释器。

enter image description here

在此处输入图片说明

回答by Adnan Murtaza

Make sure to update PyCharm. I updated to 2018.3.

确保更新 PyCharm。我更新到2018.3

Export path for pipenv: $ export PATH="$PATH:Users/{user_name}/.local/bin"

pipenv 的导出路径: $ export PATH="$PATH:Users/{user_name}/.local/bin"

PyCharm will then automatically detect pipenvunder new environment usingdropbox. Reference image hereand see full blog post here.

然后 PyCharm 将使用dropbox自动检测新环境下的pipenv这里参考图像,看到完整的博客文章在这里