Activating virtual environment in Pycharm python console

Answered

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!
 
 
 
18 comments
Comment actions Permalink

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.

0
Comment actions Permalink

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!
Elena


0
Comment actions Permalink

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

0
Comment actions Permalink

Thanks!

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

Thanks!! 

Elena 

0
Comment actions Permalink

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

conda deactivate
conda activate <venv>
0
Comment actions Permalink

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

Thank you!

0
Comment actions Permalink

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


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()
    },
})

```

0
Comment actions Permalink

Here is the output:

{'os.environ': {'BASH_FUNC_module%%': '() { eval `/usr/bin/modulecmd bash $*`\n}',
'CLUTTER_IM_MODULE': 'xim',
'COMPIZ_BIN_PATH': ['/usr/bin/'],
'COMPIZ_CONFIG_PROFILE': 'ubuntu',
'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',
'GNOME_KEYRING_CONTROL': '',
'GNOME_KEYRING_PID': '',
'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',
'IM_CONFIG_PHASE': '1',
'INSTANCE': '',
'IPYTHONENABLE': 'True',
'JOB': 'unity-settings-daemon',
'LANG': 'en_GB.UTF-8',
'LANGUAGE': 'en_GB:en',
'LD_LIBRARY_PATH': ['.', ''],
'LOADEDMODULES': '',
'LOGNAME': 'elena',
'MANDATORY_PATH': ['/usr/share/gconf/ubuntu.mandatory.path'],
'MODULEPATH': ['/etc/environment-modules/modules',
'/usr/share/modules/versions',
'/usr/Modules/$MODULE_VERSION/modulefiles',
'/usr/share/modules/modulefiles',
'/opt/Modules/modulefiles'],
'MODULESHOME': '/usr/share/modules',
'MODULE_VERSION': '3.2.10',
'MODULE_VERSION_STACK': '3.2.10',
'NLSPATH': ['/usr/dt/lib/nls/msg/%L/%N.cat'],
'PATH': ['/nfs/nhome/live/elena/anaconda/bin',
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/usr/games',
'/usr/local/games',
'/snap/bin'],
'PWD': '/nfs/nhome/live/elena/pycharm-community-2016.2.3/bin',
'PYCHARM_HOSTED': '1',
'PYTHONDONTWRITEBYTECODE': '1',
'PYTHONIOENCODING': 'UTF-8',
'PYTHONPATH': ['/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis',
'/nfs/nhome/live/elena/pycharm-community-2016.2.3/helpers/pydev'],
'PYTHONUNBUFFERED': '1',
'QT4_IM_MODULE': 'xim',
'QT_ACCESSIBILITY': '1',
'QT_IM_MODULE': 'ibus',
'QT_LINUX_ACCESSIBILITY_ALWAYS_ON': '1',
'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_INSTANCE': '',
'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_CURRENT_DESKTOP': 'Unity',
'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_DESKTOP': 'ubuntu',
'XDG_SESSION_ID': 'c2',
'XDG_SESSION_PATH': ['/org/freedesktop/DisplayManager/Session0'],
'XDG_SESSION_TYPE': 'x11',
'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',
'/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis',
'/nfs/nhome/live/elena/pycharm-community-2016.2.3/helpers/pydev',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python27.zip',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/plat-linux2',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-tk',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-old',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/lib-dynload',
'/nfs/nhome/live/elena/.local/lib/python2.7/site-packages',
'/nfs/nhome/live/elena/anaconda/envs/without_stan/lib/python2.7/site-packages',
'/nfs/nhome/live/elena/attrib_experiment_analysis/preliminary_analysis'],
'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)}

0
Comment actions Permalink

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.

0
Comment actions Permalink

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

0
Comment actions Permalink

Hello,

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.

0
Comment actions Permalink

@Pdallair

Can you try the workaround from https://youtrack.jetbrains.com/issue/PY-27234 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.
0
Comment actions Permalink

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.

1
Comment actions Permalink

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.

0
Comment actions Permalink

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.

0
Comment actions Permalink

Pavel,

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?

0
Comment actions Permalink

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

0
Comment actions Permalink

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). 

0

Please sign in to leave a comment.