Search code examples
pythonpycharmintellij-14

Within a larger PyCharm or IntelliJ project is there support for subdirectories with their own virtualenvs?


Have been putting together applications that include their own infrastructure/deployment code using IntelliJ as my IDE. The total project as checked into Git includes several independent python scripts contained in sub directories under my project (including their package information and supporting python files). My IAC/deployment code takes care of making sure these scripts have a virtualenv with desired packages when on the infrastructure it is deployed to. All of this is working fine, except for when I try to get IntelliJ to understand the python subdirectory environments.

When I open the whole project tree as an IntelliJ project, there seems to be no way to explain to IntelliJ that some of the sub directories should be viewed as their own python project, such that they can have have their own virtualenv, packages, and base import directory understood. Being that IntelliJ doesn't understand these things, it sees my imports in these scripts as broken and I can't jump around in code etc.

As a work-a-round I have been sometimes just opening the python sub directories as their own IntelliJ project so that IntelliJ can understand them. But this is not ideal, as I have to have several different IntelliJ instances for the same larger project.

So now perhaps my question makes more sense, and I will restate it. Is there some way I am missing where I could have one instance of IntelliJ correctly understand the whole project including that some subdirectories are like python sub-projects with their own virtual env and packages etc?


Solution

  • A PyCharm/IntelliJ "project" can have "sub-projects". You access this feature by "attaching" one project to another. Open a project. Then open a second project. You'll get a dialog asking how you want to open the project, and one option will be "Attach". If you select this option, the second project is added to the already open window and your window now contains two somewhat independent projects.

    If you now bring up the Preferences panel, and select "Project: xxx -> Project Interpreter", you should see that there are two projects listed in the central section of the panel. You can select each of these and configure the interpreter/environment settings for each project independently. You can also set up the Project Structure and Project Dependencies for each sub-project independently in this same way. This, I believe, is what you're asking how to do.

    To illustrate, here's a screenshot showing just what this looks like, here with three projects being managed in a single window/main project:

    PyCharm Preferences Panel

    Per the "Project: if-lab" section heading in the above screenshot, PyCharm/IntelliJ seems to have a notion that one of the projects is the primary project. You can see this in the Preferences pane, where the per-project settings are listed under a heading like "Project: xxx", where "xxx" is the name of the primary project. I'm not sure just how the notion of one project being the primary comes into play.

    Here is a page from the PyCharm documentation that explains some of this:

    https://www.jetbrains.com/help/pycharm/opening-multiple-projects.html

    The following section of that page explains how sub-projects interact with the primary project:

    A newly opened project shares the same window as the already opened one. The project that has already been opened, is considered the primary project, and is always shown first in the Project tool window. All the other projects are added to the primary project.

    You can import classes and methods from dependent projects. Use the Dependencies project settings to configure this behavior.

    Some settings (such as Django, Buildout, Google App Engine, template languages, Python interpreters, content roots) can be configured separately for each project.

    You can also configure different execution environments independent of the project's environment by creating multiple Run/Debug Configurations. These settings control what happens at execution time, but do not affect such things as syntax checking and highlighting, code completion, etc.