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)
- You've installed e.g. Miniconda on your machine
- 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 <<<
- 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 - With conda activated in the terminal you can run e.g.
conda
command as it was added to thePATH
environment variable
$ which conda
/home/parallels/miniconda3/bin/conda
python
command in the terminal will invoke Python from Miniconda as again thePATH
environment variable was patched
$ which python
/home/parallels/miniconda3/bin/python
- Now let's open some project in PyCharm with a virtual environment set as a project interpreter
- And open a built-in PyCharm terminal
- Bash source
~/.bashrc
, activates conda and you get(conda)
- PyCharm sources
activate
script of the virtual environment and you get(venv) (conda)
- 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
Is There a Matplotlib Equivalent of Matlab's Datacursormode
How to Make Image/Images Disappear in Pygame
How to Dynamically Compose an or Query Filter in Django
How to Generate a Random Number with a Specific Amount of Digits
Cleanest Way to Get Last Item from Python Iterator
Function Changes List Values and Not Variable Values in Python
How to Create PDF Files in Python
How to Filter Rows in Pandas by Regex
Print to Standard Printer from Python
How to Put Multiple Statements in One Line
Problem with Multi Threaded Python App and Socket Connections
How to Create Animated Sprites Using Sprite Sheets in Pygame
Django Db Settings 'Improperly Configured' Error
Pip Cannot Uninstall <Package>: "It Is a Distutils Installed Project"
Why Does Str(Float) Return More Digits in Python 3 Than Python 2