Search code examples
pycharmvirtualenv

Pycharm showing venv and base in the terminal


I am curious about my terminal inside the Pycharm after I have established the virtual environment. The screenshot is as follow. Is that normal to have (venv) and (base) in the front? Thanks.

enter image description here


Solution

  • My bet is (venv) comes from PyCharm auto-activating the virtual environment you have configured as a project interpreter and (base) is likely conda base environment activated in ~/.bashrc (or similar place). I guess in the terminal outside of PyCharm you only have (base).

    Details

    Let me explain what is happening in more detail (Ubuntu and Bash as an example)

    1. You've installed e.g. Miniconda on your machine
    2. Conda installer patched ~/.bashrc, e.g. cat ~/.bashrc
    # >>> conda initialize >>>
    # !! Contents within this block are managed by 'conda init' !!
    __conda_setup="$('/home/parallels/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
        eval "$__conda_setup"
    else
        if [ -f "/home/parallels/miniconda3/etc/profile.d/conda.sh" ]; then
            . "/home/parallels/miniconda3/etc/profile.d/conda.sh"
        else
            export PATH="/home/parallels/miniconda3/bin:$PATH"
        fi
    fi
    unset __conda_setup
    # <<< conda initialize <<<
    
    1. Now each time you run a terminal (either a PyChrarm-built-in one or a system one) you will get (conda) as Bash sources ~/.bashrc to start a new shell session
    2. With conda activated in the terminal you can run e.g. conda command as it was added to the PATH environment variable
    $ which conda
    /home/parallels/miniconda3/bin/conda
    
    1. python command in the terminal will invoke Python from Miniconda as again the PATH environment variable was patched
    $ which python
    /home/parallels/miniconda3/bin/python
    
    1. Now let's open some project in PyCharm with a virtual environment set as a project interpreter
    2. And open a built-in PyCharm terminal
    3. Bash source ~/.bashrc, activates conda and you get (conda)
    4. PyCharm sources activate script of the virtual environment and you get (venv) (conda)
    5. As the venv was activated last it overwrote conda activation in some places, e.g.
    $ which python
    /home/parallels/.virtualenvs/venv/bin/python
    

    The question is - should you care?

    Probably not, apart from visual clutter, it doesn't affect you much. There are some environment variables left after conda activation which were not overridden by the venv activation but they are unlikely to affect you.

    You can remove conda activation logic from ~/.bashrc if (base) bothers you too much. You can always activate it back manually. I believe it only make sense to leave the activation logic in place if you work with conda a lot in the terminal and want to save time re-activating it manually.