import, uv, and module/package in Python
A really good video talks about morden Python project structures and tools like uv/poetry.
Also explained what exactly import does
Video link module and package, uv, and package structure
1 module
Here is the example with mymath.py and main.py under same dir
print("exeucte mymath.py")
PI=3.14
def add(a,b)
return a+b
def add_global(key, value)
g = globals()
g[key]=value
and here is the main function
import mymath
# import mymath as mm
# mm = mymath
mypath.add(1,2)
print(type(mypath)) # <class `module`>
- So the
importis actually creating an instance from classmodule. It can be assigned to another variablemm, and that’s essencially whatimport mymath as mmdoes importa file multiple time, only the first time would execute the fileimport mymath as mm # file execution import mymath as mm2 # no execution assert id(mm) == id(mm2)- The module created by
importis the global namespaceprint(dir(mm)) # [...,'PI','add'] print(mm.foo) # error, no foo variable mm.add_global('foo', 'bar') print(mm.foo) # print bar - Modify imported value won’t change the value in the original file
from mymath import PI PI=999 print(PI) #999 print(mymath.PI) # 3.14 from mymath import PI print("PI still is {PI}") # PI still is 3.14
2 package
- Assume there is folder called
mymathwith__init__.pyin it The folder will have higher priority over a file and it’s imported as a package# - mymath # - __init__.py (optional) # print("executed __init__.py") # - mymath.py # - main.py import mymath # executed __init__.py import sys print(sys.path) # [., sys_path, ...] - We can use
PYTHONPATHenv var to addsys.path - When
import mydir.subdir, it will firstly addmydirin current namespace, then addsubdirto the namespace ofmydir.#- mydir # -subdir import mydir.subdir print(dir()) # [..., mydir] print(dir(mydir)) # [..., subdir] - When
from mydir import subdir, it will add bothmydirandsubdirin current namespace, then addsubdirto the namespace ofmydirsame as above.from mydir import subdir import mydir print(dir()) #[..., mydir, subdir] NEW print(dir(mydir)) # [..., subdir] SAME as import mydir.subdir - We can have relative import
from . import aaa from .. import bbb
3 uv
pip freeze > requirments.txtbut currentrequirments.txthave issues of- List all the direct dependences and indirect dependences
pip uninstallONLY remove the package but left orphan packages
- New python standard is using
pyproject.tomlwith dependencies
and multiple tools like mypyandpytestall have their config file built in[project] name='proj' version='0.1.0' [tool.mypy] exclude=['build/'] [tool.pytest.init_options] testpaths=['tests'] - Following steps can be combined into one
uvcommand, which is the high level wrapper forpip#python -m venv .venv #source .venv/bin/activate #vi pyproject.toml #pip install -e . uv add flask uv synccan read frompyproject.tomland create venv and finish all the installationuv run main.pycan run the code inside the venv automatically
4 wheel and hatchling
.whlfile is essencially a zip package and beunzipto view contentsMETADATAfile inside thepackage.version.dist-infocontains the indirect dependencies- Suggest to use the same folder name as the package name to avoid confusion

- There are multiple options can be used here and we choose the frontend uses
build(Python default) and backend useshatchling. The sample config is[build-system] requires=['hatchling'] build-backend='hatchling.build'