How to correctly configure PyCharm ‘Sources Root’ for multiple packages with separate ‘src’ folders?
I’m working on a Python project with multiple packages, each containing its own src folder:
project/
src/
package1/
pyproject.toml
src/
logic/
somestuff.py
mymodel1.py
__init__.py
package2/
pyproject.toml
src/
logic/
somestuff.py
mymodel2.py
__init__.py
The problem is that even when I define each package1 and package2 folders as ‘sources root’, PyCharm seems to confuse the src folders. It appears to be using the src folder from package2 instead of package1. How should I properly configure the Sources Root in PyCharm for this multi-package structure?
Marking both package1 and package2 as sources root creates a conflict because PyCharm adds both directories to PYTHONPATH, causing it to struggle with distinguishing between the nested src folders. To properly configure this multi-package structure, you need to mark the individual src folders as sources roots instead of the parent package directories.
Contents
- Understanding Sources Root Configuration
- Proper Configuration Steps
- Alternative Approaches for Multi-Package Projects
- Troubleshooting Common Issues
- Best Practices for Project Structure
Understanding Sources Root Configuration
When you mark a directory as a sources root in PyCharm, you’re essentially adding that directory to the PYTHONPATH variable. This tells PyCharm that files within this directory and its subfolders can be imported as modules without specifying full paths.
The problem with your current structure is that when you mark both package1 and package2 as sources roots, PyCharm adds both project/src/package1 and project/src/package2 to the Python path. This creates conflicts because:
- Both directories contain
srcsubdirectories - Files like
logic.somestuffbecome ambiguous - PyCharm may default to one over the other, causing the behavior you’re experiencing
As the JetBrains documentation explains, source roots should point to the actual directories containing your Python modules, not their parent containers.
Proper Configuration Steps
Here’s how to correctly configure your multi-package structure:
Method 1: Mark Individual src Folders as Sources Root
-
Open PyCharm and go to File → Settings (or PyCharm → Preferences on macOS)
-
Navigate to Project: [Your Project Name] → Project Structure
-
Configure content roots:
- Ensure the main project directory (
project/) is set as a content root - Remove any duplicate content roots that might cause conflicts
- Ensure the main project directory (
-
Mark src folders as sources roots:
- In the Project Structure window, navigate to
project/src/package1/src - Right-click on this folder and select Mark Directory as → Sources Root
- The folder icon should turn blue
- Repeat for
project/src/package2/src
- In the Project Structure window, navigate to
-
Verify configuration:
- Click Apply and OK
- Check that both
package1/srcandpackage2/srcappear as sources roots in the project structure - Test imports in your Python files to ensure they work correctly
Method 2: Using Project Structure Settings
Alternatively, you can configure this through the Project Structure interface:
- Hold Ctrl and click to select multiple folders (
package1/srcandpackage2/src) - Click the Sources icon (blue folder) in the upper toolbar
- This assigns all selected folders as source roots at once
As Stack Overflow users suggest, this multiple selection approach is efficient for complex project structures.
Alternative Approaches for Multi-Package Projects
Approach 1: Flatten the Structure
Consider restructuring your project to avoid nested src directories:
project/
package1/
pyproject.toml
logic/
somestuff.py
mymodel1.py
__init__.py
package2/
pyproject.toml
logic/
somestuff.py
mymodel2.py
__init__.py
This eliminates the confusion and makes configuration simpler.
Approach 2: Use Content Roots Instead
For true multi-project setups within one workspace:
- Add each package as a separate content root:
- Go to Project Structure → Add Content Root
- Add
project/src/package1andproject/src/package2as separate content roots
- Mark each package’s src folder as a sources root within its content root
This approach is particularly useful for monorepo scenarios.
Approach 3: Configure Virtual Environments
The JetBrains documentation suggests using separate Python interpreters for each package if they have different dependencies:
- Create a virtual environment for each package
- Configure separate Python interpreters in PyCharm
- Set up individual run configurations for each package
Troubleshooting Common Issues
Issue: PyCharm still can’t distinguish between packages
Solution: Check for conflicting import paths. If both packages have identical module names, PyCharm may default to one. Consider:
- Using namespace packages by marking the intermediate directories as namespace packages
- Renaming conflicting modules to be package-specific
- Using absolute imports within each package
Issue: Import errors persist after configuration
Solution: Try these troubleshooting steps:
- Delete .idea folders and reconfigure the project structure
- Verify PYTHONPATH is not set externally and conflicting with your configuration
- Check if packages are properly installed in your Python environment
- Restart PyCharm after making changes to project structure
As one Reddit user discovered, deleting .idea folders can sometimes resolve persistent issues.
Issue: Run configurations use wrong working directory
Solution: Configure working directories for each run configuration:
- Go to Run → Edit Configurations
- For each configuration, set the Working directory to the appropriate package root
- Ensure the Python interpreter is correctly configured for that package
Best Practices for Project Structure
1. Follow PEP 518 Guidelines
For modern Python projects, consider using a src-based layout:
project/
src/
package1/
logic/
somestuff.py
__init__.py
package2/
logic/
somestuff.py
__init__.py
tests/
This structure makes it clearer which directories contain actual source code.
2. Use Consistent Configuration
- Consistent naming: Use the same naming convention across all packages
- Clear separation: Ensure packages have distinct namespaces to avoid conflicts
- Proper init.py: Include init.py files in all package directories
3. Leverage PyCharm’s Advanced Features
- Namespace packages: Mark directories as namespace packages when appropriate
- Resource roots: Use resource roots for non-Python files like images and CSS
- Excluded folders: Mark build directories, .git, and other non-source folders as excluded
The official PyCharm documentation provides detailed guidance on all these features.
4. Document Your Configuration
Keep documentation of your project structure decisions, especially for complex multi-package setups. This helps team members understand the import paths and configuration logic.
Sources
- Configuring Project Structure | PyCharm Documentation
- What does “Mark directory as sources root” really do? - Stack Overflow
- Pycharm cant find modules when in a monorepo project - Reddit
- In PyCharm, how do you add a directory from one project as a source to another project? - Stack Overflow
- How to set up working directory in PyCharm and package import? – JetBrains Support
Conclusion
To correctly configure PyCharm for multiple packages with separate src folders, remember these key points:
- Mark individual src folders as sources roots, not their parent directories
- Avoid nested src structures when possible to prevent confusion
- Use content roots for true multi-project setups within one workspace
- Configure separate Python interpreters if packages have different dependencies
- Document your project structure to maintain consistency across your team
The most straightforward solution for your current structure is to mark project/src/package1/src and project/src/package2/src as sources roots rather than the parent package directories. This tells PyCharm exactly where to find the Python modules for each package, eliminating the confusion you’re experiencing.
For future projects, consider flattening the structure or using a src-based layout as recommended by modern Python development practices.