Solution for WSL (Ubuntu on Windows)
If you use WSL (Ubuntu on Windows), you can also open bash as a terminal in pycharm and activate linux virtualenv.
Use the .pycharmrc file as described in Peter Gibson's answer; Add the .pycharmrc file to your home directory with the following contents:
source ~/.bashrc source ~/path_to_virtualenv/bin/activate
In Pycharm File> Settings> Tools> Terminal, add the following "shell path":
"C:/Windows/system32/bash.exe" -c "bash --rcfile ~/.pycharmrc"
Specific virtualenv project
The path to your virtue in .pycharmrc does not have to be absolute. You can install virtualenv for a specific project by specifying the relative path from the directory of your project. My virtualenv is always in the 'venv' folder in my project directory, so my .pycharmrc file looks like this:
source ~ / .bashrc
source ~ / pycharmvenv / bin / activate #absolute path
source ./venv/bin/activate #relative path
BONUS: automatically open ssh tunnel to connect virtualenv as a project interpreter
Add the following to your .pycharmrc file:
if [ $(ps -aux | grep -c 'ssh') -lt 2 ]; then sudo service ssh start fi
This checks if the ssh tunnel is already open, and opens it otherwise. In the menu File → Settings → Project → Project Interpreter in Pycharm add a new remote interpreter with the following configuration:
+ -------------------------- + ---------------------- ----------- + ------- + ---- +
| Name: | <Interpreter name> | | |
| Select | 'SSH Credentials' | | |
| Host: | 127.0.0.1 | Port: | 22 |
| User: | <Linux username> | | |
| Auth type: | 'Password' | | |
| Password: | <Linux password> | | |
| Python interpreter path: | <Linux path to your virtualenv> | | |
| Python helpers path: | <Set automatically> | | |
+ -------------------------- + ---------------------- ----------- + ------- + ---- +
Now when you open your project, your bash automatically starts in your virtualenv, opens an ssh tunnel, and pycharm connects virtualenv as a remote interpreter.
warning: the latest update on Windows automatically starts the SshBroker and SshProxy services at startup. They block the SSH tunnel from Linux to Windows. You can stop these services in the Task Manager → Services, after which everything will work again.