Solution for WSL (Ubuntu on Windows)
If you're using WSL (Ubuntu on Windows), you can also open bash as terminal in pycharm and activate a linux virtualenv.
Use a .pycharmrc
file like described in Peter Gibson's answer; Add the .pycharmrc
file to your home directory with following content:
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"
Project specific virtualenv
The path to your virtualenv in .pycharmrc
does not have to be absolute. You can set a project specific virtualenv by setting a relative path from your project directory.
My virtualenv is always located in a 'venv' folder under 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 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 a ssh tunnel is already opened, and opens one otherwise.
In File -> Settings -> Project -> Project Interpreter in Pycharm, add a new remote interpreter with 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 a ssh tunnel, and pycharm connects the virtualenv as remote interpreter.
warning: the last update in Windows automatically starts a SshBroker and SshProxy service on startup. These block the ssh tunnel from linux to windows. You can stop these services in Task Manager -> Services, after which everything will work again.