Absolute directory and Relative directory
import os
print(ps.path.abspath('.'))
print(ps.path.abspath('..'))
If we have a file b.py in /home/a , how can I import b.py to my file?
There are three solutions we can use.
1)
import sys
sys.path.append("/home/a/")
import b
from home.a.b import *
in home/ and a/ we must add init.py file.
3)
Add __init __.py(can keep empty) into the file.
Import home.a.b
本文探讨了Python中绝对路径与相对路径的区别,展示了如何获取当前目录的绝对路径,并提供了三种从不同目录层级导入模块的方法。包括修改sys.path、使用from...import*语法以及确保目录下存在__init__.py文件。
3903

被折叠的 条评论
为什么被折叠?



