What does "Mark directory as sources root" really do?
Asked Answered
B

2

28

In Pycharm, if you right click in a folder inside your project, you can mark it as sources root, so then you can import modules from this folder and subfolders.

However doing it this way will only make your program runnable inside Pycharm, if I try to execute from outside Pycharm (eg from the console) it will complain that certain modules are not found, which is the problem that I'm facing.

If I mark a certain folder as sources root my program runs fine, but I need to understand what does it do so I can configure the program to find this modules even if not using Pycharm.

I want to know what does this option exactly do, and how can I get the same behaviour without using it.

It is just adding a __init__.py file in the root folder? Is it doing something like:

import sys
sys.path.insert(0, my_folder)
Broadnax answered 5/8, 2019 at 14:32 Comment(1)
Are you sure you don't also have a different configuration (python interpreter, environment variables) in pycharm than in your console ?Tiana
S
14

First __init__.py marks a directory as a regular package directory(this is pre 3.3, in 3.3+ its not required anymore). With this, python will look for submodules inside that directory for imports.

"Mark directory as sources root" sets the PATH(or PYTHONPATH) for the environment. In shell, it will be like,

export PYTHONPATH="${PYTHONPATH}:/your/source/root"

PYTHONPATH holds the default search path for module files. This will enable the interpreter to search for the modules in the appended path. The default value is installation dependent(normally it contains path to Python binaries).

You can also manipulate this search path using sys.path from inside a python file.

Sands answered 9/9, 2020 at 12:9 Comment(2)
Although when I run echo $PYTHONPATH it outputs nothing, and opening interpreter settings in pycharm it only shows virtual environment folders and base interpreter paths there. So where exactly is this source root path being stored?Vada
it also triggers code analysis.Allout
S
0

After struggling with PyCharm's Mark as project root I found a solution. Keep in mind that I'm doing this from a Windows 11 machine.

Answer: Essentially, "Mark directory as sources root" adds the root of your project (the parent of the src\ folder) to the PYTHONPATH variable.

One can easily check this by doing the following:

  1. On a terminal (outside PyCharm - I did this on Windows 11 CMD) navigate to your project root (the same place where you have the src\ folder) and activate the virtual environment;
  2. With the virtual environment activated execute the command python -c "import sys; print('\n'.join(sys.path))" to make the terminal print all the values of the sys.path variable, the equivalent of the PYTHONPATH;
  3. You will notice that the root of your project isn't listed there - and this is what Mark as project root does, it adds your project root to the sys.path values;
  4. You can replicate what PyCharm does by then executing the command: set PYTHONPATH=%PYTHONPATH%;C:\\--PATH--\\--TO--\\--PROJECT ROOT--; and this command will add your project's root to the PYTHONPATH variable. (executing the command python -c... again will show it);
  5. Finallly, if you now execute whatever command you executed inside PyCharm that worked, will also work from the terminal;
Sanjuanitasank answered 3/4 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.