Why is it necessary to “import” a library first before usage?

My question regards the use of import statements.

In most programming languages I’ve come across (e.g. Python, Scala, Go), it is necessary to first import a library before you can use its functions.

Why do you need to specify this import statement?

Why can’t you just use a function/method, and the environment finds and “imports” the related library/functions automatically?

3

I can think of three main reasons for it:

  • The name of a function or class could be ambiguous. There are dozens of image handling libraries. From which one do I want to use the Image class?

  • In the case there are multiple same named classes that you want to use at the same time, import in python and similar statements in other languages, allow you to alias a classname, so each class can be used with an unambiguous name. You need a statement to do that, so import is used to do it.

  • The library might not be present on the machine. A failing import statement tells you immediately what you need to do to resolve the issue. A run-time failure is much less obvious.

2

What you suggest is possible in some languages like Java and C#: If you use the fully qualified name of the class you want to use, you don’t need to explicitly import anything, the environment will automatically find it.

But you do need to use fully qualified names like new System.Web.MailMessage(). This is a hassle (especially with deeper namespaces). By importing the namespace, you can use the unqualified name like just new MailMessage().

So why isn’t all namespaces imported by default then? Because of name collisions. For example there is both a System.Net.MailMessage and a System.Web.MailMessage. You have to specify explicitly which namespace to import to avoid ambiguity.

Of course library developers could avoid reusing the same name (and it is generally good practice to avoid). But when you have multiple independently developed 3rd party libraries, there is no way to avoid this universally. So this is solved by namespacing and imports.

I don’t know Scala, but I suspects it works the same was as Java.

Python is quite different due to its dynamic nature. The Python import statement actually executes the top-level code in the library file. This code defines the functions and classes in the library. So without the import statement, the types would simply not exist, so there would be no way for the language to find them.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật