You can use relative imports only from in a module that was in turn imported as part of a package -- your script or interactive interpreter wasn't, so of course from . import (which means "import from the same package I got imported from") doesn't work. import mypackage will be fine once you ensure the parent directory of mypackage is in sys.path (how you managed to get your current directory away from sys.path I don't know -- do you have something strange in site.py, or...?)

To get your current directory back into sys.path there is in fact no better way than putting it there.

Answer from Alex Martelli on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-import-local-modules-with-python
How to Import Local Modules with Python - GeeksforGeeks
July 23, 2025 - Circular imports occur when two modules depend on each other, leading to errors. Refactor the code to break these dependencies or use local imports within functions to avoid issues. Using sys.path to Add Directories: The sys.path list determines where Python searches for modules.
🌐
Quentin Fortier
fortierq.github.io › python-import
How to import local modules with Python - Quentin Fortier
May 9, 2021 - Another possibility is to add the root directory to PYTHONPATH (before running the script). This is done automatically by poetry, for example. Relative imports were introduced in PEP 328 as a way to improve maintainability and avoid very long import statements. They must use the from .module import something syntax:
Discussions

Imports of local modules in python packages
You have 2 options. The first is to install your package in editable mode. You do this with the command pip install -e . Your working directory should be at the root of your project (i.e. same directory that contains pyproject.toml). The -e is short for --editable and the . is a reference to the current directory. When you install a package in editable mode, similarly to as if you had installed it from a wheel, you will be able to use the zero command. The difference is that any changes you make to your source code will automatically be picked up without needing to rebuild a wheel file or reinstall the package. The only caveat to this is if you make changes to pyproject.toml, then you will need to rerun the editable install command (however, you still don't need to build the wheel). Your other option is to execute your entry point as a module instead of a script. So rather than python zero/zero.py You instead run python -m zero.zero In both cases your imports should be left as import zero.add as add import zero.sub as sub More on reddit.com
🌐 r/learnpython
3
2
April 30, 2024
Import local module in python - Stack Overflow
I just change to new company and they use python to dev. When I see the source from ancestors, I see they use sys.path to import local module: More on stackoverflow.com
🌐 stackoverflow.com
python import local module from another module - Stack Overflow
You can import locally from a script (i.e. when you run main.py directly) but not from a module. You need to change main.py to an absolute import as well: from root.folder1 import * ... You will have to have a file called __init__.py in each directory so the Python interpreters treats that ... More on stackoverflow.com
🌐 stackoverflow.com
Imports of local modules in python packages : r/learnpython
Subreddit for posting questions and asking for general advice about all topics related to learning python. More on reddit.com
🌐 r/learnpython
🌐
Medium
medium.com › @vamsik23 › import-local-python-modules-a6e74950de4a
Import Local Python Modules. We all use modules in most of our… | by vamsi | Medium
August 18, 2021 - This basically adds the folder to python path so whenever python is looking for modules it will look in the appended folders too. Below is a short clip on how this works… ... Here we can see that I am adding folder_1 to python path using sys.path.append function so when I ask python to import some modules then it will check in folder_1 too.
🌐
Reddit
reddit.com › r/learnpython › imports of local modules in python packages
r/learnpython on Reddit: Imports of local modules in python packages
April 30, 2024 -
This is my second try at this question. 

I am attempting to build a python project for distribution and I am having trouble getting imports of modules in my project to work in both development and the build release. Here is a simple example which shows the issue I am having.

Given a directory structure as follows:

BuildTest:
 -> pyproject.toml
 -> src
     -> zero
	 -> add.py
	 -> sub.py
	 -> zero.py
 -> test
    empty folder

Contents of pyproject.toml:

[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "zero"
version = "1.0"
description = "No matter the value it is zero"
authors = [{ name = "Confused Dev", email = "CD@dumb.com" }]
license = {text = "MIT"}
readme = "README.md"
requires-python = ">=3.11"
dependencies = []

[project.scripts]
zero = "zero.zero:main"

[project.optional-dependencies]
dev = []

Contents of src/zero/add.py:

def add(x, y):
    """Add two numbers together."""
    return x + y

Contents of src/zero/sub.py:

def sub(x, y):
    """Subtract two numbers."""
    return x - y

Contents of src/zero/zero.py:

import sys

# these imports work for installed version
import zero.add as add
import zero.sub as sub

# these imports work for local development version
# import add as add
# import sub as sub

value = 0

def main(args=None):

    value = add.add(0, 10)
    value = sub.sub(value, 10)
    print(f"WTF? The value is {value}")

if __name__ == "__main__":
    sys.exit(main())

If I build this project using python -m build, it builds a .whl file which I can then install and when I run 'zero' it works as expected.

However, if I then go to the src/zero directory and run:

python zero.py

I get the following error:

Traceback (most recent call last):
  File "/Users/tbrown/Library/CloudStorage/Dropbox/Docs/projects/build_test/src/zero/zero.py", line 2, in <module>
    import zero.add as add
  File "/Users/tbrown/Library/CloudStorage/Dropbox/Docs/projects/build_test/src/zero/zero.py", line 2, in <module>
    import zero.add as add
ModuleNotFoundError: No module named 'zero.add'; 'zero' is not a package

If I instead use the following for imports:

import add as add
import sub as sub

It works as expected in the local directory. However, if I build with these imports and install, then the installed version gives me the same import errors about finding functions add and sub.

So the question is how do I structure the project with import code that works both when built and installed, and locally in the development directory? What am I missing?

I have hacked at this for a while with no success and searching the internet for the problem has only confused me.

🌐
Real Python
realpython.com › absolute-vs-relative-python-imports
Absolute vs Relative Imports in Python – Real Python
May 9, 2026 - standard library imports (Python’s built-in modules) related third party imports (modules that are installed and do not belong to the current application) local application imports (modules that belong to the current application)
🌐
YouTube
youtube.com › watch
Importing Your Own Python Modules Properly - YouTube
Today we learn how to properly import your own Python modules and also what the __init__.py file is about.◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾📚 Programming Books & Merch 📚🐍 ...
Published: July 6, 2022
Find elsewhere
🌐
Medium
medium.com › @jameskabbes › importing-your-local-package-with-pythonpath-c459dc209c7
Importing your Local Package with PYTHONPATH | by James Kabbes | Medium
September 21, 2023 - A common workaround is to add the parent directory of your package to sys.path before importing. This allows you to successfully import the module. >>> import sys >>> sys.path.append( '/path/to/parent_of_test_package' ) >>> import test_package >>> Let’s add to our package before moving along. This is a special file for Python packages.
🌐
Stack Overflow
stackoverflow.com › questions › 44162693 › import-local-module-in-python
Import local module in python - Stack Overflow
import sys, os sys.path.append(os.path.dirname(os.path.realpath(__file__))+"/folder/") from folder import module as module · I think it is not right. I suggest them to use package like this: ... They ignore my suggest. Am I right? I google but I don't find any hint for this. ... Your method will only work if the module is in your path. Python will add your current working directory to sys.path so it should work for local modules.
🌐
YouTube
youtube.com › watch
How to Import Local Scripts | How to Import Modules in Python - YouTube
Learn how to import local scripts in other Python files as a module.-----------------------------------------------------------------------------------💰Dona...
Published: January 1, 2024
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope. The search operation of the import statement is defined as a call to the __import__() function, ...
🌐
Medium
medium.com › swlh › how-to-import-from-local-packages-and-modules-563985d4e3e0
How to Import from Local Packages and Modules | by Brandon Kessler | The Startup | Medium
September 4, 2019 - From here, you can create and add ... or modules filled with methods, classes, or variables and import them into any file you’re working from in any directory with a simple import statement. We can also change the path for PYTHONPATH if we chose to move everything to a different location or rename the directory to something else. This is the easiest, permanent solution to working with local ...
🌐
Kaggle
kaggle.com › code › alexandervc › import-of-local-python-modules
Import of local Python modules
June 1, 2022 - Explore and run AI code with Kaggle Notebooks | Using data from Collect useful scripts
🌐
TheLinuxCode
thelinuxcode.com › home › how to import local modules with python (a deep, practical guide)
How to Import Local Modules with Python (A Deep, Practical Guide) – TheLinuxCode
January 18, 2026 - If I absolutely must reload, I use importlib.reload(module) and I do it with caution. This is also why file names matter: if you have a file named json.py in your project, Python may import your file instead of the standard library json. The search order is not neutral; it’s deterministic and local-first.
🌐
Readthedocs
mg.readthedocs.io › importing-local-python-modules-from-jupyter-notebooks › index.html
Importing Local Python Modules from Jupyter Notebooks — homepage
July 26, 2019 - If you re-use local modules a lot, you should consider turning them into proper Python packages which can be installed with Python’s package manager pip. The following sections are created from Jupyter notebooks which show multiple ways to import local Python modules, even if they are located in sub-directories.
🌐
Curiousefficiency
python-notes.curiousefficiency.org › en › latest › python_concepts › import_traps.html
Traps for the Unwary in Python’s Import System - Alyssa Coghlan's Python Notes
Another common trap, especially for beginners, is using a local module name that shadows the name of a standard library or third party package or module that the application relies on. One particularly surprising way to run afoul of this trap is by using such a name for a script, as this then combines with the previous “executing the main module twice” trap to cause trouble. For example, if experimenting to learn more about Python’s socket module, you may be inclined to call your experimental script socket.py.
🌐
Mend
mend.io › blog › developer › python import: mastering the advanced features
Python Import: Mastering the Advanced Features
November 8, 2023 - # Organizing imports according to PEP 8 style guide import os import sys # Importing standard library modules import math import datetime # Importing third-party libraries import requests import pandas as pd # Importing local modules from mypackage import mymodule · Namespace packages are a lesser-known but valuable feature of Python’s import system.
🌐
Python documentation
docs.python.org › 3 › tutorial › modules.html
6. Modules — Python 3.15.1 documentation
If you intend to use a function ... it to a local name: >>> fib = fibo.fib >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 · A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import ...