Differences between Project Dependencies vs Content Roots to access files from another project in PyCharm?

Viewed 31

As far as I understand, a PyCharm project (ProjectA) can access modules/files from another project (ProjectB) in a couple of different ways:

  1. Attach ProjectB to ProjectA using the File>Open>Attach menu. The relationship is managed via the File>Settings>Project:ProjectA>Project Dependencies menu item. OR,

  2. From within ProjectA, via File>Settings>Project:ProjectA>Project Structure>+Add Content Root. This adds ProjectB as a subdirectory in ProjectA.

I have a particular project (module library) that I share between multiple projects. What are the issues to consider in choosing one of the options over the other?

I can't see anything in the documentation (or on SO) that compares the two, so perhaps the options perform quite differently and can't be considered alternatives?

1 Answers

First ask yourself what a Project is in PyCharm?

Basically each Project is an .idea folder that the IDE creates and manages for you:

Project settings

Project settings apply to the current project only. They are stored together with other project files in the .idea directory in the .xml format.

So what's the difference between using "Project Dependencies" and "Content Root" (and what do they have in common?):

1. Using "Project Dependencies":

  1. Attach ProjectB to ProjectA using the File>Open>Attach menu. The relationship is managed via the File>Settings>Project:ProjectA>Project Dependencies menu item.

The first difference is that you'll have a full Project. It will show as a top level item in Project Tool Window; and it will be linked in .idea/modules.xml as:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/.idea/Your_Main_Project.iml" filepath="$PROJECT_DIR$/.idea/Your_Main_Project.iml" />
      <module fileurl="file://$PROJECT_DIR$/../Your_Dependency_project/.idea/Your_Dependency_Project.iml" filepath="$PROJECT_DIR$/../Your_Dependency_Project/.idea/Your_Dependency_Project.iml" />
    </modules>
  </component>
</project>

All the functionalities the IDE makes available for one Project are also available for the other, because both are Projects.

2. Using "Project Structure" > "Add Content Root":

If you add a single path as a "Content Root" directory the IDE will add a single line in the .idea/Your_Main_Project.iml file:

<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
  <component name="NewModuleRootManager">
    <content url="file://$MODULE_DIR$/../Your_Dependency_Project" />
  </component>
</module>

3. What do they have in common?

The only thing both options have in common is adding the selected directory to the PYTHONPATH. If all you're interested in is having the main project find the source files using "Add Content Root" is enough. If on the other hand you want to navigate and have the IDE manage that dependency as a complete project then choose "Project Dependencies".

Related