How to activate your virtual environment in the PyCharm terminal

Jan 21, 2015

PyCharm is an incredible Python IDE made by JetBrains, and it’s my go-to editor for all Django development. If you do Django, you should do PyCharm as well. And, if you do Django, you should also be using virtual environments to help keep your python projects separated nicely. I personally use the popular VirtualEnvWrapper and it’s a great solution to help manage your virtual environments.

So, to recap this crazy-long post so far, PyCharm = Awesome & VirtualEnvWrapper = Awesome. They work well together too! PyCharm has built-in support for virtual environments and even allows you to manage and install new PIP packages directly into your project’s virtual environment. However, one thing that it doesn’t tie together nicely is the use of the built-in terminal with the virtual environment you have associated with the opened project.

With VirtualEnvWrapper, you jump into your project environment by typing:

The workon command sets the environment so now anything you do with Python is looking at the Python and all installed packages that are in that environment only. However, this environment isn’t activated by default when you launch the PyCharm terminal. Actually, depending on your settings, the workon command may not even be found anyway. Here’s the solution I use to both of those issues:

Open your PyCharm project and create a file name .pycharm-bash in the root of your project. Add this to the file and save it:

Now this is assuming your project name is mywickedpythonproject and that you keep your virtual environments in your home directory in a folder named .virtualenvs (which I think is the default anyway). So change those if needed.

Now in the PyCharm settings go to Terminal in the Project Settings section. The shell path setting should already have your bash location, so just append –rcfile .pycharm-bash to that field. My path looks like this after: /bin/bash –rcfile .pycharm-bash

Now, just Apply the settings and restart PyCharm and your terminal will already be set to your virtual environment and all of the VirtualEnvWrapper commands work as expected.

Leave a Comment

Your email address will not be published. Required fields are marked *