How to activate virtualenv inside PyCharm terminal?

I installed PyCharm, created my virtualenv (either through the virtual env command, or directly in PyCharm), and activated this environment as my interpreter. Everything works perfectly.

However, if I open the terminal using "Tools, open terminal", then the virtual env is not used in the shell prompt; I still have to use source ~/envs/someenv/bin/activate inside this terminal to activate it.

Another method is to activate the environment in the shell and run PyCharm from this environment. This is “workable”, but rather ugly, and means that I have serious problems if I switch environments or projects from PyCharm: now I am using the completely wrong environment.

Is there another, much simpler way: "Tools, open terminal" automatically activate the virtual environment?

+96
python django shell virtualenv pycharm
Mar 09 '14 at 21:43
source share
21 answers

Edit:

According to https://www.jetbrains.com/pycharm/whatsnew/#v2016-3-venv-in-terminal , PyCharm 2016.3 (released November 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 in Settings (Preferences) | tools | Terminal.

<h / "> Old method:

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

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

Use your virtual path as the last parameter.

Then set the "Settings" → "Project Settings" → "Shell" path to the shell

 /bin/bash --rcfile ~/.pycharmrc 
+87
Mar 09 '14 at 22:35
source share
— -

Update:

Settings in Settings (Settings) | Tools | The terminal is global.
If you use venv for each project, be sure to use the current path variable and the 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 switch for cmd.exe to automatically install the virtual environment.

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 the Shell path .

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

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

+38
Apr 04 '14 at 11:36 on
source share

For Windows users, when using PyCharm and the virtual environment under Windows, you can use the / k option for cmd.exe to automatically configure the virtual environment.

Go to Settings, Terminal, Default Shell and add /K <path-to-your-activate.bat> .

I have no reputation to comment on the previous answer, so I am posting this revised version. It really saves a lot of time.

Update:

Note: Pycharm now directly supports virtual environments, and it seems to suit me, so my workaround is no longer needed.

+38
May 19 '14 at 5:32
source share

Based on Peter's answers and experiments, I came up with a good “general solution” that solves the following:

  • Restores input shell behavior. PyCharm usually starts the login shell, but --rcfile stopped it. Script still uses -rcfile, but tries to emulate the INVOCATION behavior for the login shell.
  • Removes the need to create an rc file for each environment
  • Eliminates the need to update project settings when the environment changes.

Move this Script to the bin directory somewhere. For example. ~ / 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 the PyCharm shell path to:

 /bin/bash --rcfile ~/bin/pycharmactivate 
+7
Mar 14 '14 at 19:46
source share

Thank you, Chris, your script worked on some projects, but not on my machine. Here is the script that I wrote, and I hope someone finds it useful.

 #Stored in ~/.pycharmrc ACTIVATERC=$(python -c 'import re import os from glob import glob try: #sets Current Working Directory to _the_projects .idea folder os.chdir(os.getcwd()+"/.idea") #gets every file in the cwd and sets _the_projects iml file for file in glob("*"): if re.match("(.*).iml", file): project_iml_file = file #gets _the_virtual_env for _the_project for line in open(project_iml_file): env_name = re.findall("~/(.*)\" jdkType", line.strip()) # created or changed a virtual_env after project creation? this will be true if env_name: print env_name[0] + "/bin/activate" break inherited = re.findall("type=\"inheritedJdk\"", line.strip()) # set a virtual_env during project creation? this will be true if inherited: break # find _the_virtual_env in misc.xml if inherited: for line in open("misc.xml").readlines(): env_at_project_creation = re.findall("\~/(.*)\" project-jdk", line.strip()) if env_at_project_creation: print env_at_project_creation[0] + "/bin/activate" break finally: pass ') if [ "$ACTIVATERC" ] ; then . "$HOME/$ACTIVATERC" ; fi 
+6
Jun 07
source share

PyCharm 4 integrates virtualenvs into the IDE. When choosing a project interpreter, you can create, add, or select virtualenv. They added a "Python console" that runs in a customized project interpreter.

More info here.

+6
Apr 29 '15 at 18:19
source share

I looked through all the answers above, but none of them are elegant enough for me. In Pycharm 2017.1.3 (on my computer) the easiest way is to open Settings->Tools->Terminal and check the Shell integration and Activate virtualenv options.

image

+4
Jul 26 '17 at 3:10
source share

If you are using a version of Windows, this is quite simple. If you already have a virtual environment, just go to its folder, find activate.bat inside Scripts . copy its full path and paste it into the pycharm terminal, then press Enter , and you're done!

If you need to create a new virtual environment:

Go to files> settings, then find the project interpreter , open it, press the gear shift button and create the environment where you want, and then follow the first paragraph.

The Gear!

+3
Aug 6 '17 at 12:29
source share

On a Mac, this is PyCharm => Settings ... => Tools => Terminal => Activate virtualenv , which should be enabled by default.

+3
18 Oct '18 at 12:23
source share

