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?
Based on answers from Peter and experimentation, I've come up with a good "general solution", which solves the following:
Drop this script into a bin directory somewhere. E.g. ~/bin/pycharmactivate
Then set PyCharm's Shell path to:
If you have moved your project to another directory, you can set the new path via Settings dialog. And then you need to set this Project Interpreter in the Edit Configuration dialog.
If your Pycharm 2016.1.4v and higher you should use
"default path" /K "<path-to-your-activate.bat>"
don't forget quotesUpdate:
The preferences in Settings (Preferences) | Tools | Terminal are global.
If you use a venv for each project, remember to use current path variable and a default venv name:
For Windows users: when using PyCharm with a virtual environment, you can use the
/K
parameter tocmd.exe
to set the virtual environment automatically.PyCharm 3 or 4:
Settings
,Terminal
,Default shell
and add/K <path-to-your-activate.bat>
.PyCharm 5:
Settings
,Tools
,Terminal
, and add/K <path-to-your-activate.bat>
toShell path
.PyCharm 2016.1 or 2016.2:
Settings
,Tools
,Terminal
, and add""/K <path-to-your-activate.bat>""
toShell path
and add (mind the quotes). Also add quotes around cmd.exe, resulting in:"cmd.exe" /k ""C:\mypath\my-venv\Scripts\activate.bat""
PyCharm 4.5.4
I don't why, but it doesn't work for me. PyCharm prints an error.
cmd.exe /K "<path-to-your-activate.bat>"
It works, but it creates the same virtualenv for each project, and even if this is not necessary.This receipt is working! But the string
/env_yourenvlocate/scripts/activate.bat
must contain quotes, like this"Full_path_to_your_env_locate\scripts\activate.bat"
!Deactivate the virtualenv is very easy - type in the terminal 'deactivate'
This method should work with arbitrary virtual environments per project and it doesn't make assumptions on your environment as it is using hooks you create.
You write:
Given that the current latest PyCharm (Community 2016.1) does not allow for Terminal settings per project start with the script that invokes the project specific hook. This is my
~/.pycharmrc
:If you are using something other than Bash, invoke your own
.bash_profile
equivalent should you wish to.Now set your PyCharm "Tools -> Terminal -> Shell Path" to invoke this script, e.g.:
/bin/bash --rcfile ~/.pycharmrc
Finally, for every PyCharm project you need a specific virtual environment activated, create a file within the PyCharm project root
.pycharm/term-activate
. This is your hook and it will simply define the name of the desired virtual environment for your PyCharm project:You can of course extend your hooks with anything you find useful in the terminal environment of your particular PyCharm project.