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
import
is actually creating an instance from classmodule
. It can be assigned to another variablemm
, and that’s essencially whatimport mymath as mm
does import
a 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
import
is 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
mymath
with__init__.py
in 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
PYTHONPATH
env var to addsys.path
- When
import mydir.subdir
, it will firstly addmydir
in current namespace, then addsubdir
to the namespace ofmydir
.#- mydir # -subdir import mydir.subdir print(dir()) # [..., mydir] print(dir(mydir)) # [..., subdir]
- When
from mydir import subdir
, it will add bothmydir
andsubdir
in current namespace, then addsubdir
to the namespace ofmydir
same 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.txt
but currentrequirments.txt
have issues of- List all the direct dependences and indirect dependences
pip uninstall
ONLY remove the package but left orphan packages
- New python standard is using
pyproject.toml
with dependenciesand multiple tools like
mypy
andpytest
all 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
uv
command, 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 sync
can read frompyproject.toml
and create venv and finish all the installationuv run main.py
can run the code inside the venv automatically
4 wheel and hatchling
.whl
file is essencially a zip package and beunzip
to view contentsMETADATA
file inside thepackage.version.dist-info
contains 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'