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.

Tip

More of a visual learner? Sign in to Anaconda Cloud to follow along with our Create a simple Python program with PyCharm!

Configuring a conda environment in PyCharm#

Python projects often require specific 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 for a new project, or switch conda environments within a project that already exists.

Creating a new conda environment from a PyCharm project#

PyCharm creates a basic conda environment for you (with a selected Python version) as part of the initial project setup, and links your PyCharm project to that environment.

  1. After opening PyCharm, click New Project.

  2. In the New Project screen, create the project name and its location.

  3. To have your project under Git version control, select Create Git Repository.

  4. To add the main.py file to your project, select Create welcome script.

    Note

    The welcome script file contains a very simple Python code sample and can be a starting point for your project.

  5. Select Custom environment, then select Generate New.

  6. From the Type dropdown, select Conda.

  7. From the Python version dropdown, select the Python version you want.

  8. Create your environment name.

  9. Typically, PyCharm will detect the conda installation. If not, you can specify the conda location in the Path to conda field.

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

  11. You can check that the conda environment was created by using conda info --envs in your Terminal or Anaconda Prompt:

    conda info --envs
    # 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.

  2. In the New Project screen, create the project name and its location.

  3. Select Custom environment, then Select existing.

  4. From the Type dropdown, select Conda.

  5. Typically, PyCharm will detect the conda installation. If not, you can specify the conda location in the Path to conda field.

  6. Select the environment from the list of environments.

    Note

    If you specified the path to conda manually, you may need to select Reload environments.

  7. Click Create to finish creating your new project.

Switching environments within a PyCharm Project#

If you want to change the conda environment associated with an ongoing project, update the project environment preferences using the following instructions:

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

  2. Click the gear in the top-right corner of the screen, then select Settings.

  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.

    To create a new environment, follow these steps:

    1. Select Create new environment.

    2. Create an environment name.

    3. Select the Python version.

    To use an existing environment, follow these steps:

    1. Select Use existing environment.

    2. Select an environment from the Use existing environment dropdown.

  6. 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.

# Replace <MY_ENV> with the name of the your environment
conda activate <MY_ENV>

# Replace <CHANNEL_NAME> with the name of the channel you are trying to source from
# Replace <PKG_NAME> with the name of the package you are trying to install
conda install -c <CHANNEL_NAME> <PKG_NAME>

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