Import brings code from other files into the one you're writing
An import is a statement that tells your program to use code that lives in a different file or library. Instead of rewriting the same functions over and over, you import them once and use them as many times as you need. Most programming languages have an import system — Python calls it import, JavaScript uses import or require, and Java uses import — but they all do the same thing: they load external code into your current file.
When you import something, you're not copying the code into your file. You're creating a reference to it. Your program reads that reference, finds the file or library you named, and makes those functions or variables available for you to use. This keeps your files smaller, makes your code easier to read, and lets multiple files share the same code without duplication.
Key Takeaways
- An import statement loads code from another file or library so you can use it in your current program without rewriting it.
- Different languages use different syntax — Python uses import module_name, JavaScript uses import { function } from 'file', and Java uses import package.name — but the purpose is identical.
- You can import an entire library, a single function, or specific variables, depending on what your code needs and what the language allows.
- Imports are resolved when your program runs, so the file or library you're importing must exist and be in a location your program knows how to find.
How imports work in different languages
Python imports are straightforward. If you write import math, Python loads the math library and you can use functions like math.sqrt(). If you only need one function, you can write from math import sqrt, which lets you call sqrt() directly without typing math. first. Python looks for the file in your project folder, then in the standard library that came with Python, then in any installed packages.
JavaScript has two import systems. Older code uses require('module_name'), which is synchronous — it loads the code immediately and waits for it to finish. Newer code uses import { functionName } from './file.js', which is asynchronous and works better in browsers. The syntax is stricter: you have to specify exactly what you're importing and where the file is located, including the file extension.
Java imports work at the top of your file and follow a naming convention based on folders. If you have a file in a folder called utilities, you write import utilities.MyClass. Java also has the wildcard import import utilities.*, which imports everything in that folder, though many developers avoid it because it makes the code less clear about what you're actually using.
The difference between importing a library and importing your own code
When you import a library like NumPy in Python or React in JavaScript, you're importing code that someone else wrote and packaged for distribution. These libraries are usually installed in a central location on your computer or downloaded from a package manager like npm or pip. Your program knows where to find them because the package manager registered them during installation.
When you import your own code — a file you created in your project — the import statement has to point to the exact location of that file. In Python, you might write from .utils import helper_function, where the dot means "look in the same folder as this file." In JavaScript, you write import { helper } from './utils.js', with the path relative to your current file. Your program doesn't know to look for your own files automatically; you have to tell it where they are.
What happens when an import fails
If you try to import something that doesn't exist, your program will crash with an error before it runs. Python says ModuleNotFoundError, JavaScript says Cannot find module, and Java says cannot find symbol. These errors mean either the file doesn't exist, the path is wrong, or the library isn't installed.
The most common cause is a typo in the import statement or a missing library. If you write import numpyy instead of import numpy, Python won't find it. If you installed a package but didn't save it to your project's dependency list, someone else running your code won't have it installed and will get an error. This is why most projects keep a file like requirements.txt (Python) or package.json (JavaScript) that lists every library the project needs.
Circular imports and why they cause problems
A circular import happens when File A imports from File B, and File B imports from File A. When your program tries to load File A, it immediately needs File B. But File B immediately needs File A, which is still loading. The program gets stuck in a loop and crashes with a circular dependency error.
Circular imports are usually a sign that your code organization needs rethinking. If two files need each other's code, consider moving the shared code into a third file that both can import from. Or restructure so that one file imports from the other, but not the reverse. Most linters — tools that check your code for problems — can detect circular imports before you run your program.
Namespaces and why import statements use them
A namespace is a container that holds names — function names, variable names, class names — so they don't collide with names in other parts of your code. When you write import math and then call math.sqrt(), the word math is the namespace. It tells Python "look for sqrt inside the math library, not somewhere else in my code."
Without namespaces, if you imported two libraries that both had a function called process(), your program wouldn't know which one to use. Namespaces solve this by keeping them separate. You can also rename a namespace when you import it — import numpy as np in Python lets you write np.array() instead of numpy.array(). This is common for libraries with long names.
When to import at the top versus inside a function
Most style guides say to put all your imports at the very top of your file, before any other code. This makes it easy to see at a glance what your file depends on. It also means the import happens once, when the file loads, rather than every time a function runs.
Sometimes you might import inside a function instead — for example, if a library is large and you only use it in one specific function that rarely gets called. Importing it only when needed can make your program start faster. However, this is the exception. The standard practice is to import everything at the top, and most linters will flag imports that appear later in the file as a style violation.
Frequently Asked Questions
What's the difference between import and require?
require is the older JavaScript way to import code; it loads synchronously and is still used in Node.js projects. import is the newer standard that works in browsers and modern Node.js; it loads asynchronously. Both do the same job, but import is preferred for new projects.
Can I import just one function from a library instead of the whole thing?
Yes. In Python, write from math import sqrt to import only sqrt. In JavaScript, write import { sqrt } from 'math-library'. This is called a named import and makes your code clearer about what you're actually using.
What does "relative import" mean?
A relative import points to a file based on where your current file is located. In Python, from .utils import helper means "look for utils in the same folder as me." In JavaScript, import x from './utils.js' does the same thing. An absolute import, by contrast, points to a library or a fixed location on your computer.
Why does my import work on my computer but not when someone else runs my code?
You probably installed a library on your computer, but didn't record it in your project's dependency file. When someone else clones your code, that library isn't installed for them. Always save libraries to requirements.txt (Python) or package.json (JavaScript) so others know what to install.
Can I import code from a website or a GitHub repository?
Yes, but not directly in your import statement. You use a package manager like pip or npm to download the code first, then import it normally. For example, pip install requests downloads the requests library from the internet, and then you can write import requests in your code.