Configure PyCharm With the Python Virtual Environment
It will show you how to configure PyCharm to work with Python in a virtual environment. Also you will configure the requirements.txt.
Start PyCharm create a new project or clone it from your repository. As an example I created a file first_example.py to write inside the main. I specify that this is only an example and for reasons of brevity does not follow the structure of a Python project.
Install Python
First, make sure Python 3 is installed by running the following command:
If python 2 and 3 is installed, the output will be the installed version of python 2 and 3.

If python is not installed, run the command and choose version 3.6 for example¹:
Create virtualenv with:
It is possible to create virtualenv on the same directory or on a different directory. In this article I use the same directory as the project directory. In my case i call the directory “venv”, but we can use “any_name”:

If all went well you will have the folder venv in your project:

To activate virtual environment lunch:

The fact that there is venv in parenthesis means that all libraries will be installed in the virtual environment and will not “dirty” the global environment. For more information about the virtual environment read this https://docs.python.org/3/tutorial/venv.html.
Python Interpreter
If you can’t find the python interpreter in the previous window then you have to create or add it. Just go to File →Settings →Project →Python interpreter →Add.

In the Interpreter box is present the path of our virtual environment:

Click Ok and then on Apply, then we will see all the libraries installed in our virtual environment.

Executing a program in the virtual environment
Let’s consider the example first_example, go to Add Configuration…

A window will open. Click on the “+” in the upper left corner. Now we can configure our Python program ready to run. Enter the “Name” (first_example), set “Script path” to the file where the main is located. Then we should find the “Python interpreter” already configured, in any case, having added it we will find it in the dropdown menu. Note that the interpreter path is the one related to the virtual environment created!

Lunch simple program
Let’s consider the following code:

