PyCharm support for Monorepo
Hi,
I have a microservices Python project that is configured as a mono repo (using PDM).
It contains several projects that standalone with their own dockerfiles, share a message queue, filestore, etc.
There is 2 common code packages.
Each project has its own virtual environment dir (in the project via .venv dir).
I'd like to be able to:
A) Have the environment change based on what services code I am opening (currently I have to open each sub-project one by one to have Pycharm detect)
B) When control-clicking code that comes from a “common_stuff” library, have it open the correct project location instead of the .venv location (PDM is configured as having it with a dependecy like `"common_stuff @ file:///${PROJECT_ROOT}/../../packages/common_stuff"`)
How can I achieve this?
Thanks!
Note: I wouldnt mind having to change some files inside `.idea` dir if absolutely have to (could potentially write a script to auto-configure those, or even git commit them). For example “hack” the detected .venv dir list to redirect to the correct location.
Here is a visual aid for the project dir structure (simplified out irrelevant details):
- packages
-- common_stuff
-- common_stuff2
- projects
-- api (rest api that feeds interface)
-- dashboard (web interface)
-- document_processor (data in, docs out)
-- stats_processor (data in, data out)
-- etc (5 or so more)
- scripts
- test_payloads
Makefile
docker-compose.yml (defines services and relationships but imports their dockerfiles from inside each project folder)
README.md
Please sign in to leave a comment.
Currently, you can organize such project with two options:
1. Attach each element of this repository as a project; each attached project can have its own Python interpreter.
2. Open the repo as a single project, add several Python interpreters to the project, and assign the required ones in Run configurations for particular scripts.
Also, here is a feature request for similar functionality: https://youtrack.jetbrains.com/issue/PY-48076/Multiple-interpreters-in-same-mono-repo. Please vote for it to raise its relevance and receive updates regarding the issue's status.
Hi,
Does method 1 work with the new UI? Cant seem to figure it out.
I have had some success defining each project as “Mark Directory as → Sources Root”, in order to be able to get intelisense to work and not go into the “.venv” dir. Sometimes it stops working after restarting the editor for example.
I dont actually run things from the editor except for tests, but method 2 for tests seems to work.
Just adds a bit more trouble than needed
I'm using multiple projects in a monorepo and correctly defining all the virtual environments for each project.
I've just started noticing that with this setup I open pycharm and if I change to a new folder it's starting to detect wrong/incorrect folders. the console will load into the wrong env for the folder selected etc. Is there any way to control this?
within settings project → python interpreter I have 8 different projects defined all with the correct python environment selected.
within console → python console, I have 8 different projects with 8 different and correct environments defined.
behaviour that i expect is if i highlight one of the sub-projects and select python console, the console will be loaded with that projects .venv/interpreter loaded.
Hi dogfish182 , I am wondering, which version is in use? I haven't managed to reproduce this behavior on the recent 2024.3 release version of PyCharm:
I think i figured it out, (thanks for screen capture Mikhael).
I was not correctly closing the python console each time I was done with it, rather just minimizing it. As long as you take care to close the console and correctly highlight a project it works OK.
FYI:
At the date of me posting this there is still no proper support for this kind of monorepo/multi project setup in PyCharm (latest version: 2025.3.1.1).
There is a feature request for this though:
https://youtrack.jetbrains.com/issue/PY-66254/Activate-interpreter-venv-on-a-per-file-folder-basis-in-Pycharm
Hey everyone, we're currently working to implement multi project setup for the 2026.1 version - if any changes happen, I will let you know!
Thank you for understanding