Why don't languages auto import everything based on namespace?

This is basically a continuation of this question but with a slightly altered premisse: Say we have a language like C++ / python that uses namespaces to distinguish functions and classes from other modules, would it not in theory be possible to leave out all the includes / imports and have the compiler automatically resolve the tokens back to the corresponding library? The way this would work is that the build system puts the dependencies into the compilers context, and the compiler will search through a complete index of tokens directly instead of going through the imported modules. The imports can sometimes be resolved by modern IDE's, but this is never at the language level and even for modern languages you need to do it. Why though? Consider this simple case: #include int main() { std::cout

Jan 14, 2025 - 15:55
Why don't languages auto import everything based on namespace?

This is basically a continuation of this question but with a slightly altered premisse:

Say we have a language like C++ / python that uses namespaces to distinguish functions and classes from other modules, would it not in theory be possible to leave out all the includes / imports and have the compiler automatically resolve the tokens back to the corresponding library?

The way this would work is that the build system puts the dependencies into the compilers context, and the compiler will search through a complete index of tokens directly instead of going through the imported modules.

The imports can sometimes be resolved by modern IDE's, but this is never at the language level and even for modern languages you need to do it. Why though?

Consider this simple case:

#include 

int main() {
    std::cout << "Hello World" << std::endl;
}

In 99% of the cases the user wants to import std::cout from iostream. While C++ can't necessarily guarantee that there isn't a competing std::cout out there, a language designed with this in mind could potentially (hypothesis). So why isn't it done?

A huge benefit would be that refactorings would become so much easier, since you don't have to flip through all your dependencies to check which ones to import and which not (even of course LSP's will make that easier). And at the same time you're never importing too much, only the tokens that you really need...

The question is: Is it necessary or just tradition?