Activating virtual environment in Pycharm python console


I am trying to use an existing virtual environment in PyCharm:  I picked the python in the virtual environment as the project python interpreter,  but in the console and terminal the active environment is still the base environment. 

How can I activate the same environment in the console and terminal?  
Thank you!

Hi Elena, PyCharm should activate the virtualenv from Settings | Project ... | Project Interpreter in the terminal. In the console the interpreter is usually the same, but you can change it in Settings | Build, Execution, Deployment | Console | Python Console. If this is not the case please provide a screenshot of the project interpreter window and specify how did you check if the same one was used in the terminal/console.


Thank you for your message!

I can change the project interpreter, but the console and the terminal ones are still the base environment. 
The way I'm checking is the following:
    -the environment that I've created does not have pystan installed, it's called without_stan and I check by importing pystan
    -once I change the project interpreter to the python in the without_stan environment, in the editor I get an error message, saying that there is no pystan module.
    -however in the terminal if I list the environments I see that the base environment is active, and I can launch python and import pystan   (I've attached a screenshot where you can see both the error message in the editor and the terminal)
   -in the console I can also import pystan, even though the console interpreter is the one from the without_stan environment (I've also attached a screenshot of this)
   -I've attached a screenshot of the project interpreter as well
I'm only using this without_stan environment to test environment activation. What I need in fact is to use pystan, but the trouble is that the pystan currently in the base environment does not work properly. I have an environment in which it does work properly, and that's the one that I want in fact to activate.  
Thank you!


Thank you. I think the problem is caused by PY-23417, PyCharm doesn't activate conda venv correctly.



I see.. Anything I could do to circumvent this? 




The only workaround I have in mind is to manually reactivate the venv:

conda deactivate
conda activate <venv>

This I guess will work in the terminal, but would it also work in the console?

Thank you!


Could you please start the console, execute the following code and provide the output here?


import sys
import os
import pkg_resources
from pprint import pprint

    'sys.version_info': sys.version_info,
    'sys.prefix': sys.prefix,
    'sys.path': sys.path,
    'pkg_resources.working_set': list(pkg_resources.working_set),
    'os.environ': {
        name: value.split(os.pathsep) if 'PATH' in name else value
        for name, value in os.environ.items()



Here is the output:

{'os.environ': {'BASH_FUNC_module%%': '() { eval `/usr/bin/modulecmd bash $*`\n}',
'COMPIZ_BIN_PATH': ['/usr/bin/'],
'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-EUns5WUSji',
'DEFAULTS_PATH': ['/usr/share/gconf/ubuntu.default.path'],
'DESKTOP_SESSION': 'ubuntu',
'DISPLAY': ':0',
'GDMSESSION': 'ubuntu',
'GDM_LANG': 'en_GB',
'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated',
'GPG_AGENT_INFO': '/nfs/nhome/live/elena/.gnupg/S.gpg-agent:0:1',
'GTK2_MODULES': 'overlay-scrollbar',
'GTK_IM_MODULE': 'ibus',
'GTK_MODULES': 'gail:atk-bridge:unity-gtk-module',
'HOME': '/nfs/nhome/live/elena',
'JOB': 'unity-settings-daemon',
'LANG': 'en_GB.UTF-8',
'LANGUAGE': 'en_GB:en',
'LD_LIBRARY_PATH': ['.', ''],
'LOGNAME': 'elena',
'MANDATORY_PATH': ['/usr/share/gconf/ubuntu.mandatory.path'],
'MODULEPATH': ['/etc/environment-modules/modules',
'MODULESHOME': '/usr/share/modules',
'MODULE_VERSION': '3.2.10',
'NLSPATH': ['/usr/dt/lib/nls/msg/%L/'],
'PATH': ['/nfs/nhome/live/elena/anaconda/bin',
'PWD': '/nfs/nhome/live/elena/pycharm-community-2016.2.3/bin',
'PYTHONPATH': ['/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis',
'QT4_IM_MODULE': 'xim',
'QT_IM_MODULE': 'ibus',
'QT_QPA_PLATFORMTHEME': 'appmenu-qt5',
'SESSION': 'ubuntu',
'SESSIONTYPE': 'gnome-session',
'SESSION_MANAGER': 'local/u236d:@/tmp/.ICE-unix/10081,unix/u236d:/tmp/.ICE-unix/10081',
'SHELL': '/bin/bash',
'SHLVL': '2',
'SSH_AUTH_SOCK': '/run/user/5302/keyring/ssh',
'TERM': 'emacs',
'UPSTART_EVENTS': 'xsession started',
'UPSTART_JOB': 'unity7',
'UPSTART_SESSION': 'unix:abstract=/com/ubuntu/upstart-session/5302/9848',
'USER': 'elena',
'VTE_VERSION': '4205',
'WINDOWID': '48234506',
'XAUTHORITY': '/nfs/nhome/live/elena/.Xauthority',
'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',
'XDG_DATA_DIRS': '/usr/share/ubuntu:/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/var/lib/snapd/desktop',
'XDG_GREETER_DATA_DIR': '/var/lib/lightdm-data/elena',
'XDG_MENU_PREFIX': 'gnome-',
'XDG_RUNTIME_DIR': '/run/user/5302',
'XDG_SEAT': 'seat0',
'XDG_SEAT_PATH': ['/org/freedesktop/DisplayManager/Seat0'],
'XDG_SESSION_PATH': ['/org/freedesktop/DisplayManager/Session0'],
'XDG_VTNR': '7',
'XFILESEARCHPATH': ['/usr/dt/app-defaults/%L/Dt'],
'XMODIFIERS': '@im=ibus',
'_': './../jre/jre/bin/java'},
'pkg_resources.working_set': [wsgiref 0.1.2 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7),
Python 2.7.15 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-dynload),
six 1.11.0 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
pytz 2018.3 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
python-dateutil 2.7.2 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
permutation-test 0.18 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
pandas 0.22.0 (/nfs/nhome/live/elena/.local/lib/python2.7/site-packages),
wheel 0.31.1 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages),
setuptools 40.2.0 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages),
pip 10.0.1 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages),
certifi 2018.8.24 (/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages)],
'sys.path': ['/nfs/nhome/live/elena/pycharm-community-2016.2.3/helpers/pydev',
'sys.prefix': '/nfs/nhome/live/elena/anaconda/envs/without_stan',
'sys.version_info': sys.version_info(major=2, minor=7, micro=15, releaselevel='final', serial=0)}


You can activate the console through terminal via "python3" command (exit with "exit()" command). You can then import modules, that are inside the virtual environment. The Pycharm "python console" instrument seems working for everything else, besides importing modules from inside the virtualenv.


Please try 2019.1 EAP build, the original problem with conda venv activation in the terminl should be fixed there (PY-23417).



I'm having same predicament.  The same problem occurs if I use "Run".  The virtual environment doesn't get activated.

I just downloaded and tried the 2019.1 EAP but I still have the same problem.



Can you try the workaround from and tell us if it's working? :

  • run import os in the system terminal using the same interpreter
  • run print(os.environ['PATH'])
  • copy the result and add as PATH environment variable to your Run/Debug Configuration
  • do the same for Python Console settings to get it working too.

We have come up with a similar work around here.  We added the library paths to our project interpreter Paths.

I'm relieved that there are workarounds for library paths but you should still know that this isn't a fix. 

The virtual environment's activation script is not ran and that is troublesome for some members of the python community. The basic introduction to python virtual environments instructs us to activate them.  It seems rational to expect our IDE to do the same.

When I have a virtual environment selected as project interpreter, the Terminal has no trouble activating it.  I expect the Python Console and Run Configurations to do the same but surprisingly that is not the case.

I'm not familiar with the code base IntelliJ developers have to deal with but this seems like a reasonable requirement amendment (or bug fix) to consider.

Anyways, I really like PyCharm overall, it has been making our development more efficient.

Thanks for responding, I guess I'm just as disappointed the "fix" wasn't in the 2019.1 EAP I downloaded.  Hope it makes it by release date.  Otherwise, please consider it for the subsequent release.


Well, this isn't the final fix that I'm offering. I just wanted you to try the workaround to see if it helps and confirm this is the environment activation issue.

We are aware of this issue and are working to provide a proper fix for the stable release.


Hi Pdallair, could you please clarify, do you manually edit the venv activation script to add those paths to `PYTHONPATH`? The original issue is about `conda`, and as far as I can see you are not using it so this is a separate problem.



You are right, I have edited the virtual environment installation's activation script to modify the PYTHONPATH.

You are right, I am not using Conda.  The symptoms are similar in abstract but I suppose it is, in that case, a separate problem.  In that regard, should I open another request or create another poste?


Would be great if you can submit a ticket to our bug tracker: with details about your activation script changes. Are those simple `export PYTHONPATH=...` in `activate`?


Alright, I've created the issue PY-34482.  Sorry about the drama over here. :P

To answer your final question.  Yes it is that simple (see my pastebin of the activation script). 


I was struggling to activate my virtual environment, I got this idea online and decided to apply it, just copy and paste your path to the activation script the .bat script is


It worked and I was able to run server through

python run server


Hello. The problem of python console launched not in venv happens to this day. The problem seems to be with the way, pycharm starts it:

ssh://***/env38/bin/python3.8 -u /***/.pycharm_helpers/pydev/ --mode=server

This causes problems with pyspark. Because it spawns child processes outside venv and gives error:

Exception: Python in worker has different version 2.7 than that in driver 3.7, PySpark cannot run with different minor versions.Please check environment variables PYSPARK_PYTHON and PYSPARK_DRIVER_PYTHON are correctly set.

Python console should FIRST activate venv and then launch ipython.

Is there a way I can configure it in PyCharm?


PyCharm doesn't support virtual environment activation with remote interpreters yet. Please vote for the corresponding feature requests in our issue tracker,


it's so easy to activate virtual env from pycharm 
go to the terminal of your pycharm and write this commands:

1-cd venv/Scripts

2- activate (or activate.bat)

3- cd .. (there is a space between ' cd ' and '  ..  '

4- cd ..

5- be sure to go out of the virtual environment. [ in my case -->  (venv) C:\Users\admin\PycharmProjects\pythonProject1>  ]

6-be sure to install a virtual environment before using this commands 


Good Luck.