To use import in Python, put the statement import module_name at the top of your file and then reach anything inside that module with dot notation, for example math.sqrt(16). Python looks for that module first in the folder containing the script you are running, then in the directories listed in the PYTHONPATH environment variable, and finally in the standard library and installed packages. That search order is the whole story of most import problems, so it helps to understand it before you continue.

You need a working Python installation (version 3.6 or newer is a reasonable baseline for current code) and a code editor or IDE. You can follow along in an interactive session started with python or python3 in a terminal, but for anything beyond a quick test it is better to save a file such as main.py and run it with python main.py. Only install third-party packages once you reach a step that genuinely needs one.

What You'll Need

  • A Python interpreter installed and available on your PATH — check with python --version or, on macOS and Linux, python3 --version.
  • A text editor that saves plain .py files.
  • The folder containing your main script in mind, because Python resolves imports relative to it.
  • Optionally, pip for packages outside the standard library. Standard library modules such as math, os and json need nothing extra.

Step-by-Step

  1. Import a standard library module at the top of your file. Write import math on its own line. By convention all imports live at the top of the file, after any module docstring, so a reader can see the file's dependencies at a glance.

  2. Call members with dot notation. Type print(math.sqrt(81)) and run the script. It prints 9.0. Everything defined at the top level of math is now available as math.something. This is the safest style because the source of every name stays visible.

  3. Import only what you need with from ... import .... Write from math import sqrt, pi and then use sqrt(81) and pi directly. This shortens code when a module is verbose, but it hides where the names come from, so keep the list short and resist doing it for large modules.

  4. Use an alias when the name is long or clashes. Write import numpy as np or import datetime as dt. You then call np.array(...). Aliases are useful when a module genuinely has a long name; avoid inventing unusual aliases that other people reading your code will not recognise.

  5. Import one module from a package. Packages are folders of modules. Write from os.path import exists and call exists("data.txt"). The dotted path is just folder names separated by dots, so os.path means the path module inside the os package.

  6. Import a module you wrote yourself. Create helpers.py in the same folder as your main script and write import helpers. If helpers.py defines def greet(name): return "Hello " + name, then helpers.greet("Sam") works. The file name becomes the module name, without the .py extension.

  7. Arrange your project as a package when it grows. Put your modules in a subfolder and add an empty __init__.py file to it. A folder named tools containing dates.py and __init__.py can be imported with from tools.dates import today_iso when you run your script from the folder that contains tools.

  8. Install third-party packages with pip before importing them. In a terminal, activate a virtual environment and run python -m pip install requests. Then import requests works in that environment. Using python -m pip rather than bare pip guarantees you install into the interpreter you actually run. To create an environment first, run python -m venv .venv, then activate it (on Windows, .venv\Scripts\activate; on macOS or Linux, source .venv/bin/activate).

  9. Give Python a directory to search with PYTHONPATH only when you must. If a module lives outside your project, you can set the PYTHONPATH environment variable to that folder. It is usually cleaner to install the code or move it inside your project than to depend on a machine-specific variable.

  10. Run and verify. Execute python main.py from the project folder. If you see output, the import chain resolved. If not, read the traceback from the bottom upwards; the last line names the error and the file it came from.

Common Mistakes

  • Naming a file after a standard module. A file called random.py or json.py in your project will be found before the real library, and import random then imports your file. Rename it.
  • Using the wrong case or an extension. Write import helpers, never import helpers.py. Module names are case-sensitive on Linux and macOS.
  • File names that are not valid identifiers. A file such as my-helper.py cannot be imported with a normal import statement because the hyphen is not allowed in a Python name. Use underscores.
  • A file inside a package importing its sibling by bare name. Inside tools/report.py, writing import dates usually fails. Use from . import dates for a sibling or from tools import dates when run as part of the package.
  • Wildcard imports. from math import * pollutes the namespace and hides conflicts. Import the specific names instead.
  • Circular imports. Two modules that import each other at the top can deadlock. Move one import inside the function that needs it, or restructure so only one direction depends on the other.
  • Confusing a virtual environment with the global Python. Installing a package in one environment does not make it visible in another. Check which interpreter is active before debugging the import itself.
  • Expecting a relative import in a script you run directly. Running python tools/report.py makes Python treat that file as a top-level script, so package-relative imports break. Run python -m tools.report from the parent folder instead.

Tips

  • Keep imports alphabetical or grouped (standard library, third-party, local) so gaps and duplicates stand out.
  • Run python -c "import sys; print(sys.path)" to see the exact directories Python searches. It answers most "why is it not finding it" questions immediately.
  • Use a fresh virtual environment per project to keep dependency versions separate and reproducible.
  • Prefer import module over from module import name for anything you call more than once, since the dotted call makes the source obvious during review.
  • If a module has a slow import, importing it inside the function that needs it can speed up startup of tools that only use it occasionally.
  • Name your own modules distinctly, for example myproject_utils rather than utils, to reduce the chance of shadowing a library.

Troubleshooting

ModuleNotFoundError: No module named 'x'. This is by far the most common one. Check the spelling and case, confirm the file is in the folder you run from, and check that the package is installed in the active interpreter with python -m pip show x. If pip lists it but Python cannot see it, you are very likely running a different interpreter than the one pip installed into.

ImportError with "cannot import name". The module exists but does not define that name. Either you mistyped the name, or the function you want lives in a different place in the current library version. Check the library's own documentation rather than guessing.

Importing your own file returns None or seemingly empty values. Confirm the code that sets those values actually runs at import time, and remember that module-level code executes once, the first time the module is imported. A later change to the file will not take effect in a long-running interpreter until you restart it.

Code works in one folder but not another. Imports depend on where you run from, not where the file lives. Either run from the project root or add the project root to PYTHONPATH. Do not scatter sys.path hacks through your files.

Circular import error. Look at which two files import each other. Move one of the imports inside the function that uses it, or extract the shared code into a third module both can import.

If that did not work, copy the full traceback and compare the first line of the error against the exact filename you expect. Then start a brand-new terminal session so an old environment or PYTHONPATH setting cannot mislead you, and re-run the script from the project root.

Frequently Asked Questions

What is the difference between import module and from module import name?

The first binds the whole module object, so you must use dot notation to reach its members. The second binds just the names you list, so they can be used directly. Both load the module once.

Does Python run a module's code when it is imported?

Yes. Everything at the top level of the module runs the first time it is imported, which is why modules should usually define functions and classes rather than perform long tasks on import. The if __name__ == "__main__": guard keeps the main-script part from running when the file is imported rather than executed.

Why does my import work in the editor but fail on the command line?

They are probably using different interpreters or different working directories. Check the interpreter your editor has selected and the folder you run from in the terminal; both must match, or PYTHONPATH must include the project root.

What is __init__.py for?

It turns a folder into a regular package and can run setup code. It is no longer required for namespace packages in recent Python versions, but including an empty one makes your intent clear and keeps older tooling happy.

Can I import a module from a full file path?

Not with a plain import statement. Put the file in the search path, make it part of a package, or move it into your project folder. Adding the folder to sys.path at runtime works but is best avoided in code you intend to share.

Where does Python look for modules, in order?

It checks the script's own folder first, then PYTHONPATH entries, then the standard library, and finally the site-packages folder of installed packages. That order is why a local file with a library's name can shadow the real module.