Absolute and Relative directory

本文探讨了Python中绝对路径与相对路径的区别,展示了如何获取当前目录的绝对路径,并提供了三种从不同目录层级导入模块的方法。包括修改sys.path、使用from...import*语法以及确保目录下存在__init__.py文件。

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值