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

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

问题描述

我设置了PyCharm,创建了我的virtualenv(通过虚拟env命令,或者直接在PyCharm中),并激活了这个环境作为我的口译员。一切都正常工作。



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



另一种方法是在shell中激活环境,并从该环境运行PyCharm。这是可行的,但很丑陋,意味着如果我从PyCharm切换环境或项目,我有很大的问题:我正在使用完全错误的环境。



有没有其他更容易的方式来让工具,开放终端自动激活虚拟环境?

解决方案

编辑:



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


对于bash,zsh,fish和Windows cmd,支持auto virtualenv。你
可以在设置(首选项)|中自定义你的shell首选项工具
|终端。




旧方法:



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

  source〜/ .bashrc 
source〜/ pycharmvenv / bin / activate

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



然后将Shell Preferences-> Project Settings-> Shell路径设置为

  / bin / bash --rcfile〜/ .pycharmrc 


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.

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.

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?

解决方案

Edit:

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

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


Old Method:

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

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

Using your virtualenv path as the last parameter.

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

/bin/bash --rcfile ~/.pycharmrc

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

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