Marking Directory as a python source root


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.


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:

  1. clone project recursively
  2. rename Makefile to something else, like Makefile.orig so CLion won't treat it as Makefile project and get all confused
  3. then follow this to have CLion to autogenerate CMakeLists.txt - (this step won't work if CLion finds Makefile in root) - just open a c/cpp file from the project into editor and you should get the "Create CMakeLists.txt" popup like shown in the link
  4. edit the generated CMakeLists.txt, add your defines, extra includes and so on (CLion will guess most of includes, but defines have to be entered manually)
  5. edit and reload CMakeLists.txt until your completion, etc. work
  6. save this CMakeLists.txt somewhere very, very safe
  7. delete this clone
  8. clone the project again, into different directory (!!!), put that CMakeLists.txt you just created into root and only then open it with CLion, selecting it as CMake project (you can fix build settings later to whatever build system you have)
  9. if you don't delete and re-clone the project, you will trigger bug where CLion may "forget" that it's CMake project on the next start of CLion and undo that work
  10. the above bug seems can be fixed by forcing clean with "Invalidate caches", but it's just easier to keep your curated CMakeLists.txt for project, and copy it to root of your project's dir after you clone it, but before open in CLion
  11. then add the Python interpreter, set a subdir(s) as "source" like in the above screenshot (and also Cargo.toml if you are using Rust or maybe some other languages)

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 - (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 (

Feel free to comment or upvote See if you are not familiar with YouTrack.


Please sign in to leave a comment.