I just added a script called pycharmactivate to my home directory. Set the value to PyCharm (4.0.1) File> Settings> Tools> Terminal> Shell path to / bin / bash --rcfile ~ / pycharmactivate. This may not be the best solution, because you have different project and virtual directories / names, but this works for me. This script contains the following 3 lines and assumes your virtualenv has the same name as your dir project.

 source ~/.bashrc projectdir=${PWD##*/} source ~/.virtualenvs/$projectdir/bin/activate 
+2
Nov 28 '14 at
source share

After Peter's answer, here's the Mac version of the .pycharmrc file:

 source /etc/profile source ~/.bash_profile source <venv_dir>/bin/activate 

Chicken

+2
Jun 03 '16 at 15:13
source share

I have a solution that worked on my windows 7 machine.

I believe the PyCharm terminal is the result of running cmd.exe , which will load the Windows PATH variable and use the Python version that it will find first in this PATH . To change this variable, right-click My Computer → Properties → Advanced System Settings → Advanced tab → Environment Variables .... In the System Variables section, select and edit the PATH variable.

Here is the relevant part of my PATH editing before :

C: \ python27 \;
C: \ python27 \ Lib \ site packages \ pip \;
C: \ python27 \ Scripts;
C: \ python27 \ Lib \ site packages \ Django \ Bin;

... and after editing PATH (now only 3 lines):

C: [project_path] \ virtualenv-Py2.7_Dj1.7 \ Lib \ site packages \ pip;
C: [project_path] \ virtualenvs \ virtualenv-Py2.7_Dj1.7 \ Scripts;
C: [project_path] \ virtualenvs \ virtualenv-Py2.7_Dj1.7 \ Lib \ site packages \ Django \ Bin;

To check this, open a Windows new terminal (Start → type cmd and press Enter ) and see if it uses its virtual environment. If this works, restart PyCharm and then check it in the PyCharm terminal.

+1
Nov 22 '14 at 9:30
source share

this is what i am doing: create an active_env.bat file (windows, possibly .sh in linux) in the source folder:

 /env_yourenvlocate/scripts/activate.bat 

and another deactivate_env.bat file:

 /env_yourenvlocate/scripts/deactivate.bat 

each time open a terminal window, just execute the bat file to activate / deactivate virtualenv, you will remain in the path to the source code, you do not need to change the path and vice versa.

 E:\Projects\django_study\src>active_env.bat E:\Projects\django_study\src>../env_django_study/scripts/activate.bat (env_django_study) E:\Projects\django_study\src> (env_django_study) E:\Projects\django_study\src>deactive_env.bat (env_django_study)E:\Projects\django_study\src>../env_django_study/scripts/deactivate.bat E:\Projects\django_study\src> 
+1
Jul 13 '15 at 8:02
source share

If your Pycharm 2016.1.4v and above you should use "default path" /K "<path-to-your-activate.bat>" do not forget quotes

+1
Jun 23 '16 at 4:56 on
source share

Another option is to use virtualenvwrapper to manage your virtual environment. It seems that when the virtualenvwrapper script is activated, pycharm can use it, and then a simple workon command will be available from the pycharm console and present you with the available virtual environments:

 kevin@debian:~/Development/django-tutorial$ workon django-tutorial FlaskHF SQLAlchemy themarkdownapp kevin@debian:~/Development/django-tutorial$ workon django-tutorial (django-tutorial)kevin@debian:~/Development/django-tutorial$ 
0
Feb 14 '15 at 21:31
source share

This method should work with arbitrary virtual environments for each project, and it does not make assumptions in your environment, because it uses the hooks you create.

You write:

  • Global script that calls hook
  • Script hook for PyCharm project (optional)

Given that the current latest PyCharm (Community 2016.1) does not allow setting terminal parameters for each project using a script that calls a specific project hook. This is my ~/.pycharmrc :

 if [ -r ".pycharm/term-activate" ]; then echo "Terminal activation hook detected." echo "Loading Bash profile..." source ~/.bash_profile echo "Activating terminal hook..." source ".pycharm/term-activate" source activate $PYCHARM_VENV fi 

If you use anything other than Bash, call your own equivalent .bash_profile if you want.

Now install PyCharm "Tools → Terminal → Shell Path" to call this script, for example: /bin/bash --rcfile ~/.pycharmrc

Finally, for each PyCharm project you need to activate a specific virtual environment, create a file in the root of the PyCharm project project .pycharm/term-activate . This is your hook, and it will simply determine the name of the desired virtual environment for your PyCharm project:

 export PYCHARM_VENV=<your-virtual-env-name> 

You can, of course, expand your hooks with everything that is convenient for you, in the terminal environment of your specific PyCharm project.

0
Apr 25 '16 at 14:37
source share

For conda virtual environments on Windows, make sure your batch file is NOT named activate.bat , as this will conflict with the conda activate command, which will cause the batch file to be recursively called.

The following shell path works for me:

 "cmd.exe" /k ""C:\FullPathToYourProject\activate-env.bat"" 

And in the activate-env.bat file:

 call activate myenvname 
0
Jul 19 '16 at 9:14
source share

I need a separate virtual environment for each project, and it was not very important for me to have additional files to facilitate this. The solution that you need to run only once for all projects adds the following to your .bashrc or .bash_profile :

 if [ -d "./venv" ]; then source ./venv/bin/activate fi 

This checks to see if there is a virtual environment in which the terminal is open, and if it does so (and, of course, other relative paths can be used). PyCharm terminal settings can be left as default.

0
Nov 22 '16 at 23:37
source share

PyCharm 4.5.4

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

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

Use your virtual path as the last parameter.

Then set the "Settings" → "Project Settings" → "Shell" path to the shell

 /bin/bash --rcfile ~/.pycharmrc 

I do not understand, but it does not 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 it is not needed.

This receipt works! But the line /env_yourenvlocate/scripts/activate.bat should contain quotes, such as "Full_path_to_your_env_locate\scripts\activate.bat" !

Deactivating virtualenv is easy - enter the terminal "deactivate"

 (virt_env) D:\Projects\src>deactivate D:\Projects\src> 
0
Dec 14 '16 at 12:03
source share

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.

0
Jan 10 '18 at 11:14
source share

One option you have when you go into terminal> Run> Debug> Change Settings enter image description here

enter image description here

select the appropriate conda environmentmnent .. Also, when creating a new project - he asks to configure this location.

0
Feb 09 '18 at 3:16
source share



All Articles