Unable to start virtual environment (virtualenv) in wsl in integrated terminal

I am running Ubuntu in wsl on windows.
From the terminal settings, I configured terminal to use wsl.exe instead of cmd.exe
My problem is that pycharm doesn't automatically start my virtual environment in the terminal when I open it. I have to manually go to the location and manually start using source.
I have already configured the environment in Project Settings -> Interpreter.
The python console (django console) is working properly in the venv, but the integrated terminal is not

How do I configure the integrated terminal to automatically start with the virtual environment (like it does in normal linux distributions) 

4

Hi,

Are you using virtualenv inside WSL? Please provide more information regarding your project interpreter configuration, preferably with screenshots. I'm interested how exactly it is configured and where your virtual environment directory is located.

 

0
Avatar
Permanently deleted user

Yes. I am using virtualenv inside wsl. Here are some screenshots

1. Tjis is how I have setup my python interpreter

2. This is how I setup integrated terminal to use wsl instead of cmd. Also I have enabled "Activate virtualenv option"

 

3. But when I start the terminal, it does not start with the environment. I have to manually start it

 

4. When I open django console, I get this weird error

However the django shell is working properly without issues.

I dont get these problems when I using a linux machine directly.
They seem to come up only on wsl

0
Avatar
Permanently deleted user

Hey, I was ablet to get my virtual env started automatically by using this path for my terminal instead - C:\Windows\System32\bash.exe

0
Avatar
Permanently deleted user

Hey, I have followed the same steps including using bash.exe as terminal, but the virtualenv is not activated automatically. Any ideas why? Thanks

0
Avatar
Permanently deleted user

So I came up with a workaround to get my virtualenv activated automatically

1) Add this to your ~/.bashrc

if [ -n "$ACTIVATE_VENV" ]; then . "$ACTIVATE_VENV"; fi

effectively this means if there is an environment variable called ACTIVATE_VENV then treat it as a filename and source it

2) In PyCharm's Terminal settings add an environment variable called ACTIVATE_VENV with the (linux) path to the activate command in your virtualenv

3) As this is a windows environment variable passed to wsl, it will not automatically be available to your linux shell so add another environment variable called WSLENV set to be ACTIVATE_VENV. This tells wsl to pass that environment variable through

5

Any official solution to this? I'm getting the same error.

0

Hello,

Unfortunately, this feature wasn't implemented yet; please vote for the issue on YouTrack to raise its relevance.

0

Building on top of James Hardi answer, added next to my ~/.bashrc to make the activate universal for all projects, just indicate your own parent directory:


parent_directory="/mnt/c/Users/divi/PycharmProjects"
case $PWD/ in
       $parent_directory/*) export ACTIVATE_VENV=~/.virtualenvs/${PWD##*/}/bin/activate;;
esac
if [ -n "$ACTIVATE_VENV" ]; then . "$ACTIVATE_VENV"; fi

0

请先登录再写评论。