Using PyCharm#

Note

These instructions refer to Anaconda Distribution exclusively, but will also work with Miniconda.

PyCharm is an IDE that integrates with IPython Notebook, has an interactive Python console, and supports Anaconda as well as multiple scientific packages. PyCharm also supports creating virtual environments for Python with conda. This topic will cover the following:

Configuring a conda environment in PyCharm#

Many times, Python projects will require very different setups, with access to different versions of Python and different packages and their dependencies. Conda environments can be used with PyCharm projects to ensure that each of your projects are being built and run to the exact Python specifications that they require.

You can create a new conda environment when you create a new Python project in PyCharm, configure an existing conda environment to a new project, or switch conda environments within a project that already exists.

Creating a new conda environment from a PyCharm project#

PyCharm will create a basic conda environment for you (with a selected Python version) as part of an initial project setup, and will link your PyCharm project to that environment.

  1. After opening PyCharm, click New Project.

    1. Use Location to change your project’s name and file location, if necessary.

    2. Expand Python Interpreter, if necessary.

    3. Your New environment should use Conda instead of the default Virtualenv.

    4. Specify the location and name of the new conda environment in Location.

      Note

      The directory where the new conda environment is located must be empty.

    5. Select the correct Python version.

    6. Specify the location of the Conda executable file.

    7. Select whether to Make available to all projects.

  2. Click Create to create the PyCharm project and conda environment.

  3. You can check that the conda environment was created by using conda env list in your Terminal or Anaconda Prompt:

    (base) C:\Users\doc> conda env list
    # conda environments:
    #
    base              *   C:\Users\doc\anaconda3
    pythonProject1        C:\Users\doc\anaconda3\envs\pythonProject1
    

    Or you can look at the Environments tab in Navigator to see the same information:

Configuring a PyCharm Project with an existing conda environment#

Let’s say you’ve already created a conda environment that you know will be perfect for your latest PyCharm project. You can easily link an existing conda environment to a project in PyCharm using the following instructions:

  1. After opening PyCharm, click New Project.

    1. Use Location to change your project’s name and file location, if necessary.

    2. Expand Python Interpreter, if necessary.

    3. Choose Previously configured interpreter.

    4. Click the three dots to add a Python Interpreter.

    5. Choose Conda Environment.

    6. If the link to the correct environment’s bin folder does not appear in the Interpreter dropdown, you can easily find the file path using your Terminal or Anaconda Prompt:

      • Open the correct environment using conda activate ENV-NAME, replacing ENV-NAME with the name of your environment.

        (base) C:\Users\doc> conda activate my_env
        (my_env) C:\Users\doc>
        
      • Then, use either where.exe python (Windows) or which python (macOS or Linux) to determine the file path location of that environment’s Python installation.

        (my_env) C:\Users\doc> where.exe python
        C:\Users\doc\anaconda3\envs\my_env\python.exe
        
    7. Select the three dots beside Interpreter and copy the path. If that instance of Python exists, the file tree will open to its file directory and select it.

    8. If necessary, change the file path to your conda installation and choose whether to make this environment available to all PyCharm projects.

  2. Click OK and then Create to finish creating your new project.

Switching environments within a PyCharm Project#

Sometimes you may want to change the conda environment associated with an ongoing project. You can use the following instructions to change your project environment preferences:

  1. Open the PyCharm project associated with the conda environment you want to change.

  2. Open the Settings/Preferences dialog. In Windows, go to File > Settings. In macOS, go to PyCharm > Preferences.

  3. Select Project: <project name>, then Project Interpreter.

  4. Select a new Python Interpreter by clicking the gear and then clicking Add.

  5. Select Conda Environment.

  6. To create a new environment:

    1. Select New environment.

    2. Specify the location and name of the new conda environment in Location.

      Note

      The directory where the new conda environment should be located must be empty.

    3. Select the correct Python version.

    4. Specify the location of the Conda executable file.

    5. Select whether to Make available to all projects.

  7. To use an existing environment:

    1. Select Existing environment.

    2. Specify the required interpreter. For more information on finding the correct environment, see Configuring a PyCharm Project with an existing conda environment.

    3. Select whether to Make available to all projects.

  8. Click OK to finish changing your PyCharm project’s environment.

Adding a package to a project#

If you’ve added a package to your PyCharm project that is not within the standard Python library, you can add it to your project’s conda environment with PyCharm.

The project in this example uses the flask package.

You can see that the package import is red underlined in the code. If you hover over one of them, PyCharm tells you that the reference to flask is unresolved. That means that the package is not available to the program and needs to be installed.

  1. Click Install package flask in the popup to install flask to the environment you currently have connected to your project.

  2. After flask is installed, it will be displayed in your project’s python packages. Click Python Packages and search for “flask” to view newly installed package.

Adding a repository to a project#

Sometimes packages you’re using in your PyCharm project won’t be available in any Anaconda default channels. To add a new repository to your PyCharm project, use the Python Packages tool window.

  1. Go to View > Tool Windows > Python Packages.

  2. Click the gear next to the search bar.

  3. Click +.

  4. Enter the name of the repository.

  5. Enter the repository URL.

  6. If the repository is local, choose “None” for Authorization. Otherwise, choose “Basic HTTP” and enter your username and password for the repository.

For repositories like conda-forge, the easiest way to install packages is to use your terminal/Anaconda Prompt. Activate your project’s environment and install the package.

(base) C:\Users\doc> conda activate my_env
(my_env) C:\Users\doc> conda install -c conda-forge PKG-NAME

Replace PKG-NAME with the name of the package you are trying to install. Once installation is complete, you will see the package listed in the package list for the selected conda environment for your PyCharm project.

For more information on adding repositories to your PyCharm project’s conda environment, see the PyCharm documentation.