In PyCharm, how do you add a directory from one project as a source to another project?
Solution 1
You have to tell Pycharm to use Project A as another "Content Root" for Project B.
- From the upper bar, go to File -> Settings... -> Project: [yourprojectnamehere] -> Project Structure.
- Click on
+ Add Content Root
at the right panel - Select the Project A folder from the popup named
Select content root directory
- Click OK
Now, you'll see Project A is listed at the right panel.
- Click on Project A from the right panel
- Now, the content of project A is listed. Select the ones you want to be included as source. You can hold
Ctrl
and clickleft mouse button
to select multiple entities. - Click
Sources
from the upper bar to assign the selected entities as Source.
Now you should see Source Folders at the right panel.
UPDATE: As of at least Pycharm 2019.1.3, this seems to have changed, to be simpler thankfully.
- From the upper bar, go to File -> Settings... -> Project: [yourprojectnamehere] -> Project Dependencies.
- Select your project, and then click the checkboxes for each other project it's dependent on.
Solution 2
Pycharm 2020.2 - just in your project, File -> Open select project whitch you want attach. And choose 'Attach' in Open project window.
CivFan
Updated on August 05, 2022Comments
-
CivFan almost 2 years
I have several python projects started from git repos, all related to each other, that are all open in one PyCharm window.
I have python code in one project, call it project B, that imports python packages from project A, but PyCharm can't find the source.
I've marked the directories with python packages in project A as source directories in PyCharm, and indeed other code in project A can lookup these python packages. But these source directories don't appear to be part of the lookup table for other projects in the same window.
Is there any way in PyCharm to make one project recognize directories from another project as a source directory?
-
CivFan almost 8 yearsThis is 95% working for me. The one problem I had was that I couldn't add the highest level root directory of the other project because
"Two modules in a project cannot share the same content root."
I got around this by just adding a sub-directory, which is good enough for my current needs. -
Yibin Lin almost 6 yearsIn Mac OS: it seems to be in Pycharm (menu) > Preference > Project Structure
-
Charlie Parker over 3 years"File -> Settings... -> Project: [yourprojectnamehere] -> Project Structure." does not exist
-
PhiM over 3 yearsWith PyCharm 2020.2, I followed n jetbrains.com/help/pycharm/project-dependencies.html to create a dependency. However, I find the result unintuitive, at best, as PyCharm always shows the two projects in the same window. It seems to create more than a dependency. The settings of project B somehow appear to also depends on the settings of project A, which is very strange. Hence I deleted my answer on how to do this, because there are still things I do not understand.
-
Scott Skiles about 3 yearsVery nice. Worked for me. One of the things about PyCharm that is tricky is how much the UI changes over time. I suppose it is good as this is an improvement but it makes the answers above obsolete.