首页 > 编程知识 正文

什么是绝对路径和相对路径,qpython

时间:2023-05-05 18:55:12 阅读:39449 作者:2949

absolute导入

you’vegottenuptospeedonhowtowriteimportstatementsandhowtostylethemlikeapro.nowit’stimetolearnalitttlemoreaboutaboutabsoluteimpprotete

anabsoluteimportspecifiestheresourcetobeimportedusingitsfullpathfromtheproject’sroot folder。

syntax和practical examples

let’ssayyouhavethefollowingdirectorystructure :

() module1.py

() module2.py

there’sa directory,project,whichcontainstwosub-directories,package1and package2. the package1directoryhastwofiles,mory

the package2directoryhasthreefiles 3360 two modules,module3.pyandmodule4.py,and an initialization file,_ _ init _.py su su py

let’sassumethefollowing :

package1/module2. pycontainsafunction,function1。

package2/_ _ init _.pycontainsaclass,class1。

包2/sub包1/module5. pycontainsafunction,function2。

thefollowingarepracticalexamplesofabsoluteimports :

从包1导入模块1

from package1. module 2导入功能1

从包2导入类1

从包2.sub包1.module 5导入功能2

notethatyoumustgiveadetailedpathforeachpackageorfile,from the top-levelpackagefolder.thisissomewhatsimilartoitsfilepath,)

Pros and Cons of Absolute Imports

absoluteimportsarepreferredbecausetheyarequiteclearandstraightforward.itiseasytotellexactlywheretheimportedresourceis, justbylookingatthestatement.additionally,absoluteimportsremainvalidevenifthecurrentlocationoftheimportstatemementchantes.iontchanges.ion

Sometimes,however,absoluteimportscangetquiteverbose,dependingonthecomplexityofthedirectorystructure.imaginehavingastatatatate

来自包1.sub包2.sub包3.sub包4.module 5导入功能6

that’sridiculous,right? Luckily,relativeimportsareagoodalternativeinsuchcases!

可撤销导入

arelativeimportspecifiestheresourcetobeimportedrelativetothecurrentlocation-thatis, thelocationwheretheimportstatementis.therearetwotypesofrelativeimports 3360 implicitandexplicit.implicitrelativeimportshave port

syntax和practical examples

thesyntaxofarelativeimportdependsonthecurrentlocationaswellasthelocationofthemodule,package,orobjecttobeimported.hereare

from.some _ moduleimportsome _ class

from . some _ packageimportsome _ function

from . import some_class

youcanseethatthereisatleastonedotineachimportstatementabove.relativeimportsmakeuseofdotnotationtospecifylocation。

asingledotmeansthatthemoduleorpackagereferencedisinthesamedirectoryasthecurrentlocation.twodotsmeanthatitisintheparentdirec tdirec atis,thedirectoryabove.threedotsmeanthatitisinthegrandparentdirectory,and soon.thiswillprobablybefamiliartoyouifyouseauseared

let’sassumeyouhavethesamedirectorystructureasbefore :

() module1.py

() module2.py

召回文件内容:

package1/module2. pycontainsafunction,function1。

package2/_ _ init _.pycontainsaclass,class1。

包2/sub包1/module5. pycontainsafunction,function2。

youcanimportfunction1into the package1/module1. pyfilethisway :

# package1/module1.py

from .module2 import function1

you’duseonlyonedotherebecausemodule2. pyisinthesamedirectoryasthecurrentmodule,which ismodule1.py。

youcanimportclass1and function2into the package2/module3. pyfilethisway :

# package2/module3.py

from . import class1

from.sub package1. module 5导入功能2

in the first导入语句,thesingledotmeansthatyouareimportingclass1fromthecurrentpackage.rememberthatimportingapackagessssess

In the second import statement,you’duseasingledotagainbecausesubpackage1isinthesamedirectoryasthecurrentmodule,whichismodule

资料来源: https://www.cn blogs.com/Chester-cs/p/13193634.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。