How to Activate a Virtualenv Inside Pycharm's Terminal

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

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

Pycharm showing venv and base in the terminal

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.

activate virtualenv in git bash using pycharm on windows

There is a python package called pipenv.

It can be installed easily by entering pip install pipenv command. Then start using it by executing the following command:

pipenv shell

It automatically creates a new virtual environment.

Freezing the packages is as simple as:

pipenv lock -r > requirements.txt

PyCharm virtual environment is not automatically activated for zsh terminal, MacOS

I have solved this problem with an alternative method found in: https://stackoverflow.com/a/55658404/15416614

Although this question is originally asking about the Windows environment and it is in 4 years ago, there is an answer from 'Ethan Yanjia Li' that can still solve my current problem.

This seems to be a PyCharm bug that the 'Activate virtualenv' option is not working sometimes. It has been fixed before but reappears in some situations, according to: https://youtrack.jetbrains.com/issue/PY-23417.

PyCharm - venv is not getting activated

Pycharm automatically activates the virtual environment fo your project, see the (venv) in Pycharm terminal before the folder information.

How to activate venv which is created by PyCharm via shell?

If you want to activate your virtual env, go to you Project-dir using cmd.

From there type:

venv\Scripts\activate.bat

Your virtual env python interpreter should be added at beginning of prompt, like this:

(venv) C:\Users\my-name>

Run your some.py file using:

(venv) C:\Users\my-name>python some.py

If you are using shell, for example git-bash or something else, use:

source venv\Scripts\activate

For more info, please read virtualenv userguide



Related Topics



Leave a reply



Submit