How do I activate a virtualenv inside PyCharm's terminal?

207,475

Solution 1

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 | check Activate virtaulenv

you also need to make sure to have the path of virtual environment path included in the content root folder of your project structure. You can go to settings (preference) | project | Project Structure | if your environment is not included in the project directory.


***Old Method:***

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

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

Use your virtualenv path as the last parameter.

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

/bin/bash --rcfile ~/.pycharmrc

Solution 2

Update:

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:

"cmd.exe" /k ""%CD%\venv\Scripts\activate"" 

For Windows users: when using PyCharm with a virtual environment, you can use the /K parameter to cmd.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> to Shell path.

PyCharm 2016.1 or 2016.2: Settings, Tools, Terminal, and add ""/K <path-to-your-activate.bat>"" to Shell path and add (mind the quotes). Also add quotes around cmd.exe, resulting in:

"cmd.exe" /k ""C:\mypath\my-venv\Scripts\activate.bat""

Solution 3

Somehow a small trick worked for me. All you gotta do is change the default terminal from Power shell to CMD.

  1. Open pycharm --> Go to Settings --> Tools --> Terminal
  2. Change the Shell Path to C:\Windows\system32\cmd.exe from PS
  3. Check the Activate virtualenv checkbox
  4. Hit apply and open new terminal

It's 2021 you don't need to specify the file path or add the environment variable.

Update: It's 2022 and I run into the same issue Fix: Follow the above steps and in addition, make sure you have selected the Your virtual env python.exe as your project python interpreter, and that's it

Solution 4

If You are using windows version it is quite easy. If you already have the virtual environment just navigate to its folder, find activate.bat inside Scripts folder. copy it's full path and paste it in pycharm's terminal then press Enter and you're done!

If you need to create new virtual environment :

Go to files > settings then search for project interpreter, open it, click on gear button and create the environment wherever you want and then follow first paragraph.

The Gear!

Solution 5

Based on answers from Peter and experimentation, I've come up with a good "general solution", which solves the following:

  • Restores the behaviour of a login shell. PyCharm normally runs a login shell, but --rcfile stopped this happening. Script still uses --rcfile, but attempts to emulate the INVOCATION behaviour of a login shell.
  • Removes the need to create an rcfile for each environment
  • Removes the need to update the project settings if you change the environment.

Drop this script into a bin directory somewhere. E.g. ~/bin/pycharmactivate

if [ -r "/etc/profile" ] ; then . /etc/profile ; fi
if [ -r "~/.bash_profile" ] ; then
    . ~/.bash_profile
elif [ -r "~/.bash_login" ] ; then
    . ~/.bash_login
elif [ -r "~/.profile" ] ; then
    . ~/.profile
fi
ACTIVATERC=`cat .idea/workspace.xml | perl -n -e 'print "\$1/bin/activate" if m:option name="SDK_HOME" value="\\\$USER_HOME\\\$(.*)/bin/python":'`
if [ -n "$ACTIVATERC" ] ; then . "$HOME/$ACTIVATERC" ; else echo "Could not find virtualenv from PyCharm" ; fi

Then set PyCharm's Shell path to:

/bin/bash --rcfile ~/bin/pycharmactivate
Share:
207,475
Chris Cogdon
Author by

Chris Cogdon

Swimming in stuff mostly consisting of Python, Django, PostgreSQL and SSL Certificate infrastructure.

Updated on January 09, 2022

