设置代码库
- readme.md
- LICENSE.md
- .gitignore
- 一个目录(与项目名相同)
模块和包
模块相当于一个.py 文件
包是包含一个或多个模块的目录,此目录必须有个 init.py
不包含 init.py 的包文件夹称为命名空间包
import 相关问题
导入模块
from src import test2
从模块导入函数
from src.test2 import calculate_product
覆盖问题
函数名重复了怎么办,用 as 换替换名
from src.test2 import calculate_product as test2_calculate_product
导入的方式
绝对路径导入
相对路径导入:. .. ... ....
控制包的导入
init.py 可以控制模块的导入方式 from src imort *
在 init.py 中定义 **all** = ["模块 1","模块 2"]
入口点
当导入模块的时候,会被赋予一个变量 name,当此模块直接运行的时候 name 被设置为 main
from src import test2
print(f"{test2.__name__} module imported successfully.")
print(f"{__name__} is running as the main program.")
输出:
src.test2 module imported successfully.
__main__ is running as the main program.