Marking Directory as a python source root Follow
In PyCharm, we can mark a directory as a python source root via "Mark Directory As" in the project pane.
Can I do the same in CLion? I am working in a c++/python hybrid project, and the lack of this option is giving me headaches as the IDE is not handling my python imports well. I need multiple nested directories in my project to be marked as python source roots.
I am using CLion 2022.2.3 on Ubuntu 22.04.1 LTS 64-bit.
Please sign in to leave a comment.
Yes it's possible to do it in CLion. (Spent a few hours today to remember how to do it, since it seems almost impossible to find).
After configuring or choosing python interpreter, use the "Mark directory as" on the directory you want as python root, but choose "Project sources and headers" not "Python namespace package".
Now python inspections and so on should work.
Where can I find the "Mark directory as" option? When I right click the directory in the project pane, I don't see that option. When I go to Settings > Python Interpreter, I don't see that option either.
Try configuring Python interpreter first.
It should appear when you right click a subdirectory in the project pane like this, "Mark directory as" is at the bottom of context menu:
Directory marked as this will have then "light blue folder icon" like you can see left to "mocks/generated" in the screenshot. I think the subdirectory you want to change cannot have other "mark" applied to it, e.g. it does not show for me in Rust projects if it's already mentioned in Cargo.toml.
Another possibility if you don't see it, try clean all caches (from File->Invalidate caches menu) and tick all checkboxes. I've fixed some weird bugs this way.
Yet another possibility is that this may work for some projects (like CMakeFile-based), but not others (e.g. Makefile-based)?
The project from screenshot uses C+Rust+micropython on ARM Cortex M4 (and build with Makefile+scons+rustc+...), so I usually instead create CMakeLists.txt, give -D definitions and includes in there. Then set the python source dirs, set Rust cargofile by right clicking dirs and cargofile, because CLion wouldn't have any way to work with this otherwise.
I have the exact same version of CLion and the same Ubuntu LTS version.
The trick I use for that complicated build system is that I let CLion create CMakeLists.txt automatically, in these steps:
It's a bit painful, but there's probably no simpler way for complicated build systems. Cherish that CMakeLists.txt like your child, you can always copy it to new version of cloned project/branch, sometimes just edit it for added/removed files or defines. I used this system for over 3 years and it's best way that worked for any IDE. Most of the time I just copy over the old CMakeFiles.txt, ocassionaly I need to edit it for removed/added files or defines. But usually won't take more than 30 mins to edit it even if 170 files changed, including added/removed (I either use the manual adding/removing files or let CLion autogenerate and add the defines and some includes by copying from old CMakeLists).
Only thing I haven't figured out yet is how to add assembler files to be able to be understood by the code insight/completion (but I think CLion doesn't understand ARM/Thumb assembler anyway).
BTW if you can't see the screenshot in the post above clearly because of zoom, do Copy Image Link or View Image, depending on browser or just click this image link - https://intellij-support.jetbrains.com/hc/user_images/Rfxvt05W3QFHCgDa4lKmCA.png (no idea why there is no zoom builtin in the forum).
Thank you so much! It was indeed the absence of a CMakeLists.txt file that caused the "Mark Directory as" option to be unavailable. After adding a CMakeLists.txt file, the option appeared.
Yes, unfortunately, for now the "Mark directory as" option is not available for Python projects which are not based on any build system supported in CLion (https://www.jetbrains.com/help/clion/project-models.html).
Feel free to comment or upvote https://youtrack.jetbrains.com/issue/CPP-13158. See https://intellij-support.jetbrains.com/hc/en-us/articles/207241135-How-to-follow-YouTrack-issues-and-receive-notifications if you are not familiar with YouTrack.