What importing means and why you need it

Importing means bringing code that someone else wrote — or code you wrote in a separate file — into the program you are working on right now. Instead of rewriting the same functions over and over, you import them once and use them as many times as you need.

When you import, you are telling your program: "Go find this code somewhere else and make it available to me." That code might live in a file on your computer, in a folder within your project, or in a library that thousands of programmers share. The import statement is the instruction that makes the connection.

Most real programs are built this way. A web process might import a library for handling dates, another for validating email addresses, and another for connecting to a database. Without imports, every programmer would have to write date-handling code from scratch, which wastes time and introduces bugs.

Key Takeaways

  • An import statement tells your program where to find code and what name to call it by in your current file.
  • Built-in libraries come with your programming language and do not require installation; third-party libraries must be installed first using a package manager.
  • The syntax for importing changes depending on your language — Python uses import, JavaScript uses import or require, and other languages have their own rules.
  • When an import fails, the most common causes are a typo in the name, the library not being installed, or the file path being wrong.
  • Organizing your imports at the top of your file and grouping them by type makes your code easier to read and debug.

Built-in libraries versus third-party packages

Every programming language comes with a built-in library — code that is already installed and ready to use the moment you install the language itself. In Python, the math library is built-in, so you can import it without doing anything else. In JavaScript running in a browser, you have built-in objects like Math and Date that you can use when ready.

A third-party package is code written by someone outside the language's core team. If you want to use a library called requests in Python (which makes it straightforward to fetch data from websites), you have to install it first. You do this using a package manager — a tool that downloads and sets up the library for you.

Python uses pip as its package manager. JavaScript projects typically use npm (Node Package Manager) or yarn. When you run pip install requests or npm install express, the package manager downloads the library and stores it in a folder on your computer. Only after installation can you import it into your code.

How to import in Python

In Python, the simplest import looks like this: import math. This brings in the entire math library. To use a function from it, you write the library name, then a dot, then the function name: math.sqrt(16) returns 4.

If you only need one specific function, you can import just that function: from math import sqrt. Now you can call sqrt(16) directly without typing math. first. This is useful when you are using the same function many times and want shorter code.

You can also rename an import to make it shorter: import numpy as np. The numpy library is now called np in your code, so you write np.array() instead of numpy.array(). This is common practice for libraries with long names.

Always put your imports at the very top of your file, before any other code. Python reads from top to bottom, and if you try to use an imported function before you import it, the program will crash with a NameError.

How to import in JavaScript

JavaScript has two main import styles depending on where your code runs. In modern JavaScript (called ES6 modules, used in most new projects), you use the import keyword: import express from 'express'. This brings in the express library, which is popular for building web servers.

Older JavaScript projects use require instead: const express = require('express'). The require style is still common in Node.js projects, especially older ones. Both do the same thing — they load the library and store it in a variable you can use.

You can import specific functions from a library: import { readFile } from 'fs'. The curly braces mean "import only this one function from the library." Without the braces, import fs from 'fs' imports the entire library, and you would call fs.readFile() instead.

Like Python, put all your imports at the top of the file. JavaScript will read them before running the rest of your code.

Installing packages before you can import them

Before you can import a third-party library, it must be installed. The process is the same idea across languages: you run a command in your terminal, the package manager downloads the library, and it gets stored in a folder called node_modules (JavaScript) or site-packages (Python).

In Python, you open your terminal and type pip install requests. The pip tool downloads the requests library and everything it depends on. After that, you can import it in any Python file in that project.

In JavaScript, you type npm install express. The npm tool downloads express and stores it in node_modules. Your project also gets a file called package.json that lists all your installed libraries, so other people (or you on a different computer) can install the same versions by running npm install with no arguments.

If you try to import a library that is not installed, your program will crash with an error like "ModuleNotFoundError" in Python or "Cannot find module" in JavaScript. The fix is always the same: install it first using your package manager.

Organizing imports and avoiding common mistakes

As your program grows, you will have many imports. Organize them in this order: built-in libraries first, then third-party packages, then your own files. Separate each group with a blank line. This makes it straightforward to see at a glance what your program depends on.

A common mistake is importing something with the wrong name. If a library is called pillow but you write import PIL, it will fail — PIL is the name you use inside your code, but the package name (what you install) is pillow. Check the library's documentation to see what to type in your import statement.

Another mistake is forgetting to install a package before importing it. If you copy code from a tutorial that uses a library you have never heard of, install it first. The error message will tell you the package name, so you can run pip install [name] or npm install [name].

Circular imports — where file A imports file B, and file B imports file A — can cause crashes. If you run into this, reorganize your code so that one file imports from the other, but not the other way around. Most of the time, moving a shared function into a third file solves the problem.

When imports fail and how to fix them

If your program crashes with an import error, check these things in order. First, make sure the library is installed. Run pip list (Python) or npm list (JavaScript) to see what is installed. If the library is missing, install it.

Second, check the spelling of the import statement. import numpy is not the same as import Numpy — most libraries use lowercase names. Look at the library's official documentation to see the exact spelling.

Third, check the file path if you are importing your own code. If you have a file called helpers.py in the same folder, you import it as import helpers. If it is in a subfolder called utils, you import it as from utils import helpers. Get the path wrong and the import fails.

Fourth, make sure you are running your code from the right location. If you are in a different folder than your project, Python and JavaScript may not find your libraries. Open your terminal in the project folder before running your program.

Frequently Asked Questions

What is the difference between import and require?

import is the modern JavaScript standard and is used in most new projects. require is the older Node.js style and still works in many projects. They do the same thing — they load a library — but the syntax is different. Most new code uses import.

Do I have to install built-in libraries?

No. Built-in libraries come with your programming language and are ready to use when ready. You only need to install third-party packages using a package manager like pip or npm.

Why does my import work on one computer but not another?

The library is probably not installed on the second computer. When you move your code to a new machine, you need to install all the third-party packages again. Use pip install -r requirements.txt (Python) or npm install (JavaScript) to install everything at once.

Can I import code from a file in a different folder?

Yes, but the syntax depends on your language and folder structure. In Python, if a file is in a subfolder, you use from folder_name import file_name. In JavaScript, you use a relative path: import helper from './utils/helper.js'. Check your language's documentation for the exact syntax.

What does "module not found" mean?

It means the library you are trying to import does not exist or is not installed. Check the spelling of the import statement and make sure you installed the package using your package manager. If you copied code from a tutorial, the tutorial may use a library you have not installed yet.