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.
Imports of local modules in python packages
Import local module in python - Stack Overflow
python import local module from another module - Stack Overflow
Cannot import local module python - Stack Overflow
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.
Keep it simple:
try:
from . import mymodule # "myapp" case
except:
import mymodule # "__main__" case
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 folderContents 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 + yContents of src/zero/sub.py:
def sub(x, y):
"""Subtract two numbers."""
return x - yContents 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 packageIf 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.