如何在 PyCharm 的终端中激活 virtualenv? [英] How do I activate a virtualenv inside PyCharm's terminal?

查看:54
本文介绍了如何在 PyCharm 的终端中激活 virtualenv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了 PyCharm,创建了我的 virtualenv(通过 virtual env 命令,或直接在 PyCharm 中)并激活该环境作为我的解释器.一切正常.

I've set up PyCharm, created my virtualenv (either through the virtual env command, or directly in PyCharm) and activated that environment as my Interpreter. Everything is working just fine.

但是,如果我使用工具,打开终端"打开终端,提供的 shell 提示使用虚拟环境;我仍然必须在该终端中使用 source ~/envs/someenv/bin/activate 来激活它.

However, if I open a terminal using "Tools, Open Terminal", the shell prompt supplied is not using the virtual env; I still have to use source ~/envs/someenv/bin/activate within that Terminal to activate it.

另一种方法是在 shell 中激活环境,然后从该环境运行 PyCharm.这是可行的"但非常丑陋,这意味着如果我从 PyCharm 切换环境或项目,我会遇到重大问题:我现在使用的是完全错误的环境.

Another method is to activate the environment in a shell, and run PyCharm from that environment. This is "workable" but pretty ugly, and means I have major problems if I switch environments or projects from PyCharm: I'm now using the totally-wrong environment.

是否有其他更简单的方法可以让工具,打开终端"自动激活虚拟环境?

Is there some other, much-easier way to have "Tools, Open Terminal" automatically activate the virtual environment?

推荐答案

根据 https://www.jetbrains.com/pycharm/whatsnew/#v2016-3-venv-in-terminal,PyCharm 2016.3(2016 年 11 月发布)对开箱即用的终端提供 virutalenv 支持

According to https://www.jetbrains.com/pycharm/whatsnew/#v2016-3-venv-in-terminal, PyCharm 2016.3 (released Nov 2016) has virutalenv support for terminals out of the box

bash、zsh、fish 和 Windows cmd 支持自动 virtualenv.你可以在设置(首选项)中自定义您的外壳首选项 |工具|终端 |检查激活 virtaulenv

Auto virtualenv is supported for bash, zsh, fish, and Windows cmd. You can customize your shell preference in Settings (Preferences) | Tools | Terminal | check Activate virtaulenv

您还需要确保项目结构的内容根文件夹中包含虚拟环境路径的路径.您可以转到设置(首选项)|项目 |项目结构 |如果您的环境未包含在项目目录中.

you also need to make sure to have the path of virtual environment path included in the content root folder of your project structure. You can go to settings (preference) | project | Project Structure | if your environment is not included in the project directory.

在您的主文件夹中创建一个文件 .pycharmrc,其中包含以下内容

Create a file .pycharmrc in your home folder with the following contents

source ~/.bashrc
source ~/pycharmvenv/bin/activate

使用您的 virtualenv 路径作为最后一个参数.

Use your virtualenv path as the last parameter.

然后将shell Preferences->Project Settings->Shell path设置为

Then set the shell Preferences->Project Settings->Shell path to

/bin/bash --rcfile ~/.pycharmrc

这篇关于如何在 PyCharm 的终端中激活 virtualenv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