requirements.txt
If you share a project with other users using a build system or plan to copy the project to another location where an environment is to be restored, you must specify the external packages required by the project. The recommended approach is to use a requirements.txt file, which contains a list of pip commands and allows you to install the required versions of dependent packages.
We assume to install Pandas (data analysis and manipulation tool), we lunch:
Getting Started with PyCharm¶
This tutorial assumes you are familiar with the process of building Mantid (with separate source and build directories inside a root directory), and that you have built a working version. If you are unclear about this see here .
Setting up PyCharm on Windows¶
Once PyCharm is open, set up the project. Go to File->Open and select the root directory in which both your source and build directories reside.
Go to File->Settings , then under Project you will set two sub-menus Project Interpreter and Project Structure . The interpreter defines the python executable that will be used to run your code, and the structure menu allows you to decide which folders within the project to include and index.
In the Project Interpreter sub menu, at the top select the options button and click Add. , a new window should appear titled “Add Python Interpreter”. In the menu on the left, select “System Interpreter” (a version of Python with all the correct variables set already exists within Mantid). Click on the . to open a file browser, and navigate to;
This is the interpreter, so select “Ok” and apply the changes. This should bring up a list of all the packages associated to the interpreter. There should be many packages, however you should not see PyQt (but instead QtPy).
In the Project Structure sub menu you should see your root directory with the source/build directories both visible (if not, add them). The folder structure should be present in the centre of the window allowing you to mark folders orange (excluded) or blue (source). Source directories will be searched for python code.
Within the source directory add the following to your sources:
If you are writing scripts in any other directories, you can also mark them as sources. This helps PyCharm give better auto-complete and import suggestions during development.
Additionally, in the Mantid build directory add the following as source folders:
here we are setting up PyCharm for the Debug build, you would use /bin/Release instead if you are building mantid in release mode.
The environment needs to be set up before running the configuration. Follow the instructions below to use either the EnvFile plugin (recommended) or manual path setup.
NOTE : In some cases, imports in the code will still be highlighted red when they come from folders within the script/ folder, or from other folders entirely. To fix this simply add the relevant folder that contains the module you are importing in the same fashion as step 3 above.
Running Files in the Debugger with EnvFile extension¶
Running python code from within PyCharm which depends on the python API, or PyQt for example requires one extra step. Because the source root labelling from the previous section only affects PyCharm searching and not the run configuration, before running the file we must set up the run configuration correctly.
- Install the EnvFile plugin by Borys Pierov. The plugin can be installed in multiple ways:
- Open Settings(CTRL + SHIFT + S), to go Plugins and search for EnvFile . Install and restart PyCharm.
- Go to the plugin’s webpage, download and install it.
For running the Workbench continue onto Workbench , and follow the instructions to set up the Script Path and Working Directory.
Advantages of this approach:
- You can have multiple instances of PyCharm running with environment configuration for separate repositories. This is otherwise not possible, as all PyCharm instances seem to share a parent process and environment. (as is the case of 11/01/2019, it might change in the future)
- This makes possible switching projects for multiple repositories via the File > Open Recent … menu, as when the new project is opened its environment won’t be poluted with environment variables from the last one.
- This can cause errors when the external dependencies aren’t quite the same between all the repositories, as some packages might be missing, or be different versions.
- Additional setup for each configuration necessary. Thankfully, if the template is edited to have the correct EnvFile setup, all copies of it will have it too. Copying an already existing configuration also copies the EnvFile setup.
Running Files in the Debugger without EnvFile extension¶
This can be done in two ways:
Open PyCharm using pycharm.bat which can be found in the build directory (this sets some additional environment variables compared with simply opening PyCharm directly).
- This is preferred if you only have 1 repository with which PyCharm is used. If you need to use PyCharm on multiple repositories, it is recommended that you use the EnvFile extension.
To edit the configurations go to Run->Run. and select Edit Configurations . This should open up a sub window. Hit the green + in the top left to create a new configuration and name it. In order to tell PyCharm where to look for python modules and libraries we need to add some folders to the PATH environment variable. Click on the . next to the Environment Variables box, and hit the + icon. In the Name column enter “PATH”, in the value column enter the following;
The semi-colon delimited list of paths should end in ;%PATH% so that we prepend to the existing list of paths rather than overwriting them.
You should now be able to run and debug the scripts using the newly created configuration, by adding the full path of the file in the Script path box at the top of the configuration window.
As an example, create a new file in <Mantid Source Directory>/scripts/ called test.py . Copy into it the Python code below.
Testing using PyQt¶
To test that the above instructions have worked, you can simply create a new Python file with the following content (for PyQt5)
Local Debugging of Unit Tests with PyCharm¶
This does not require a PyCharm Professional license for debugging, but requires additional setup for running unit tests.
- Go to your Run/Debug Configurations.
- Open Templates > Python tests > Unittests configuration.
- Set the working directory to <Mantid Build Dir>/bin/Debug , for a Debug build, or <Mantid Build Dir>/bin/Release for a Release build.
- Add the EnvFile to the Unittests configuration, instructions in Running Files in the Debugger with EnvFile extension .
- You should now be able to click the Run/Debug icons next to each unit test method or class to run/debug them.
Remote Debugging of Unit Tests with PyCharm¶
This requires a PyCharm Professional license for the Remote Debugging feature.
This approach can be used to debug unit tests. However, as the required package pydevd is not shipped with Mantid, we need to manually add it at runtime. This can be done by appending a directory that contains the installed pydevd package on the PYTHONPATH . The following code does so at runtime:
A Remote Debugging configration needs to be setup to use the 44444 port (can be changed, but it needs to be reflected in the code), and running before the tests are run!
The pydevd package does not have to be installed on Python 2. As of 12/11/2018 installing pydevd on a separate installation with Python 3.7, and adding the code above successfully connects.
Setting up PyCharm on Linux¶
Use the native python interpreter ( /usr/bin/python2.7 ) rather than from <Mantid Source Directory>/external/src/ThirdParty/lib/python2.7/python.exe
In the Project Structure sub menu you should see your root directory with the source/build directories both visible (if not, add them). The folder structure should be present in the centre of the window allowing you to mark folders orange (excluded) or blue (source). Source directories will be searched for python code.
Within the source directory add the following to your sources:
If you are writing scripts in any other directories, you can also mark them as sources. This helps PyCharm give better auto-complete and import suggestions during development.
Additionally, in the Mantid build directory add the following as source folders:
It is recommended that you add the whole build folder to excluded . This will not interfere with the bin directory, inside the build, being used as a source folder. It will just limit the scope that PyCharm searches for files, classes, etc.
Go to Run->Run… and select Edit Configurations. Go to Templates > Python. Make <Mantid Build Directory>/bin; the Working Directory . This will then be used for all Python configurations you make.
Useful Plugins¶
You can install non-default plugins by pressing Ctrl+Alt+S to open the Settings/Preferences dialog and then going to Plugins. From here you can manage plugins, or add new ones by clicking Browse repositories.
The following non-default plugins are things our team has found useful for Mantid development:
- Markdown support — Side by side rendering of markdown documents such as«.md« , .rst (requires Graphviz to show graphs in preview)
- dotplugin — Syntax highlighting for DOT
- BashSupport — Syntax highlighting for BASH scripts
- CMD Support — Syntax highlighting for .BAT
Add directory to Python path in PyCharm?
I want to be able to use the paraview.simple library in PyCharm. I already have paraview installed in my computer. This package cannot be installed with pip and there are no .whl files as far as I can tell. The website docs recommend that the directory containing all the python files be added to PYTHONPATH.
How do I add the relevant folder in PYTHONPATH on my PyCharm session, and keep it there by default, such that when I close out and reopen the paraview.simple library is still available?
5 Answers 5
You can add custom paths this way.
- Go to File->Settings->project Interpreter
- In the Project-Interpreter field, click the down facing arrow and select «show All»
- In that Menu, highlight your interpreter and then in the right menu, select the button «Show paths for the selected interpreter» (this is the last button)
- click the plus symbol to add your path
Adding interpreter paths in PyCharm:
- Project Settings/Project Interpreter: select «settings» icon
- Project Interpreters: select «tree» icon
- Interpreter Paths: select «plus» icon

In PyCharm version 2020.3.1 use the following workflow instead (see official docs):
Ctrl+Alt+S — open Settings.
You will see project folder structure. Mark subfolders as Sources or Excluded