Comments

  • Chris Cogdon
    Chris Cogdon over 2 years

    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?

  • Chris Cogdon
    Chris Cogdon over 10 years
    Thank you! I hadn't thought of overriding the shell's rc file. To better support pycharm working with multiple projects, each which might have a different virtualenv, I'd call the ".pycharmrc" file something different, and perhaps put it into the env directory itself. ~/pycharmenv/bin/terminalactivate sounds like a good option.
  • Chris Cogdon
    Chris Cogdon over 9 years
    However, that requires me have to activate the workon script every time I open the terminal, and know which virtual environment pycharm is currently using for running the program, both of which I was explicitly trying to avoid.
  • frnhr
    frnhr almost 9 years
    How does this answer the OP? Still have to source /path/to/venv/bin/activate manually in the terminal.
  • Sam R.
    Sam R. almost 9 years
    @PeterGibson, pferate's answer has nothing to do with the OP's question. The problem is terminal not the Python interpreter. The venv integration has been there way before PyCharm 4. Your answer works though.
  • Peter Gibson
    Peter Gibson almost 9 years
    @norbertpy I hadn't tested it so thanks for the update
  • tchakravarty
    tchakravarty almost 9 years
    This is the simplest solution, except that I place my .pycharmrc in the project home folder, so that I can have a different file for each project. In a properly configured project, there is no need to provide the absolute path to the .pycharmrc file.
  • zerohedge
    zerohedge over 8 years
    THANK YOU! This worked for me after hours of unsuccessful attempts to figure out where to start bashrc and bash profiles. However it threw and error that it could'nt find my virtual env so I modified it like so, can you tell me if it's OK? if [ -r "/etc/profile" ] ; then . /etc/profile ; fi if [ -r "~/.bash_profile" ] ; then . ~/.bash_profile elif [ -r "~/.bash_login" ] ; then . ~/.bash_login elif [ -r "~/.profile" ] ; then . ~/.profile fi source ~/learnp/project1/venv/bin/activate
  • Chris Cogdon
    Chris Cogdon over 8 years
    @zerohedge: Your script will work fine, but it's fixed to your "project1" virtualenv. The script above was supposed to do auto-detection, but there's a bunch of assumptions being made: that the initial working directory is the location of the ".idea" directory; that the format of the workspace.xml file doesn't change significantly. Unfortunately, it looks like pycharm version 5 has made significant changes in that area. I can't actually figure out where the data is, now.
  • zerohedge
    zerohedge over 8 years
    well I don't mind changing my addition depending on the project, since the Shell Path to is also project specific. I could only get a prompt saying "bash 3.2$:" before finding your script, with no indication of my current directory or any resemblance of my Terminal prompt. Thank you again!
  • renskiy
    renskiy over 7 years
    At least for macOS you have to add -i option after --rcfile to enable interactive mode or your ~/.pycharmrc will not be executed. Here is what man bash says about --rcfile option: Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive
  • Cloud
    Cloud over 7 years
    macos 10.12.2, pycharm 2016.3.2 /bin/bash --rcfile ~/.pycharmrc or /bin/bash --rcfile -i ~/.pycharmrc not work for me @renskiy
  • renskiy
    renskiy over 7 years
    @SiminJie, try this one: /bin/bash --rcfile ~/.pycharmrc -i
  • Cloud
    Cloud over 7 years
    @renskiy /bin/bash --rcfile ~/.pycharmrc -i return bash: /Users/username/pycharmvenv/bin/activate: No such file or directory -- Pycharm version 2016.3.2
  • renskiy
    renskiy over 7 years
    @SiminJie, PyCharm 2016.3.2 has such option from the box. Go to the: settings -> Tools -> Terminal. And make sure "Activate virtualenv" option is enabled.
  • Nir Alfasi
    Nir Alfasi about 7 years
    "I don't why, but it doesn't work for me" - maybe because this solution is good for linux/mac - not for windows ? ;)
  • user1700890
    user1700890 over 6 years
    Great answer! Thank you! Quick question. I have 3 different virtual environments that I created in Anaconda. Is there separate activate.bat for each of them?
  • AmiNadimi
    AmiNadimi over 6 years
    @user1700890 yes! In case you haven't overwritten the environments, they should have separate folders and as a result, separate activate.bat files. if you like the answer, feel free to upvote ;)
  • user1700890
    user1700890 over 6 years
    Thank you, I just checked, there are no folders in Scripts folder. Also Pycharm complains that activate.bat is not a valid python SDK
  • AmiNadimi
    AmiNadimi over 6 years
    So i think you should first try and find the virtual-environment folder created when you where using Anaconda and then run the activate batch file inside it. or if it is possible for you, just go with creating a new environment.
  • Soorena
    Soorena over 6 years
    also I used source `pwd`/.env/bin/activate instead of second line, It made me able to use same rc file for other projects too.
  • Vikas Prasad
    Vikas Prasad over 6 years
    Well, the accepted answer quotes this very same thing. Its just that it doesn't have picture.
  • Vikas Prasad
    Vikas Prasad over 6 years
    @pferate @norbertpy @renskiy The Tools -> Terminal -> Activate virtual env solution works if I want to do bash script.sh in PyCharm terminal. But I am not able to make the virtualenv effective for the case when I right click on bash file and select Run script.sh. When I run this way, it doesn't respect the virtualenv. I can add virtual env for Python type run configurations, but couldn't find how to set virtual env for Bash type run configuration. Running script.sh as a before launch activity inside a virtual environment set Python run configuration doesn't work either. Help
  • Vikas Prasad
    Vikas Prasad over 6 years
    well for now I just did this hack where I activated the virtual env in my script.sh itself by adding these lines: source `which virtualenvwrapper.sh` workon <venvname>
  • xji
    xji almost 6 years
    Is this option only available in PyCharm but not IntelliJ IDEA? I went to Settings -> Tools -> Terminal but there is no option related to virtual env.
  • Peter Gibson
    Peter Gibson almost 6 years
    @xji I expect you'd need to use the Python specific PyCharm
  • bad_coder
    bad_coder about 4 years
    In this case %CD% is the current working directory, see Windows shell command to get the full path to the current directory?
  • Tobias Uhmann
    Tobias Uhmann over 3 years
    I also had to restart PyCharm for the "Activate virtualenv" setting to kick in again. Opening an new terminal tab was not enough.
  • Patrick
    Patrick about 3 years
    This was exactly what I was looking for. As someone who enjoys Debian but is too lazy to set up a second OS, I cannot thank you enough. I would like to add that the activate script provided to me by Pycharm ended up needing to be run through dos2unix before it would run correctly.
  • Dan
    Dan about 3 years
    Still you have to be careful with PyCharm's terminal. I polluted my host's python dependencies because I believed that PyCharm was intelligent enough to automatically activate the venv in the console.